m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/Typecheck.hs
blob: 6c62909a10bab2ebe92846a36a18f51f74409fe6 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
module Typecheck where

import Prelude hiding (lookup)
import Parser.AbsSchmim
import Data.Map.Lazy
import Control.Monad.Reader

type Context = Map Ident Type
type Typechecker a = ReaderT Context Maybe a

assert :: MonadPlus m => Bool -> m ()
assert True = return ()
assert False = mzero

typeof :: Exp -> Typechecker Type
typeof ETr = return TBool
typeof EFl = return TBool
typeof (ENum _) = return TInt
typeof (EVar x) = do
    mt <- asks $ lookup x
    case mt of 
        (Just t) -> return t
        Nothing -> lift Nothing
typeof (EAdd e1 e2) = do
    t1 <- typeof e1
    t2 <- typeof e2
    assert (t1 == TInt)
    assert (t2 == TInt)
    return TInt
typeof (ESub e1 e2) = do
    t1 <- typeof e1
    t2 <- typeof e2
    assert (t1 == TInt)
    assert (t2 == TInt)
    return TInt
typeof (EMul e1 e2) = do
    t1 <- typeof e1
    t2 <- typeof e2
    assert (t1 == TInt)
    assert (t2 == TInt)
    return TInt
typeof (EDiv e1 e2) = do
    t1 <- typeof e1
    t2 <- typeof e2
    assert (t1 == TInt)
    assert (t2 == TInt)
    return TInt
typeof (EAnd e1 e2) = do
    t1 <- typeof e1
    t2 <- typeof e2
    assert (t1 == TBool)
    assert (t2 == TBool)
    return TBool
typeof (EOr e1 e2) = do
    t1 <- typeof e1
    t2 <- typeof e2
    assert (t1 == TBool)
    assert (t2 == TBool)
    return TBool
typeof (ENot e) = do
    t <- typeof e
    assert (t == TBool)
    return TBool
typeof (EEq e1 e2) = do
    t1 <- typeof e1
    t2 <- typeof e2
    assert (t1 == TInt)
    assert (t2 == TInt)
    return TBool
typeof (ELeq e1 e2) = do
    t1 <- typeof e1
    t2 <- typeof e2
    assert (t1 == TInt)
    assert (t2 == TInt)
    return TBool
typeof (EGeq e1 e2) = do
    t1 <- typeof e1
    t2 <- typeof e2
    assert (t1 == TInt)
    assert (t2 == TInt)
    return TBool
typeof (ELess e1 e2) = do
    t1 <- typeof e1
    t2 <- typeof e2
    assert (t1 == TInt)
    assert (t2 == TInt)
    return TBool
typeof (EGrt e1 e2) = do
    t1 <- typeof e1
    t2 <- typeof e2
    assert (t1 == TInt)
    assert (t2 == TInt)
    return TBool
typeof (EIfte e1 e2 e3) = do
    t1 <- typeof e1
    t2 <- typeof e2
    t3 <- typeof e3
    assert (t1 == TBool)
    assert (t2 == t3)
    return t2
typeof (EAbs x t e) = do
    t' <- local (insert x t) $ typeof e
    return $ TFun t t'
typeof (EApp e []) = typeof e
typeof (EApp e1 [e2]) = do
    t1 <- typeof e1
    t2 <- typeof e2
    case t1 of
        (TFun t t')
            | t == t2 -> return t'
        _ -> lift Nothing
typeof (EApp e1 es) = do
    t1 <- typeof e1
    ts <- forM es typeof
    applyTypes t1 ts
    where
        applyTypes t [] = return t
        applyTypes (TFun t t') (t1:ts) = do
            assert $ t == t1
            applyTypes t' ts
typeof (EVrnt l e t@(TVrnt ls)) = do
    t1 <- typeof e
    (Labeled l' t2) <- lift $ lookupLabel l ls
    assert $ t1 == t2
    return t
    where
        lookupLabel l [] = Nothing
        lookupLabel l (lbl@(Labeled l1 t):ls)
            | l == l1 = Just lbl
            | otherwise = lookupLabel l ls
typeof (EVrnt l e _) = lift Nothing
typeof (ETpl es) = do
    ts <- forM es typeof
    return $ TTpl ts
typeof (EProj e n) = do
    t <- typeof e
    case t of
        (TTpl ts)
            | length ts >= fromIntegral n -> return $ ts !! (fromIntegral (n - 1))
        _ -> lift Nothing
typeof (EMtch e ms) = do
    t <- typeof e
    case t of
        (TVrnt ls) -> do
            assert $ (length ms == length ls)
            ts <- forM (zip ms ls) getMatchingType
            assert $ allSame ts
            return $ head ts
        _ -> lift Nothing
    where
        getMatchingType (Matching l1 x e, Labeled l2 t) = do
            assert $ l1 == l2
            local (insert x t) $ typeof e
        allSame [] = True
        allSame [_] = True
        allSame (x:y:zs)
            | x == y = allSame (y:zs)
            | otherwise = False
typeof (ENil t) = do
    case t of
        (TList _) -> return t
        _ -> lift Nothing
typeof (ECons e1 e2) = do
    t1 <- typeof e1
    t2 <- typeof e2
    case t2 of
        (TList t) -> do
            assert $ t == t1
            return t2
        _ -> lift Nothing
typeof (EHead e) = do
    t <- typeof e
    case t of
        (TList t') -> return t'
        _ -> lift Nothing
typeof (ETail e) = do
    t <- typeof e
    case t of
        (TList _) -> return t
        _ -> lift Nothing
typeof (EIsnil e) = do
    t <- typeof e
    case t of
        (TList _) -> return TBool
        _ -> lift Nothing
typeof (ELet f ps e1 e2) = do
    t1 <- local (insertAll ps) $ typeof e1
    local (insert f (prependParamTypes t1 ps)) $ typeof e2
    where
        insertAll [] env = env
        insertAll ((Param x t):ps) env = insertAll ps $ insert x t env
        prependParamTypes t ps = Prelude.foldr (\(Param _ t2) t1 -> TFun t2 t1) t ps
typeof (EFix f) = do
    t <- typeof f
    case t of
        (TFun t1 t2)
            | t1 == t2 -> return t1
        _ -> lift Nothing
typeof (ELetrec f t ps e1 e2) = do
    typeof (EApp (EAbs f t e2) [(EFix (EAbs f t e1'))])
    where
        e1' = Prelude.foldr (\(Param x t') e -> (EAbs x t' e)) e1 ps