annotate untyped/Untyped.hs @ 9:4bfeb679ef15

Add eval1 in Untyped
author atton
date Tue, 01 Nov 2016 04:53:18 +0000
parents 5fa32bb75df6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
1 module Untyped where
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
2
9
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
3 import Data.Either
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
4 import Data.Functor
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
5
8
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
6 type Index = Int
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
7 type ContextSize = Int
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
8 type Name = String
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
9 data Binding = NameBind deriving Show
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
10 type Context = [(Name, Binding)]
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
11
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
12 data Term = TmVar Index ContextSize |
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
13 TmAbs String Term |
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
14 TmApp Term Term
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
15 deriving Show
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
16
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
17 -- my implementation
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
18 index2name :: Context -> Index -> String
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
19 index2name ctx i = fst $ ctx !! i
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
20
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
21 pickFreshName :: Context -> Name -> (Context, Name)
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
22 pickFreshName ctx n = (((freshName, NameBind) : ctx), freshName)
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
23 where
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
24 freshName = head . dropWhile (flip elem $ map fst ctx) $ iterate (++ "'") n
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
25
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
26
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
27 -- from book
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
28 showTerm :: Context -> Term -> String
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
29 showTerm ctx (TmAbs x t) = let (ctx', x') = pickFreshName ctx x
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
30 in "(lambda " ++ x' ++ ". " ++ (showTerm ctx' t) ++ ")"
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
31 showTerm ctx (TmApp t1 t2) = "(" ++ (showTerm ctx t1) ++ " " ++ (showTerm ctx t2) ++ ")"
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
32 showTerm ctx (TmVar i n)
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
33 | length ctx == n = index2name ctx i
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
34 | otherwise = "[bad index]"
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
35
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
36 termShift :: Int -> Term -> Term
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
37 termShift d t = walk 0 t
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
38 where
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
39 walk c (TmVar x n)
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
40 | x >= c = TmVar (x+d) (n+d)
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
41 | otherwise = TmVar x (n+d)
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
42 walk c (TmAbs x t1) = TmAbs x (walk (c+1) t1)
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
43 walk c (TmApp t1 t2) = TmApp (walk c t1) (walk c t2)
5fa32bb75df6 Write Untyped to termShift
atton
parents:
diff changeset
44
9
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
45 termSubst :: Index -> Term -> Term -> Term
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
46 termSubst j s t = walk 0 t
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
47 where
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
48 walk c (TmVar x n)
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
49 | x == (j+c) = termShift c s
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
50 | otherwise = TmVar x n
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
51 walk c (TmAbs x t1) = TmAbs x (walk (c+1) t1)
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
52 walk c (TmApp t1 t2) = TmApp (walk c t1) (walk c t2)
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
53
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
54 termSubstTop s t = termShift (-1) (termSubst 0 (termShift 1 s) t)
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
55
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
56 isVal :: Context -> Term -> Bool
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
57 isVal ctx (TmAbs _ _) = True
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
58 isval _ _ = False
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
59
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
60 eval1 :: Context -> Term -> Either String Term
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
61 eval1 ctx (TmApp (TmAbs x t12) v2)
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
62 | isVal ctx v2 = return $ termSubstTop v2 t12
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
63 | otherwise = Left $ show v2 ++ " is not variable."
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
64 eval1 ctx (TmApp t1 t2)
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
65 | isVal ctx t1 = eval1 ctx t2 >>= (\t -> return $ TmApp t1 t)
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
66 | otherwise = eval1 ctx t1 >>= (\t -> return $ TmApp t t2)
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
67 eval1 _ _ = Left "No Rule Applies"
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
68
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
69
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
70 -- x
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
71 sampleContext1 = [("x", NameBind)]
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
72 sampleTerm1 = TmVar 0 1
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
73
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
74 -- \y -> y
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
75 sampleContext2 = []
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
76 sampleTerm2 = TmAbs "y" (TmVar 0 1)
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
77
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
78 -- (\x -> x) (\x -> x x) == \x' -> x' x'
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
79 sampleContext3 = []
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
80 sampleTerm3 = (TmApp (TmAbs "x" (TmVar 0 1)) (TmAbs "x" (TmApp (TmVar 0 1) (TmVar 0 1))))
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
81
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
82 -- (\y -> (\x -> y)) (\x -> x x)
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
83 sampleContext4 = []
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
84 sampleTerm3 = (TmApp (TmAbs "x" (TmVar 0 1)) (TmAbs "x" (TmApp (TmVar 0 1) (TmVar 0 1))))
4bfeb679ef15 Add eval1 in Untyped
atton
parents: 8
diff changeset
85 sampleTerm4 = TmApp (TmAbs "y" (TmAbs "x" (TmVar 1 2))) (TmAbs "x" (TmApp (TmVar 0 1) (TmVar 0 1)))