on d2bc280 help sign in
elk/elm/src/Types/Types.hs
117 lines 3.3 kB history raw
2module Types.Types where
4import Context
5import Data.Char (isDigit)
6import Data.List (intercalate,isPrefixOf)
7import qualified Data.Set as Set
8import qualified Data.Map as Map
10type X = Int
12data Type = LambdaT Type Type
13 | VarT X
14 | ADT String [Type]
15 | EmptyRecord
16 | RecordT (Map.Map String [Type]) Type
17 | Super (Set.Set Type)
18 deriving (Eq, Ord)
20data Scheme = Forall [X] [Context Constraint] Type deriving (Eq, Ord)
22data Constraint = Type :=: Type
23 | Type :<: Type
24 | X :<<: Scheme
25 deriving (Eq, Ord, Show)
27recordT :: [(String,Type)] -> Map.Map String [Type]
28recordT fields =
29 foldl (\r (x,t) -> Map.insertWith (++) x [t] r) Map.empty fields
31tipe t = ADT t []
33int = tipe "Int"
34float = tipe "Float"
35number = Super (Set.fromList [ int, float ])
37char = tipe "Char"
38bool = tipe "Bool"
40string = listOf char -- tipe "String"
41text = tipe "Text"
43time = float --tipe "Time"
44date = tipe "Date"
45month = tipe "Month"
46day = tipe "Day"
48element = tipe "Element"
49direction = tipe "Direction"
50form = tipe "Form"
51line = tipe "Line"
52shape = tipe "Shape"
53color = tipe "Color"
54position = tipe "Position"
55location = tipe "Location"
57listOf t = ADT "List" [t]
58signalOf t = ADT "Signal" [t]
59tupleOf ts = ADT ("Tuple" ++ show (length ts)) ts
60maybeOf t = ADT "Maybe" [t]
61pairOf t = tupleOf [t,t]
62point = pairOf int
63appendable t = Super (Set.fromList [ string, text, listOf t ])
64comparable = Super (Set.fromList [ int, float, char, string, time, date ])
66jsBool = tipe "JSBool"
67jsNumber = tipe "JSNumber"
68jsString = tipe "JSString"
69jsElement = tipe "JSElement"
70jsArray t = ADT "JSArray" [t]
71jsTuple ts = ADT ("JSTuple" ++ show (length ts)) ts
72jsonValue = tipe "JsonValue"
73jsonObject = tipe "JsonObject"
75infixr ==>
76t1 ==> t2 = LambdaT t1 t2
78infix 8 -:
79name -: tipe = (,) name $ Forall [] [] tipe
81hasType t = map (-: t)
83parens = ("("++) . (++")")
85instance Show Type where
86 show t =
87 case t of
88 { LambdaT t1@(LambdaT _ _) t2 -> parens (show t1) ++ " -> " ++ show t2
89 ; LambdaT t1 t2 -> show t1 ++ " -> " ++ show t2
90 ; VarT x -> 't' : show x
91 ; ADT "List" [ADT "Char" []] -> "String"
92 ; ADT "List" [tipe] -> "[" ++ show tipe ++ "]"
93 ; ADT name cs ->
94 if isTupleString name
95 then parens . intercalate "," $ map show cs
96 else case cs of
97 [] -> name
98 _ -> parens $ name ++ " " ++ unwords (map show cs)
99 ; Super ts -> "{" ++ (intercalate "," . map show $ Set.toList ts) ++ "}"
100 ; EmptyRecord -> "{}"
101 ; RecordT fs t ->
102 start ++ intercalate ", " (concatMap fields $ Map.toList fs) ++ " }"
103 where field n s = n ++ " :: " ++ show s
104 fields (n,ss) = map (field n) ss
105 start = case t of
106 EmptyRecord -> "{ "
107 _ -> "{ " ++ show t ++ " | "
108 }
110instance Show Scheme where
111 show (Forall [] [] t) = show t
112 show (Forall xs cs t) =
113 concat [ "Forall ", show xs
114 , concatMap (("\n "++) . show) cs
115 , "\n ", parens (show t) ]
117isTupleString str = "Tuple" `isPrefixOf` str && all isDigit (drop 5 str)