-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTyping.hs
67 lines (54 loc) · 1.73 KB
/
Typing.hs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{-# LANGUAGE DeriveFunctor #-}
module Typing where
import Meta (ASTMeta, getMeta)
type FTid = Int
data FreeType m = FT FTid m
deriving (Show, Read, Functor)
data PolyType m = Poly (FreeType m) (PolyType m) m | Mono (MonoType m) m
deriving (Show, Read, Functor)
data MonoType m = Func [MonoType m] (MonoType m) m
| Pair (MonoType m) (MonoType m) m
| List (MonoType m) m
| Free (FreeType m) m
| Int m
| Bool m
| Void m
deriving (Show, Read, Functor)
-- Returns whether Void is used in an active position (ie non-return type)
usingVoid :: MonoType m -> Bool
usingVoid (Func args _ _) = any usingVoid args
usingVoid (Pair t1 t2 _) = usingVoid t1 || usingVoid t2
usingVoid (List t _) = usingVoid t
usingVoid (Void _) = True
usingVoid _ = False
isPolymorphic :: PolyType a -> Bool
isPolymorphic (Poly _ _ _) = True
isPolymorphic (Mono _ _) = False
instance Eq (FreeType m) where
(==) (FT x _) (FT y _) = x == y
instance Eq (PolyType m) where
(==) (Poly ft1 pt1 _) (Poly ft2 pt2 _) = ft1 == ft2 && pt1 == pt2
(==) (Mono mt1 _) (Mono mt2 _) = mt1 == mt2
(==) _ _ = False
instance Eq (MonoType m) where
(==) (Func xs xr _) (Func ys yr _) = xs == ys && xr == yr
(==) (Pair xx xy _) (Pair yx yy _) = xx == yx && xy == yy
(==) (List x _) (List y _) = x == y
(==) (Free x _) (Free y _) = x == y
(==) (Int _) (Int _) = True
(==) (Bool _) (Bool _) = True
(==) (Void _) (Void _) = True
(==) _ _ = False
instance ASTMeta FreeType where
getMeta (FT _ m) = m
instance ASTMeta PolyType where
getMeta (Poly _ _ m) = m
getMeta (Mono _ m) = m
instance ASTMeta MonoType where
getMeta (Func _ _ m) = m
getMeta (Pair _ _ m) = m
getMeta (List _ m) = m
getMeta (Free _ m) = m
getMeta (Int m) = m
getMeta (Bool m) = m
getMeta (Void m) = m