pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Haskell pastebin - collaborative debugging tool View Help


Posted by Tiago Cogumbreiro on Wed 4 Feb 23:48 (modification of post by Tiago Cogumbreiro view diff)
report abuse | download | new post

  1. import qualified Data.Map as Map
  2. import Data.Maybe
  3.  
  4. data Type = UnitType
  5.           | TypeVar String
  6.           | ArrowType Type Type
  7.  
  8. -- Pretty print types
  9.  
  10. instance Show Type where
  11.     show UnitType  = "Unit"
  12.     show (TypeVar x) = x
  13.     show (ArrowType t1 t2) = "(" ++ (show t1) ++ " -> " ++ (show t2) ++ ")"
  14.  
  15. -- Expressions
  16.  
  17. data Expr = Unit
  18.           | Var String
  19.           | App Expr Expr
  20.           | Fun String Expr
  21.  
  22. type TypeAssign = (String, Type)
  23.  
  24. type TypeEnv = Map.Map String Type
  25.  
  26. -- pretty print values
  27.  
  28. instance Show Expr where
  29.     show Unit = "unit"
  30.     show (Var x) = x
  31.     show (App e1 e2) = "(" ++ (show e1) ++ " " ++ (show e2) ++ ")"
  32.     show (Fun x e) = "(fun " ++ x ++ " = " ++ (show e) ++ ")"
  33.  
  34. -- | Functions to generate fresh type-variables:
  35.  
  36. genNames name = map (\x -> name ++ (show x)) naturals
  37.     where naturals = iterate (+ 1) 1
  38.  
  39. genTypeVars = map TypeVar . genNames
  40.  
  41. -- | The result type
  42. data Constraint = Eq Type Type
  43.  
  44. -- Make it print'able
  45. instance Show Constraint where
  46.     show (Eq t1 t2) = (show t1) ++ "=" ++ (show t2)
  47.  
  48. -- |  The algorithm to infer types.
  49. infer:: TypeEnv      -- ^ the environment
  50.      -> Expr         -- ^ the expression whose type we want to infer
  51.      -> Type         -- ^ the type of the expression
  52.      -> [Type]       -- ^ the generator of fresh type-variables
  53.      -> [Constraint] -- ^ a set of constraints
  54.  
  55. infer _ Unit t vars = [Eq t UnitType]
  56.  
  57. infer type_env (Var x) t vars = [(Eq ret_type t)]
  58.     where ret_type = fromJust (Map.lookup x type_env)
  59.  
  60. infer type_env (App m n) t (a:vars) = e1_result ++ e2_result
  61.     where
  62.       e2_result = infer type_env n a vars
  63.       e1_result = infer type_env m (ArrowType a t) new_vars
  64.       new_vars = genTypeVars new_name
  65.       new_name = (show a) ++ "_"
  66.  
  67. infer type_env (Fun x m) t (a:b:vars) = (Eq t (ArrowType a b)):exp_result
  68.     where exp_result = infer (Map.insert x a type_env) m b vars
  69.  
  70. -- | Generates the constraints for certain type
  71. inferTypes :: TypeEnv -> Expr -> [Constraint]
  72. inferTypes type_env exp = infer type_env exp a vars
  73.     where
  74.       a = head (genTypeVars "a")
  75.       vars = tail (genTypeVars "a")

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.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post