-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypes.hs
More file actions
38 lines (30 loc) · 1.13 KB
/
Types.hs
File metadata and controls
38 lines (30 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module Types where
import Control.DeepSeq (NFData(rnf))
import Data.List (intercalate)
data Type = TInt
| TRef Type
| TFun [Type] Type
| TReg Type -- Regular type, where the argument is a parent reference
parens :: String -> String
parens a = "(" ++ a ++ ")"
showFun :: Int -> [Type] -> Type -> String
showFun n dom cod | null dom = "() -> " ++ show' n cod
showFun n dom cod = "(" ++ intercalate ", " (map (show' n) dom) ++ ") -> " ++ show' n cod
instance Eq Type where
TInt == TInt = True
TRef t1 == TRef t2 = t1 == t2
TFun d1 c1 == TFun d2 c2 = c1 == c2 && all id (zipWith (==) d1 d2)
--TReg t1 == TReg t2 = ???
_ == _ = False
instance Show Type where
show = show' 2
instance NFData Type where
rnf TInt = ()
rnf (TRef t) = rnf t
rnf (TFun ts t) = rnf $ t : ts
rnf (TReg t) = () -- Do not force evaluation of t, that would cause an infinite loop
show' n TInt = "int"
show' n (TRef t) = '&' : show' n t
show' n (TFun dom cod) = showFun n dom cod
show' 0 (TReg _) = "..."
show' n (TReg t) = show' (n-1) t