| otherwise = Right $ 'Map'.singleton tv t1
unify t1=:(IdType tv) t2 = unify t2 t1
unify (ta1->>ta2) (tb1->>tb2) = unify ta1 tb1 >>= \s1->
- unify ta2 tb2 >>= \s2->
- Right $ compose s1 s2
+ unify (subst s1 ta2) (subst s1 tb2) >>= \s2->
+ Right $ compose s2 s1
unify (TupleType (ta1,ta2)) (TupleType (tb1,tb2)) = unify ta1 tb1 >>= \s1->
unify ta2 tb2 >>= \s2->
- Right $ compose s1 s2
+ Right $ compose s2 s1
unify (ListType t1) (ListType t2) = unify t1 t2
unify (FuncType t1) (FuncType t2) = unify t1 t2
unify t1 t2 | t1 == t2 = Right zero
let given = t1 ->> t2 ->> tv in
op2Type op >>= \expected ->
lift (unify expected given) >>= \s3 ->
+ applySubst s3 >>|
pure ((compose s3 $ compose s2 s1), subst s3 tv, Op2Expr p e1_ op e2_)
Op1Expr p op e1 =
infer e1 >>= \(s1, t1, e1_) ->
+ applySubst s1 >>|
fresh >>= \tv ->
let given = t1 ->> tv in
op1Type op >>= \expected ->
lift (unify expected given) >>= \s2 ->
+ applySubst s2 >>|
pure (compose s2 s1, subst s2 tv, Op1Expr p op e1)
- EmptyListExpr _ = (\tv->(zero,tv,e)) <$> fresh
+ EmptyListExpr _ = (\tv->(zero,ListType tv,e)) <$> fresh
TupleExpr p (e1, e2) =
infer e1 >>= \(s1, t1, e1_) ->
+ applySubst s1 >>|
infer e2 >>= \(s2, t2, e2_) ->
+ applySubst s2 >>|
pure (compose s2 s1, TupleType (t1,t2), TupleExpr p (e1_,e2_))
LambdaExpr _ _ _ = liftT $ Left $ Error "PANIC: lambdas should be Unfolded"
lookup f >>= \expected ->
let accST = (\(s,ts,es) e->infer e >>= \(s_,et,e_)-> pure (compose s_ s,ts++[et],es++[e_])) in
foldM accST (zero,[],[]) args >>= \(s1, argTs, args_)->
+ applySubst s1 >>|
(case f of
"print" = case head argTs of
IntType = pure "1printint"
//TODO: Fieldselectors!
_ = (let given = foldr (->>) tv argTs in
lift (unify expected given) >>= \s2->
+ applySubst s2 >>|
let fReturnType = subst s2 tv in
foldM foldFieldSelectors fReturnType fs >>= \returnType ->
pure (compose s2 s1, returnType, FunExpr p newF args_ fs))