Go to the documentation of this file. 1 package com.cliffc.aa.HM;
7 import java.util.Arrays;
8 import java.util.HashMap;
9 import java.util.HashSet;
25 HashMap<String,HMType> env =
new HashMap<>();
39 env.put(
"if/else3",
Oper.
fun(
bool, var3, var3,var3 ));
41 env.put(
"dec",
Oper.
fun(int64,int64));
43 env.put(
"*2" ,
Oper.
fun(int64, int64,int64 ));
44 env.put(
"==0",
Oper.
fun(int64,
bool));
47 env.put(
"str",
Oper.
fun(int64,strp));
49 env.put(
"factor",
Oper.
fun(flt64,
new Oper(
"pair",flt64,flt64)));
51 return prog.
hm(env,
new HashSet<>());
56 public static abstract class Syntax {
57 abstract HMType hm(HashMap<String,HMType> env, HashSet<HMVar> nongen);
63 @Override
HMType hm(HashMap<String,HMType> env, HashSet<HMVar> nongen) {
71 @Override
HMType hm(HashMap<String,HMType> env, HashSet<HMVar> nongen) {
74 throw new RuntimeException(
"Parse error, "+
_name+
" is undefined");
85 @Override
HMType hm(HashMap<String,HMType> env, HashSet<HMVar> nongen) {
102 @Override
HMType hm(HashMap<String,HMType> env, HashSet<HMVar> nongen) {
106 env.put(
_arg0,tnew0);
107 env.put(
_arg1,tnew1);
111 nongen.remove(tnew0);
112 nongen.remove(tnew1);
115 return Oper.
fun(tnew0,tnew1,trez);
124 @Override
HMType hm(HashMap<String,HMType> env, HashSet<HMVar> nongen) {
127 env.put(
_arg0,tndef);
130 nongen.remove(tndef);
143 @Override
HMType hm(HashMap<String,HMType> env, HashSet<HMVar> nongen) {
147 for(
int i=0; i<
_args.length; i++ )
148 targs[i] =
_args[i].
hm(env,nongen);
168 HashMap<HMType,HMType> vars =
new HashMap<>();
169 return _fresh(nongen,vars);
173 if( t2 instanceof
HMVar ) {
176 : vars.computeIfAbsent(t2, e ->
new HMVar(((
HMVar)t2)._t));
180 for(
int i=0; i<args.length; i++ )
199 if( y instanceof
Oper )
214 if(
_u!=
null && !debug )
return _u.
_str(sb,dups,debug);
218 if(
_u!=
null )
_u.
_str(sb.
p(
">>"),dups,debug);
224 if( u==
null )
return this;
225 if( u.
_u==
null )
return u;
227 while( u.
_u!=
null ) u = u.
_u;
234 if( that instanceof
HMVar ) that = that.
find();
235 if(
this==that )
return this;
237 throw new RuntimeException(
"recursive unification");
239 if( that instanceof
HMVar ) {
254 if(
_name.equals(
"->") ) {
256 for(
int i=0; i<
_args.length-1; i++ )
264 t._str(sb,dups,debug).
p(
',');
270 if( !(that instanceof
Oper) )
return that.
union(
this);
274 throw new RuntimeException(
"Cannot unify "+
this+
" and "+that);
275 for(
int i=0; i<
_args.length; i++ )
static HMType HM(Syntax prog)
abstract SB _str(SB sb, VBitSet vbs, boolean debug)
HMType _fresh(HashSet< HMVar > nongen, HashMap< HMType, HMType > vars)
an implementation of language AA
HMType hm(HashMap< String, HMType > env, HashSet< HMVar > nongen)
HMType hm(HashMap< String, HMType > env, HashSet< HMVar > nongen)
boolean occurs_in_type(HMType v)
SB str(SB sb, VBitSet dups, TypeMem mem, boolean debug)
SB _str(SB sb, VBitSet dups, boolean debug)
HMType hm(HashMap< String, HMType > env, HashSet< HMVar > nongen)
abstract HMType hm(HashMap< String, HMType > env, HashSet< HMVar > nongen)
static final TypeInt INT64
HMType hm(HashMap< String, HMType > env, HashSet< HMVar > nongen)
SB _str(SB sb, VBitSet dups, boolean debug)
Tight/tiny StringBuilder wrapper.
HMType hm(HashMap< String, HMType > env, HashSet< HMVar > nongen)
HMType hm(HashMap< String, HMType > env, HashSet< HMVar > nongen)
boolean occurs_in(HashSet< HMVar >nongen)
abstract HMType union(HMType t)
static Oper fun(HMType... args)
Oper(String name, HMType... args)
HMType fresh(HashSet< HMVar > nongen)
static final TypeMemPtr STRPTR
boolean occurs_in(HMType[] args)
HMType union(HMType that)
Let(String arg0, Syntax def, Syntax body)
Apply(Syntax fun, Syntax... args)
Lambda2(String arg0, String arg1, Syntax body)
static final TypeInt BOOL
static final TypeFlt FLT64
Lambda(String arg0, Syntax body)