Go to the documentation of this file. 1 package com.cliffc.aa.HM;
5 import org.jetbrains.annotations.
NotNull;
32 static final HashMap<String,HMType>
ENV =
new HashMap<>();
68 while( work.
len()>0 ) {
93 @NotNull @Override
public Iterator<HMVar>
iterator() {
return new Iter(); }
94 private class Iter implements Iterator<HMVar> {
105 private final HashSet<Syntax>
_work =
new HashSet<>();
136 if(
_kids==
null )
return true;
137 for(
Syntax kid :
_kids )
if( kid._hm==
null )
return false;
147 if( !work.
has(
this) )
151 if( !kid.check_progress(work) )
171 if(
_hm!=
null )
return null;
181 throw new RuntimeException(
"Parse error, "+
_name+
" is undefined");
205 if( !(
_hm instanceof
Oper) )
return true;
206 if( !((
Oper)
_hm)._name.equals(
"->") )
return true;
209 return !fcn.
eq(tnew) || !rez.
eq(trez);
228 tnew.
union(tbody,work);
258 nfun.
union(tfun,work);
267 if( !(tfun instanceof
Oper) )
return true;
268 if( !((
Oper)tfun)._name.equals(
"->") )
return true;
270 return !arg0.
eq(targ);
290 static final HashMap<HMVar,HMVar>
EQS =
new HashMap<>();
295 HashMap<HMVar,HMVar> vars =
new HashMap<>();
296 HashMap<Oper,Oper> opers =
new HashMap<>();
302 if( vstk==
null )
return false;
317 if( y instanceof
Oper )
329 if(
_u!=
null && !debug )
return _u.
_str(sb,dups,debug);
333 if(
_u!=
null )
_u.
_str(sb.
p(
">>"),dups,debug);
339 if( u==
null )
return this;
340 if( u.
_u==
null )
return u;
342 while( u.
_u!=
null ) u = u.
_u;
349 if( that instanceof
HMVar ) that = that.
find();
350 if(
this==that )
return this;
353 System.out.println(
"recursive unification");
355 if( that instanceof
HMVar ) {
362 throw new RuntimeException(
"Cannot unify "+
this+
" and "+that);
364 if( that._ids==
null ) that._ids =
_ids;
365 else that._ids.addAll(
_ids);
368 if( that._ids!=
null ) {
369 for(
Ident id : that._ids )
id._hm=
null;
370 work.addAll(that._ids);
376 if(
this==v )
return true;
377 if( v==
null )
return false;
379 if( !(v2 instanceof
HMVar) )
return false;
380 assert
_u==
null && v2.
_u==
null;
390 : vars.computeIfAbsent(
this, e ->
new HMVar(
_t));
401 if(
_name.equals(
"->") ) {
402 if( debug ) sb.
p(
"v").
p(
_uid).
p(
":");
410 if( debug ) sb.
p(
_uid);
413 t._str(sb,dups,debug).
p(
',');
419 if(
this==that )
return this;
420 if( !(that instanceof
Oper) )
return that.
union(
this,work);
421 if(
_uid < that._uid )
return that.
union(
this,work);
425 throw new RuntimeException(
"Cannot unify "+
this+
" and "+that);
426 for(
int i=0; i<
_args.length; i++ )
429 if( that._ids!=
null ) {
for(
Ident id : that._ids )
id._hm=
null; work.addAll(that._ids); }
434 if(
this==v )
return true;
435 if( !(v instanceof
Oper) )
return false;
441 for(
int i=0; i<
_args.length; i++ ) {
446 if( !h0.
_eq(h1,dups) )
453 Oper op = opers.get(
this);
454 if( op!=
null )
return op;
458 for(
int i=0; i<
_args.length; i++ )
HMType _fresh(VStack vstk, HashMap< HMVar, HMVar > vars, HashMap< Oper, Oper > opers)
boolean tset(int b0, int b1)
boolean occurs_in(VStack vstk, VBitSet dups)
void get_ids(VStack vstk, Worklist work)
abstract HMType union(HMType t, Worklist work)
Oper(String name, HMType... args)
an implementation of language AA
final boolean eq(HMType v)
final void add_neighbors(Worklist work)
final boolean all_kids_ready()
Let(String arg0, Syntax body, Syntax use)
Lambda(String arg0, Syntax body)
SB _str(SB sb, VBitSet dups, boolean debug)
HMType fresh(VStack vstk)
abstract boolean progress()
SB str(SB sb, VBitSet dups, TypeMem mem, boolean debug)
void get_ids(VStack vstk, Worklist work)
void get_ids(VStack vstk, Worklist work)
void addAll(Ary<? extends Syntax > ss)
static final TypeInt INT64
abstract HMType hm(Worklist work)
static final HashMap< String, HMType > ENV
static Oper fun(HMType... args)
void get_ids(VStack vstk, Worklist work)
HMType union(HMType that, Worklist work)
Tight/tiny StringBuilder wrapper.
boolean _eq(HMType v, BitSetSparse dups)
void get_ids(VStack vstk, Worklist work)
boolean check_progress(Worklist work)
boolean _eq(HMType v, BitSetSparse dups)
abstract boolean _eq(HMType v, BitSetSparse dups)
boolean occurs_in(HMType[] args, VBitSet dups)
final void add_work(Worklist work)
SB _str(SB sb, VBitSet dups, boolean debug)
HMType _fresh(VStack vstk, HashMap< HMVar, HMVar > vars, HashMap< Oper, Oper > opers)
abstract SB _str(SB sb, VBitSet vbs, boolean debug)
final HashSet< Syntax > _work
abstract HMType _fresh(VStack vstk, HashMap< HMVar, HMVar > vars, HashMap< Oper, Oper > opers)
VStack(VStack par, HMVar nongen)
abstract void get_ids(VStack vstk, Worklist work)
Iterator< HMVar > iterator()
static HMType hm(Syntax prog)
static final TypeMemPtr STRPTR
static final HashMap< HMVar, HMVar > EQS
static final TypeInt BOOL
boolean occurs_in_type(HMType v, VBitSet dups)
Apply(Syntax fun, Syntax arg)
static final TypeFlt FLT64