Go to the documentation of this file. 1 package com.cliffc.aa.HM;
5 import org.jetbrains.annotations.
NotNull;
24 static final HashMap<String,T2>
PRIMS =
new HashMap<>();
56 int cnt_syns = prog.
prep_tree(
null,
null,work);
57 int init_T2s =
T2.
CNT;
59 int cnt=0, DEBUG_CNT=-1;
60 while( work.
len()>0 ) {
65 System.out.println(
"break here");
89 private final HashSet<Syntax>
_work =
new HashSet<>();
110 @NotNull @Override
public Iterator<T2>
iterator() {
return new Iter(); }
111 private class Iter implements Iterator<T2> {
125 return t==
_t ? t : (
_t=t);
148 return work.
has(
this) || !
hm(
null);
158 return p2(sb.
ii(1),dups).
di(1);
171 @Override
T2 lookup(String name) {
throw unimpl(
"should not reach here"); }
193 throw new RuntimeException(
"Parse error, "+
_name+
" is undefined");
196 @Override
T2 lookup(String name) {
throw unimpl(
"should not reach here"); }
207 syn.prep_lookup_deps(
this);
232 return old.
unify(fun,work);
276 return old.
unify(fun,work);
350 @Override
SB p1(
SB sb) {
return sb.
p(
"(...)"); }
367 if( work==
null )
return true;
369 for(
int i=0; i<
_args.length; i++ )
373 tfun.
unify(nfun,work);
378 throw new RuntimeException(
"Mismatched argument lengths");
380 boolean progress =
false;
381 for(
int i=0; i<
_args.length; i++ ) {
383 if( progress && work==
null )
394 for(
Syntax arg :
_args ) cnt += arg.prep_tree(
this,nongen,work);
401 for(
Syntax arg :
_args )
if( !arg.more_work(work) )
return false;
412 public static class T2 {
455 if( u==
null )
return this;
456 if( u.
no_uf() )
return u;
467 return u==uu ? uu : (
_args[i]=uu);
474 if(
this==that )
return false;
475 if( work==
null )
return true;
477 if(
_deps !=
null ) {
480 if( that._deps==
null && that.is_leaf() ) that._deps =
_deps;
482 for(
Ident dep :
_deps ) that.push_update(dep);
494 static private final HashMap<Long,T2>
DUPS =
new HashMap<>();
496 if(
this==that )
return false;
497 assert
DUPS.isEmpty();
498 boolean progress =
_unify(that,work);
508 if(
this==that )
return false;
512 if(
is_leaf() )
return union(that,work);
517 throw new RuntimeException(
"Cannot unify "+
this+
" and "+that);
520 throw new RuntimeException(
"Cannot unify "+
this+
" and "+that);
525 assert rez==
null || rez==that;
526 if( rez!=
null )
return false;
530 boolean progress=
false;
531 for(
int i=0; i<
_args.length; i++ ) {
533 if( progress && work!=
null )
return true;
542 if( work==
null )
return true;
545 return union(that,work);
549 if( con==that.
_con )
return false;
550 if( work!=
null ) that.
_con = con;
560 static private final HashMap<T2,T2>
VARS =
new HashMap<>();
562 assert
VARS.isEmpty() &&
DUPS.isEmpty();
567 throw unimpl(
"busted, made T2s but just testing");
574 T2 prior =
VARS.get(
this);
592 throw new RuntimeException(
"Cannot unify "+
this+
" and "+that);
595 boolean progress =
vput(that,
false);
596 for(
int i=0; i<
_args.length; i++ ) {
598 if( progress && work==
null )
return true;
603 private boolean vput(
T2 that,
boolean progress) {
VARS.put(
this,that);
return progress; }
609 if( rez!=
null )
return rez;
619 for(
int i=0; i<
_args.length; i++ )
627 if( syn==
null )
return false;
628 assert
ODUPS.isEmpty();
634 assert
ODUPS.isEmpty();
640 for( ; syn!=
null; syn=syn.
_par )
648 if( x==
this )
return true;
651 for(
int i=0; i<x.
_args.length; i++ )
658 if( syn==
null )
return false;
659 assert
ODUPS.isEmpty();
665 for(
T2 t2 : nongen )
672 static private final HashMap<T2,T2>
CDUPS =
new HashMap<>();
674 assert
CDUPS.isEmpty();
681 if(
this==t )
return true;
693 for(
int i=0; i<
_args.length; i++ )
712 for(
int i=0; i<
_args.length; i++ )
723 for(
int i=0; i<
_args.length; i++ )
738 t._get_dups(visit,dups);
750 boolean dup = dups.get(
_uid);
751 if( dup ) sb.
p(
'$').
p(
_uid);
752 if( visit.
tset(
_uid) && dup )
return sb;
757 for(
int i=0; i<
_args.length-1; i++ )
775 boolean dup = dups.get(
_uid);
776 if( dup ) sb.
p(
'$').
p(
_uid);
777 if( visit.
tset(
_uid) && dup )
return sb;
781 for(
int i=0; i<
_args.length-1; i++ )
782 args(i).
_p(sb,visit,dups).
p(
" ");
783 return args(
_args.length-1).
_p(sb.
p(
"-> "),visit,dups).
p(
" }");
787 for(
int i=0; i<
_args.length; i++ )
args(i).
_p(sb,visit,dups).
p(
" ");
void add_kids(Worklist work)
static final HashMap< T2, T2 > CDUPS
static final HashMap< T2, T2 > VARS
boolean _occurs_in_type(T2 x)
Let(String arg0, Syntax def, Syntax body)
VBitSet _get_dups(VBitSet visit, VBitSet dups)
void add_occurs(Worklist work)
VStack(VStack par, T2 nongen)
void add_kids(Worklist work)
boolean more_work(Worklist work)
boolean more_work(Worklist work)
static final VBitSet UPDATE_VISIT
static T2 make_base(Type con)
SB p2(SB sb, VBitSet dups)
void prep_lookup_deps(Ident id)
static final HashMap< String, T2 > PRIMS
static boolean eq(String s0, String s1)
boolean _fresh_unify(T2 that, VStack nongen, Worklist work)
boolean nongen_in(VStack syn)
static final HashMap< Long, T2 > DUPS
void add_kids(Worklist work)
boolean _unify(T2 that, Worklist work)
void prep_lookup_deps(Ident id)
SB str(SB sb, VBitSet visit, VBitSet dups)
SB p2(SB sb, VBitSet dups)
SB p2(SB sb, VBitSet dups)
an implementation of language AA
Lambda2(String arg0, String arg1, Syntax body)
void add_occurs(Worklist work)
abstract boolean hm(Worklist work)
boolean hm(Worklist work)
static SB str(SB sb, VBitSet visit, T2 t, VBitSet dups)
void add_kids(Worklist work)
void addAll(Ary<? extends Syntax > ss)
boolean hm(Worklist work)
boolean hm(Worklist work)
boolean _occurs_in(Syntax syn)
boolean _cycle_equals(T2 t)
abstract int prep_tree(Syntax par, VStack nongen, Worklist work)
static RuntimeException unimpl()
void add_occurs(Worklist work)
int prep_tree(Syntax par, VStack nongen, Worklist work)
int prep_tree(Syntax par, VStack nongen, Worklist work)
void add_deps_work(Worklist work)
Apply(Syntax fun, Syntax... args)
boolean occurs_in_type(T2 x)
static final TypeInt INT64
boolean more_work(Worklist work)
void prep_lookup_deps(Ident id)
VBitSet get_dups(VBitSet dups)
void push_update_impl(Ident a)
final SB p0(SB sb, VBitSet dups)
abstract T2 lookup(String name)
int prep_tree(Syntax par, VStack nongen, Worklist work)
boolean union(T2 that, Worklist work)
static boolean DEBUG_LEAKS
Lambda(String arg0, Syntax body)
boolean hm(Worklist work)
boolean push_update(Ident a)
boolean hm(Worklist work)
static T2 make_fun(T2... args)
boolean _nongen_in(VStack nongen)
Iterator< T2 > iterator()
void add_occurs(Worklist work)
int prep_tree(Syntax par, VStack nongen, Worklist work)
abstract void add_kids(Worklist work)
void add_deps_work_impl(Worklist work)
SB p2(SB sb, VBitSet dups)
void prep_lookup_deps(Ident id)
void prep_lookup_deps(Ident id)
boolean more_work(Worklist work)
void add_kids(Worklist work)
abstract SB p2(SB sb, VBitSet dups)
abstract void prep_lookup_deps(Ident id)
Tight/tiny StringBuilder wrapper.
abstract boolean more_work(Worklist work)
final HashSet< Syntax > _work
static T2 prim(String name, T2... args)
SB p2(SB sb, VBitSet dups)
boolean more_work(Worklist work)
an implementation of language AA
boolean hm(Worklist work)
static T2 hm(Syntax prog)
boolean cycle_equals(T2 t)
int prep_tree(Syntax par, VStack nongen, Worklist work)
void add_occurs(Worklist work)
SB _p(SB sb, VBitSet visit, VBitSet dups)
int prep_tree(Syntax par, VStack nongen, Worklist work)
boolean fresh_base(T2 that, Worklist work)
void add_kids(Worklist work)
void prep_lookup_deps(Ident id)
boolean fresh_unify(T2 that, VStack nongen, Worklist work)
boolean vput(T2 that, boolean progress)
static final VBitSet ODUPS
boolean occurs_in(Syntax syn)
SB p2(SB sb, VBitSet dups)
boolean more_work(Worklist work)
boolean unify(T2 that, Worklist work)
static final TypeMemPtr STRPTR
boolean unify_base(T2 that, Worklist work)
static final TypeInt BOOL
final boolean more_work_impl(Worklist work)
T2(@NotNull String name, Type con, T2 @NotNull ... args)
static final TypeFlt FLT64
final void prep_tree_impl(Syntax par, VStack nongen, Worklist work, T2 t)