Posted by Tiago Cogumbreiro on Wed 4 Feb 23:48 (modification of post by Tiago Cogumbreiro view diff)
report abuse | download | new post
- import qualified Data.Map as Map
- import Data.Maybe
- data Type = UnitType
- | TypeVar String
- | ArrowType Type Type
- -- Pretty print types
- show UnitType = "Unit"
- -- Expressions
- data Expr = Unit
- | Var String
- | App Expr Expr
- | Fun String Expr
- type TypeEnv = Map.Map String Type
- -- pretty print values
- show Unit = "unit"
- -- | Functions to generate fresh type-variables:
- genTypeVars = map TypeVar . genNames
- -- | The result type
- data Constraint = Eq Type Type
- -- Make it print'able
- -- | The algorithm to infer types.
- infer:: TypeEnv -- ^ the environment
- -> Expr -- ^ the expression whose type we want to infer
- -> Type -- ^ the type of the expression
- -> [Type] -- ^ the generator of fresh type-variables
- -> [Constraint] -- ^ a set of constraints
- infer type_env (App m n) t (a:vars) = e1_result ++ e2_result
- where
- e2_result = infer type_env n a vars
- e1_result = infer type_env m (ArrowType a t) new_vars
- new_vars = genTypeVars new_name
- where exp_result = infer (Map.insert x a type_env) m b vars
- -- | Generates the constraints for certain type
- inferTypes :: TypeEnv -> Expr -> [Constraint]
- where
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.