-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypedrec.hs
285 lines (251 loc) · 7.46 KB
/
typedrec.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE FlexibleContexts #-}
import Text.Parsec hiding (many, (<|>))
import Text.Parsec.String
import Data.List
import Data.Maybe
import Control.Applicative
import Control.Monad
import System.Console.Readline
---Data and formatting
data Term =
TmVar Int |
TmAbs String Term Ty |
TmApp Term Term |
TmTest Term Term Term |
TmTrue |
TmFalse |
TmZero |
TmSucc Term |
TmPred Term |
TmIsZero Term |
TmFix Term deriving (Show)
data Ty =
TyBool |
TyArr Ty Ty |
TyNat deriving (Show, Eq)
type Context = [String]
type TyContext = [Ty]
pickFreshName ctx name
| name `elem` ctx = pickFreshName ctx (name ++ "'")
| otherwise = (name, name:ctx)
fmtTerm :: Context -> Term -> String
fmtTerm ctx (TmAbs name t1 ty) =
"(\\" ++ name' ++ ":" ++ show ty ++ "." ++ fmtTerm ctx' t1 ++ ")"
where
(name', ctx') = pickFreshName ctx name
fmtTerm ctx (TmApp t1 t2) = fmtTerm ctx t1 ++ " " ++ fmtTerm ctx t2
fmtTerm ctx (TmVar x) = ctx !! x
fmtTerm ctx (TmTest t1 t2 t3) = "If " ++ fmtTerm ctx t1 ++ " Then " ++ fmtTerm ctx t2 ++ " Else " ++ fmtTerm ctx t3
fmtTerm ctx TmTrue = "true"
fmtTerm ctx TmFalse = "false"
fmtTerm ctx TmZero = "z"
fmtTerm ctx (TmSucc t) = "succ " ++ fmtTerm ctx t
fmtTerm ctx (TmPred t) = "pred " ++ fmtTerm ctx t
fmtTerm ctx (TmIsZero t) = "isz " ++ fmtTerm ctx t
fmtTerm ctx (TmFix t) = "lolwut"
---Parser
pInLine :: Context -> Parser (Maybe String, Term)
pInLine ctx = (pAssign ctx <|> fmap (Nothing,) (pApp ctx))
--This shit is recursive... hopefully
pAssign ctx = do
string "letrec"
spaces
name <- many1 lower
spaces
char ':'
spaces
ty <- pType
spaces
char '='
spaces
term <- pApp (name:ctx)
return (Just name, TmFix (TmAbs name term ty))
--pApp is the top level parser. Every term is some for of application.
--Even if there is no application at all, it will be parsed as 'something'
--applied to nothing.
pApp ctx = do
apps <- sepBy1 (pTerm ctx) spaces
return $ foldl1 TmApp apps
--What we call a Term is actuall every term which is not an application.
--This is to avoid left recursion in the pApp parser. To allow for nested
--applications we have the pPar parser
pTerm :: Context -> Parser Term
pTerm ctx =
try (pTest ctx) <|>
try (pAbs ctx) <|>
try (pVar ctx) <|>
try (pPar ctx)
--This parser allows for nested applications.
pPar ctx = between (char '(') (char ')') (pApp ctx)
pVar ctx = do
name <- many1 lower
let brujin = elemIndex name ctx
maybe (fail $ "Unbound var: " ++ name) (return . TmVar) brujin
pAbs ctx = do
char '\\'
b <- many1 lower
char ':'
ty <- pType
char '.' >> many space
t <- pApp (b:ctx)
return (TmAbs b t ty)
pTest ctx = TmTest <$>
((string "If" >> spaces) *> pTerm ctx <* spaces) <*>
((string "Then" >> spaces) *> pTerm ctx <* spaces) <*>
((string "Else" >> spaces) *> pTerm ctx)
pType = do
arrs <- sepBy1 (pTyBool <|> pTyPar <|> pTyNat) (string "->")
return $ foldr1 TyArr arrs
pTyBool = string "Bool" >> return TyBool
pTyNat = string "Nat" >> return TyNat
pTyPar = between (char '(') (char ')') pType
---Type checking
typeof :: TyContext -> Term -> Maybe Ty
typeof ctx (TmVar x) = return $ ctx !! x
typeof ctx (TmAbs x t ty) = do
ty2 <- typeof (ty:ctx) t
return $ TyArr ty ty2
typeof ctx (TmApp t1 t2) = do
ty1 <- typeof ctx t1
ty2 <- typeof ctx t2
case ty1 of
(TyArr ty11 ty12) ->
if ty2 /= ty11 then Nothing else
return ty12
_ -> Nothing
typeof ctx TmTrue = return TyBool
typeof ctx TmFalse = return TyBool
typeof ctx (TmTest t1 t2 t3) = do
ty1 <- typeof ctx t1
ty2 <- typeof ctx t2
ty3 <- typeof ctx t3
if ty1 /= TyBool then Nothing else
if ty2 /= ty3 then Nothing else
return ty2
typeof ctx TmZero = return TyNat
typeof ctx (TmSucc t) = do
ty <- typeof ctx t
case ty of
TyNat -> return TyNat
_ -> Nothing
typeof ctx (TmPred t) = do
ty <- typeof ctx t
case ty of
TyNat -> return TyNat
_ -> Nothing
typeof ctx (TmIsZero t) = do
ty <- typeof ctx t
case ty of
TyNat -> return TyBool
_ -> Nothing
typeof ctx (TmFix t) = do
ty <- typeof ctx t
case ty of
TyArr t1 t2 -> if t1 == t2 then return t1 else Nothing
_ -> Nothing
---Evaluation
shift :: Int -> Int -> Term -> Term
shift d c (TmVar x)
| x < c = TmVar x
| x >= c = TmVar (x + d)
shift d c (TmAbs name t ty) = TmAbs name (shift d (c+1) t) ty
shift d c (TmApp t1 t2) = TmApp (shift d c t1) (shift d c t2)
shift d c (TmTest t1 t2 t3) = TmTest
(shift d c t1)
(shift d c t2)
(shift d c t3)
shift d c (TmSucc t) = (TmSucc (shift d c t))
shift d c (TmPred t) = (TmPred (shift d c t))
shift d c (TmIsZero t) = (TmIsZero (shift d c t))
shift d c (TmFix t) = (TmFix (shift d c t))
shift d c x = x
subst :: Int -> Term -> Term -> Term
subst j s (TmVar x)
| x == j = s
| otherwise = TmVar x
subst j s (TmAbs name t ty) = TmAbs name (subst (j+1) (shift 1 0 s) t) ty
subst j s (TmApp t1 t2) = TmApp (subst j s t1) (subst j s t2)
subst j s (TmTest t1 t2 t3) = TmTest
(subst j s t1)
(subst j s t2)
(subst j s t3)
subst j s (TmSucc t) = TmSucc (subst j s t)
subst j s (TmPred t) = TmPred (subst j s t)
subst j s (TmIsZero t) = TmIsZero (subst j s t)
subst j s (TmFix t) = TmFix (subst j s t)
subst j s x = x
isNumValue :: Term -> Bool
isNumValue TmZero = True
isNumValue (TmSucc t) = isNumValue t
isNumValue _ = False
isValue :: Term -> Bool
isValue (TmAbs _ _ _) = True
isValue TmTrue = True
isValue TmFalse = True
isValue TmZero = True
isValue (TmSucc t) = isValue t
isValue x = isNumValue x
unSucc :: Term -> Term
unSucc (TmSucc t) = t
unSucc _ = error "Something is really wrong"
recEval :: Term -> Term
recEval (TmApp (TmAbs name t1 ty) t2)
| isValue t2 = recEval $ shift (-1) 0 (subst 0 (shift 1 0 t2) t1)
| otherwise = recEval $ TmApp (TmAbs name t1 ty) (recEval t2)
recEval (TmApp t1 t2) = recEval $ TmApp (recEval t1) t2
recEval (TmTest TmTrue t2 t3) = recEval t2
recEval (TmTest TmFalse t2 t3) = recEval t3
recEval (TmTest t1 t2 t3) = recEval $ TmTest (recEval t1) t2 t3
recEval (TmSucc t) = TmSucc (recEval t)
recEval (TmPred TmZero) = TmZero
recEval (TmPred t)
| isNumValue t = recEval $ unSucc t --Since t is a numeric value and not zero it must be a TmSucc
| otherwise = recEval $ TmPred (recEval t)
recEval (TmIsZero TmZero) = TmTrue
recEval (TmIsZero t)
| isNumValue t = TmFalse
| otherwise = recEval $ TmIsZero (recEval t)
recEval (TmFix (TmAbs name t ty)) =
recEval $ subst 0 (TmFix (TmAbs name t ty)) t
recEval (TmFix t) = recEval $ TmFix (recEval t)
recEval t = t
---REPL
contextWrap name ctxTerm ty term = TmApp (TmAbs name term ty) ctxTerm
startCtx =
(contextWrap "succ"
(TmAbs "n" (TmSucc (TmVar 0)) TyNat)
(TyArr TyNat TyNat))
. (contextWrap "pred"
(TmAbs "n" (TmPred (TmVar 0)) TyNat)
(TyArr TyNat TyNat))
. (contextWrap "isz"
(TmAbs "n" (TmIsZero (TmVar 0)) TyNat)
(TyArr TyNat TyBool))
. (contextWrap "zero"
TmZero
TyNat)
. (contextWrap "false"
TmFalse
TyBool)
. (contextWrap "true"
TmTrue
TyBool)
repl :: Context -> (Term -> Term) -> IO ()
repl ctxNames ctx = do
(Just line) <- readline ">"
case (parse (pInLine ctxNames) line line) of
(Right (Nothing, term)) ->
case typeof [] (ctx term) of
(Just ty) -> do
(putStrLn . fmtTerm ctxNames . recEval) (ctx term)
repl ctxNames ctx
Nothing -> putStrLn "Type error" >> repl ctxNames ctx
(Right (Just name, term)) ->
case typeof [] (ctx term) of
(Just ty) -> do
let term' = recEval $ ctx term
repl (name:ctxNames) (ctx . contextWrap name term' ty)
Nothing -> putStrLn "Type error" >> repl ctxNames ctx
(Left r) -> putStrLn (show r) >> repl ctxNames ctx
main = repl ["true", "false", "z", "isz", "pred","succ"] startCtx