The user is able to use lambda calculator and type inference machine in the context of simple strictly typed lambda calculus.
The task is to be able to derive a type of any given expression, or raise an error if it is impossible. The algorithm return a list of tuples, where each variable has a type.
Compile the .hs files in type_inference directory:
GHCi> :load environment.hs type_inference.hs
GHCI> import TypeInferenceUse cases:
GHCi> let Right pp = principalPair (Var "x") in pp
(Env [("x",TVar "a")],TVar "a")GHCi> let Right pp = principalPair (Var "f" :@ Var "x") in pp
(Env [("f",TVar "a" :-> TVar "b"),("x",TVar "a")],TVar "b")GHCi> let Right pp = principalPair (Lam "x" $ Lam "y" $ Var "y") in pp
(Env [],TVar "a" :-> (TVar "b" :-> TVar "b"))GHCi> let Left err = principalPair (Var "x" :@ Var "x") in err
"Can't unify (TVar \"a\") with (TVar \"a\" :-> TVar \"b\")!"The idea is to be able to compare different expressions. Two expressions are considered equal (beta equivalent) if they could be reduced to the same result. Reductions are independent of the order.
Compile the .hs files in lambda_calculator directory:
GHCi> :load environment.hs calculator.hs combinators.hs
GHCI> import Combinators
GHCI> import MainUse cases:
GHCI> fac :@ three `betaEq` six
True
GHCI> fac :@ three `betaEq` seven
False