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<>();
27 public static T2 hm( String sprog ) {
59 int cnt_syns = prog.
prep_tree(
null,
null,work);
60 int init_T2s =
T2.
CNT;
62 int cnt=0, DEBUG_CNT=-1;
63 while( work.
len()>0 ) {
68 System.out.println(
"break here");
91 private static byte[]
BUF;
97 if(
skipWS() != -1 )
throw unimpl(
"Junk at end of program: "+
new String(
BUF,
X,
BUF.length-
X));
101 if(
skipWS()==-1 )
return null;
114 String arg0 =
id(), arg1=
null;
135 private static String
id() {
144 sum = sum*10+
BUF[
X++]-
'0';
147 float f = (float)sum;
148 f = f + (
BUF[
X++]-
'0')/10.0f;
153 while(
X<
BUF.length &&
BUF[
X]!=
'"' )
X++;
160 private static boolean isWS (
byte c) {
return c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r'; }
161 private static boolean isDigit (
byte c) {
return '0' <= c && c <=
'9'; }
162 private static boolean isAlpha0(
byte c) {
return (
'a'<=c && c <=
'z') || (
'A'<=c && c <=
'Z') || (c==
'_') || (c==
'*') || (c==
'='); }
163 private static boolean isAlpha1(
byte c) {
return isAlpha0(c) || (
'0'<=c && c <=
'9') || (c==
'/'); }
164 private static <T> T
require(
char c, T t) {
if(
skipWS()!=c )
throw unimpl(
"Missing '"+c+
"'");
X++;
return t; }
167 for(
int i=0; i<s.length(); i++ )
168 if(
X+i >=
BUF.length ||
BUF[
X+i]!=s.charAt(i) )
169 throw unimpl(
"Missing '"+s+
"'");
178 private final HashSet<Syntax>
_work =
new HashSet<>();
200 @NotNull @Override
public Iterator<T2>
iterator() {
return new Iter(); }
201 private class Iter implements Iterator<T2> {
216 return t==
_t ? t : (
_t=t);
239 return work.
has(
this) || !
hm(
null);
249 return p2(sb.
ii(1),dups).
di(1);
262 @Override
T2 lookup(String name) {
throw unimpl(
"should not reach here"); }
284 throw new RuntimeException(
"Parse error, "+
_name+
" is undefined");
287 @Override
T2 lookup(String name) {
throw unimpl(
"should not reach here"); }
298 syn.prep_lookup_deps(
this);
323 return old.
unify(fun,work);
367 return old.
unify(fun,work);
441 @Override
SB p1(
SB sb) {
return sb.
p(
"(...)"); }
458 if( work==
null )
return true;
460 for(
int i=0; i<
_args.length; i++ )
464 tfun.
unify(nfun,work);
469 throw new RuntimeException(
"Mismatched argument lengths");
471 boolean progress =
false;
472 for(
int i=0; i<
_args.length; i++ ) {
474 if( progress && work==
null )
485 for(
Syntax arg :
_args ) cnt += arg.prep_tree(
this,nongen,work);
492 for(
Syntax arg :
_args )
if( !arg.more_work(work) )
return false;
546 if( u==
null )
return this;
547 if( u.
no_uf() )
return u;
558 return u==uu ? uu : (
_args[i]=uu);
565 if(
this==that )
return false;
566 if( work==
null )
return true;
568 if(
_deps !=
null ) {
571 if( that._deps==
null && that.is_leaf() ) that._deps =
_deps;
573 for(
Ident dep :
_deps ) that.push_update(dep);
585 static private final HashMap<Long,T2>
DUPS =
new HashMap<>();
587 if(
this==that )
return false;
588 assert
DUPS.isEmpty();
589 boolean progress =
_unify(that,work);
599 if(
this==that )
return false;
603 if(
is_leaf() )
return union(that,work);
608 throw new RuntimeException(
"Cannot unify "+
this+
" and "+that);
611 throw new RuntimeException(
"Cannot unify "+
this+
" and "+that);
616 assert rez==
null || rez==that;
617 if( rez!=
null )
return false;
621 boolean progress=
false;
622 for(
int i=0; i<
_args.length; i++ ) {
624 if( progress && work!=
null )
return true;
633 if( work==
null )
return true;
636 return union(that,work);
640 if( con==that.
_con )
return false;
641 if( work!=
null ) that.
_con = con;
651 static private final HashMap<T2,T2>
VARS =
new HashMap<>();
653 assert
VARS.isEmpty() &&
DUPS.isEmpty();
658 throw unimpl(
"busted, made T2s but just testing");
665 T2 prior =
VARS.get(
this);
683 throw new RuntimeException(
"Cannot unify "+
this+
" and "+that);
686 boolean progress =
vput(that,
false);
687 for(
int i=0; i<
_args.length; i++ ) {
689 if( progress && work==
null )
return true;
694 private boolean vput(
T2 that,
boolean progress) {
VARS.put(
this,that);
return progress; }
700 if( rez!=
null )
return rez;
710 for(
int i=0; i<
_args.length; i++ )
718 if( syn==
null )
return false;
719 assert
ODUPS.isEmpty();
725 assert
ODUPS.isEmpty();
731 for( ; syn!=
null; syn=syn.
_par )
739 if( x==
this )
return true;
742 for(
int i=0; i<x.
_args.length; i++ )
749 if( syn==
null )
return false;
750 assert
ODUPS.isEmpty();
756 for(
T2 t2 : nongen )
763 static private final HashMap<T2,T2>
CDUPS =
new HashMap<>();
765 assert
CDUPS.isEmpty();
772 if(
this==t )
return true;
784 for(
int i=0; i<
_args.length; i++ )
803 for(
int i=0; i<
_args.length; i++ )
814 for(
int i=0; i<
_args.length; i++ )
829 t._get_dups(visit,dups);
841 boolean dup = dups.get(
_uid);
842 if( dup ) sb.
p(
'$').
p(
_uid);
843 if( visit.
tset(
_uid) && dup )
return sb;
848 for(
int i=0; i<
_args.length-1; i++ )
866 boolean dup = dups.get(
_uid);
867 if( dup ) sb.
p(
'$').
p(
_uid);
868 if( visit.
tset(
_uid) && dup )
return sb;
872 for(
int i=0; i<
_args.length-1; i++ )
873 args(i).
_p(sb,visit,dups).
p(
" ");
874 return args(
_args.length-1).
_p(sb.
p(
"-> "),visit,dups).
p(
" }");
878 for(
int i=0; i<
_args.length; i++ )
args(i).
_p(sb,visit,dups).
p(
" ");
SB _p(SB sb, VBitSet visit, VBitSet dups)
Apply(Syntax fun, Syntax... args)
static boolean DEBUG_LEAKS
abstract T2 lookup(String name)
T2(@NotNull String name, Type con, T2 @NotNull ... args)
boolean more_work(Worklist work)
static final VBitSet UPDATE_VISIT
static Syntax parse(String s)
E push(E e)
Add element in amortized constant time.
boolean _fresh_unify(T2 that, VStack nongen, Worklist work)
void prep_lookup_deps(Ident id)
static boolean eq(String s0, String s1)
SB str(SB sb, VBitSet visit, VBitSet dups)
static SB str(SB sb, VBitSet visit, T2 t, VBitSet dups)
static final HashMap< T2, T2 > CDUPS
boolean hm(Worklist work)
boolean hm(Worklist work)
static T2 make_base(Type con)
boolean more_work(Worklist work)
VBitSet get_dups(VBitSet dups)
boolean union(T2 that, Worklist work)
static final VBitSet ODUPS
void add_kids(Worklist work)
SB p2(SB sb, VBitSet dups)
static final HashMap< Long, T2 > DUPS
int prep_tree(Syntax par, VStack nongen, Worklist work)
boolean more_work(Worklist work)
an implementation of language AA
final void prep_tree_impl(Syntax par, VStack nongen, Worklist work, T2 t)
static TypeInt con(long con)
boolean nongen_in(VStack syn)
void add_kids(Worklist work)
boolean more_work(Worklist work)
boolean _occurs_in(Syntax syn)
boolean more_work(Worklist work)
static void require(String s)
boolean unify_base(T2 that, Worklist work)
void add_deps_work_impl(Worklist work)
boolean hm(Worklist work)
static RuntimeException unimpl()
void add_kids(Worklist work)
abstract void prep_lookup_deps(Ident id)
abstract SB p2(SB sb, VBitSet dups)
void add_occurs(Worklist work)
boolean hm(Worklist work)
int prep_tree(Syntax par, VStack nongen, Worklist work)
static final TypeInt INT64
boolean fresh_unify(T2 that, VStack nongen, Worklist work)
static boolean isAlpha0(byte c)
static Type con(double con)
boolean hm(Worklist work)
static T2 prim(String name, T2... args)
SB p2(SB sb, VBitSet dups)
void add_deps_work(Worklist work)
static boolean isAlpha1(byte c)
abstract boolean more_work(Worklist work)
SB p2(SB sb, VBitSet dups)
boolean more_work(Worklist work)
boolean push_update(Ident a)
static T2 make_fun(T2... args)
static T2 hm(String sprog)
Lambda(String arg0, Syntax body)
static< T > T require(char c, T t)
boolean _occurs_in_type(T2 x)
abstract void add_kids(Worklist work)
void add_kids(Worklist work)
void add_occurs(Worklist work)
Lambda2(String arg0, String arg1, Syntax body)
static TypeStr con(String con)
boolean cycle_equals(T2 t)
final HashSet< Syntax > _work
boolean _nongen_in(VStack nongen)
void add_occurs(Worklist work)
static boolean isDigit(byte c)
Tight/tiny StringBuilder wrapper.
boolean occurs_in(Syntax syn)
void push_update_impl(Ident a)
int prep_tree(Syntax par, VStack nongen, Worklist work)
static final HashMap< T2, T2 > VARS
void add_kids(Worklist work)
an implementation of language AA
static boolean isWS(byte c)
static final HashMap< String, T2 > PRIMS
int prep_tree(Syntax par, VStack nongen, Worklist work)
boolean fresh_base(T2 that, Worklist work)
void prep_lookup_deps(Ident id)
Let(String arg0, Syntax def, Syntax body)
void add_occurs(Worklist work)
Iterator< T2 > iterator()
SB p2(SB sb, VBitSet dups)
void prep_lookup_deps(Ident id)
SB p2(SB sb, VBitSet dups)
boolean _cycle_equals(T2 t)
boolean occurs_in_type(T2 x)
SB p2(SB sb, VBitSet dups)
boolean unify(T2 that, Worklist work)
void addAll(Ary<? extends Syntax > ss)
boolean vput(T2 that, boolean progress)
int prep_tree(Syntax par, VStack nongen, Worklist work)
boolean hm(Worklist work)
boolean _unify(T2 that, Worklist work)
void prep_lookup_deps(Ident id)
abstract boolean hm(Worklist work)
void add_kids(Worklist work)
VStack(VStack par, T2 nongen)
void prep_lookup_deps(Ident id)
static final TypeMemPtr STRPTR
VBitSet _get_dups(VBitSet visit, VBitSet dups)
void add_occurs(Worklist work)
abstract int prep_tree(Syntax par, VStack nongen, Worklist work)
static final TypeInt BOOL
final boolean more_work_impl(Worklist work)
int prep_tree(Syntax par, VStack nongen, Worklist work)
final SB p0(SB sb, VBitSet dups)
static final TypeFlt FLT64
void prep_lookup_deps(Ident id)