summaryrefslogtreecommitdiff
path: root/prototypes
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2015-12-18 18:31:50 +0100
committerPéter Diviánszky <divipp@gmail.com>2015-12-18 18:31:50 +0100
commit90b12679223bdd65d622209a0d9eb7a305e534fb (patch)
tree22750d04bfbd70abefb145195ee86b171b8e6f3f /prototypes
parent9489ddf4c0fced7ccf2611bbe4fe672ed26a60bb (diff)
Graphics.lc compiles now
Diffstat (limited to 'prototypes')
-rw-r--r--prototypes/CGExp.hs44
-rw-r--r--prototypes/Infer.hs14
2 files changed, 41 insertions, 17 deletions
diff --git a/prototypes/CGExp.hs b/prototypes/CGExp.hs
index 0ba0199c..92c2f2c4 100644
--- a/prototypes/CGExp.hs
+++ b/prototypes/CGExp.hs
@@ -31,7 +31,8 @@ import Infer (Binder(..), SName, Lit(..), Visibility(..), FunName(..), CaseFunNa
31-------------------------------------------------------------------------------- 31--------------------------------------------------------------------------------
32 32
33data Exp_ a 33data Exp_ a
34 = Bind_ Binder SName a a -- TODO: prohibit meta binder here 34 = Pi_ Visibility SName a a -- TODO: prohibit meta binder here
35 | Lam_ Visibility Pat a a
35 | Con_ (SName, a) [a] 36 | Con_ (SName, a) [a]
36 | ELit_ Lit 37 | ELit_ Lit
37 | Fun_ (SName, a) [a] 38 | Fun_ (SName, a) [a]
@@ -44,7 +45,8 @@ data Exp_ a
44 45
45instance PShow Exp where pShowPrec p = text . show 46instance PShow Exp where pShowPrec p = text . show
46 47
47pattern Bind a b c d = Exp (Bind_ a b c d) 48pattern Pi h n a b = Exp (Pi_ h n a b)
49pattern Lam h n a b = Exp (Lam_ h n a b)
48pattern Con a b = Exp (Con_ a b) 50pattern Con a b = Exp (Con_ a b)
49pattern ELit a = Exp (ELit_ a) 51pattern ELit a = Exp (ELit_ a)
50pattern Fun a b = Exp (Fun_ a b) 52pattern Fun a b = Exp (Fun_ a b)
@@ -74,12 +76,12 @@ toExp = flip runReader [] . flip evalStateT freshTypeVars . f
74 dty = TFloat 76 dty = TFloat
75 (gets head <* modify tail) >>= \n -> return $ app' (EFieldProj (Pi Visible n sty dty) s) e 77 (gets head <* modify tail) >>= \n -> return $ app' (EFieldProj (Pi Visible n sty dty) s) e
76 I.Var i -> asks $ uncurry Var . (!!! i) 78 I.Var i -> asks $ uncurry Var . (!!! i)
77 I.Bind b x y -> (gets head <* modify tail) >>= \n -> do 79 I.Pi b x y -> (gets head <* modify tail) >>= \n -> do
78 t <- f x 80 t <- f x
79 Bind b n t <$> local ((n, t):) (f y) 81 Pi b n t <$> local ((n, t):) (f y)
80 I.Lam b x y -> (gets head <* modify tail) >>= \n -> do 82 I.Lam b x y -> (gets head <* modify tail) >>= \n -> do
81 t <- f x 83 t <- f x
82 Bind (BLam b) n t <$> local ((n, t):) (f y) 84 Lam b (PVar t n) t <$> local ((n, t):) (f y)
83 I.Con (ConName s _ _ t) xs -> con s <$> f t <*> mapM f xs 85 I.Con (ConName s _ _ t) xs -> con s <$> f t <*> mapM f xs
84 I.TyCon (TyConName s _ _ t _ _) xs -> con s <$> f t <*> mapM f xs 86 I.TyCon (TyConName s _ _ t _ _) xs -> con s <$> f t <*> mapM f xs
85 I.ELit l -> pure $ ELit l 87 I.ELit l -> pure $ ELit l
@@ -121,14 +123,16 @@ freeVars = \case
121 ELit _ -> mempty 123 ELit _ -> mempty
122 Fun _ xs -> S.unions $ map freeVars xs 124 Fun _ xs -> S.unions $ map freeVars xs
123 EApp a b -> freeVars a `S.union` freeVars b 125 EApp a b -> freeVars a `S.union` freeVars b
124 Bind _ n a b -> freeVars a `S.union` (S.delete n $ freeVars b) 126 Pi _ n a b -> freeVars a `S.union` (S.delete n $ freeVars b)
127 Lam _ n a b -> freeVars a `S.union` (foldr S.delete (freeVars b) (patVars n))
128 EFieldProj a _ -> freeVars a
125 TType -> mempty 129 TType -> mempty
126 130
127type Ty = Exp 131type Ty = Exp
128 132
129tyOf :: Exp -> Ty 133tyOf :: Exp -> Ty
130tyOf = \case 134tyOf = \case
131 Lam h n t x -> Pi h n t $ tyOf x 135 Lam h (PVar _ n) t x -> Pi h n t $ tyOf x
132 EApp f x -> app (tyOf f) x 136 EApp f x -> app (tyOf f) x
133 Var _ t -> t 137 Var _ t -> t
134 Pi{} -> Type 138 Pi{} -> Type
@@ -145,15 +149,16 @@ tyOf = \case
145substE n x = \case 149substE n x = \case
146 z@(Var n' _) | n' == n -> x 150 z@(Var n' _) | n' == n -> x
147 | otherwise -> z 151 | otherwise -> z
148 Bind h n' a b | n == n' -> Bind h n' (substE n x a) b 152 Pi h n' a b | n == n' -> Pi h n' (substE n x a) b
149 Bind h n' a b -> Bind h n' (substE n x a) (substE n x b) 153 Pi h n' a b -> Pi h n' (substE n x a) (substE n x b)
154 Lam h n' a b -> Lam h n' (substE n x a) $ if n `elem` patVars n' then b else substE n x b
150 Con cn xs -> Con cn (map (substE n x) xs) 155 Con cn xs -> Con cn (map (substE n x) xs)
151 Fun cn xs -> Fun cn (map (substE n x) xs) 156 Fun cn xs -> Fun cn (map (substE n x) xs)
152 TType -> TType 157 TType -> TType
153 EApp a b -> app' (substE n x a) (substE n x b) 158 EApp a b -> app' (substE n x a) (substE n x b)
154 z -> error $ "substE: " ++ show z 159 z -> error $ "substE: " ++ show z
155 160
156app' (Lam _ n _ x) b = substE n b x 161app' (Lam _ (PVar _ n) _ x) b = substE n b x
157app' a b = EApp a b 162app' a b = EApp a b
158 163
159-------------------------------------------------------------------------------- 164--------------------------------------------------------------------------------
@@ -165,23 +170,29 @@ data Pat
165 170
166instance PShow Pat where pShowPrec p = text . show 171instance PShow Pat where pShowPrec p = text . show
167 172
173patVars (PVar _ n) = [n]
174patVars (PTuple ps) = concatMap patVars ps
175
176patTy (PVar t _) = t
177patTy (PTuple ps) = Con ("Tuple" ++ show (length ps), tupTy $ length ps) $ map patTy ps
178
179tupTy n = foldr (:~>) Type $ replicate n Type
180
168------------- 181-------------
169 182
170pattern EVar n <- Var n _ 183pattern EVar n <- Var n _
171pattern TVar t n = Var n t 184pattern TVar t n = Var n t
172 185
173pattern Pi h n a b = Bind (BPi h) n a b
174pattern Lam h n a b = Bind (BLam h) n a b
175pattern ELam n b <- (mkLam -> Just (n, b)) where ELam n b = eLam n b 186pattern ELam n b <- (mkLam -> Just (n, b)) where ELam n b = eLam n b
176 187
177pattern a :~> b = Pi Visible "" a b 188pattern a :~> b = Pi Visible "" a b
178infixr 1 :~> 189infixr 1 :~>
179 190
180eLam (PVar t n) x = Lam Visible n t x 191eLam p x = Lam Visible p (patTy p) x
181 192
182mkLam (Lam Visible n t (Fun ("Tuple2Case", _) [_, _, motive, Lam Visible n1 t1 (Lam Visible n2 t2 body), Var n' _])) | n == n' 193mkLam (Lam Visible (PVar _ n) t (Fun ("Tuple2Case", _) [_, _, motive, Lam Visible (PVar _ n1) t1 (Lam Visible (PVar _ n2) t2 body), Var n' _])) | n == n'
183 = Just (PTuple [PVar t1 n1, PVar t2 n2], body) 194 = Just (PTuple [PVar t1 n1, PVar t2 n2], body)
184mkLam (Lam Visible n t b) = Just (PVar t n, b) 195mkLam (Lam Visible p t b) = Just (p, b)
185mkLam _ = Nothing 196mkLam _ = Nothing
186 197
187pattern PrimN n xs <- Fun (n, t) (filterRelevant (n, 0) t -> xs) where PrimN n xs = Fun (n, hackType n) xs 198pattern PrimN n xs <- Fun (n, t) (filterRelevant (n, 0) t -> xs) where PrimN n xs = Fun (n, hackType n) xs
@@ -267,6 +278,9 @@ getTuple = \case
267 AN "Tuple2" [a, b] -> Just [a, b] 278 AN "Tuple2" [a, b] -> Just [a, b]
268 AN "Tuple3" [a, b, c] -> Just [a, b, c] 279 AN "Tuple3" [a, b, c] -> Just [a, b, c]
269 AN "Tuple4" [a, b, c, d] -> Just [a, b, c, d] 280 AN "Tuple4" [a, b, c, d] -> Just [a, b, c, d]
281 AN "Tuple5" [a, b, c, d, e] -> Just [a, b, c, d, e]
282 AN "Tuple6" [a, b, c, d, e, f] -> Just [a, b, c, d, e, f]
283 AN "Tuple7" [a, b, c, d, e, f, g] -> Just [a, b, c, d, e, f, g]
270 _ -> Nothing 284 _ -> Nothing
271 285
272pattern ERecord a <- (const Nothing -> Just a) 286pattern ERecord a <- (const Nothing -> Just a)
diff --git a/prototypes/Infer.hs b/prototypes/Infer.hs
index c02da80a..09d38a63 100644
--- a/prototypes/Infer.hs
+++ b/prototypes/Infer.hs
@@ -12,7 +12,7 @@
12module Infer 12module Infer
13 ( Binder (..), SName, Lit(..), Visibility(..), FunName(..), CaseFunName(..), ConName(..), TyConName(..), Export(..), ModuleR(..) 13 ( Binder (..), SName, Lit(..), Visibility(..), FunName(..), CaseFunName(..), ConName(..), TyConName(..), Export(..), ModuleR(..)
14 , Exp (..), GlobalEnv 14 , Exp (..), GlobalEnv
15 , pattern Var, pattern Fun, pattern CaseFun, pattern App, pattern FunN, pattern ConN, pattern VV2, pattern VV3, pattern VV4 15 , pattern Var, pattern Fun, pattern CaseFun, pattern App, pattern FunN, pattern ConN, pattern VV2, pattern VV3, pattern VV4, pattern Pi
16 , parse 16 , parse
17 , mkGlobalEnv', joinGlobalEnv', extractGlobalEnv' 17 , mkGlobalEnv', joinGlobalEnv', extractGlobalEnv'
18 , litType, infer 18 , litType, infer
@@ -266,7 +266,8 @@ pattern EInt a = ELit (LInt a)
266pattern EFloat a = ELit (LFloat a) 266pattern EFloat a = ELit (LFloat a)
267 267
268mkBool False = TCon "False" 0 TBool [] 268mkBool False = TCon "False" 0 TBool []
269mkBool True = TCon "True" 1 TBool [] 269mkBool True = VTrue
270pattern VTrue = TCon "True" 1 TBool []
270 271
271pattern LCon <- (isCon -> True) 272pattern LCon <- (isCon -> True)
272pattern CFun <- (caseFunName -> True) 273pattern CFun <- (caseFunName -> True)
@@ -545,6 +546,15 @@ eval te = \case
545 FunN "PrimSubS" [_, _, _, _, EInt x, EInt y] -> EInt (x - y) 546 FunN "PrimSubS" [_, _, _, _, EInt x, EInt y] -> EInt (x - y)
546 FunN "PrimAddS" [_, _, _, _, EFloat x, EFloat y] -> EFloat (x + y) 547 FunN "PrimAddS" [_, _, _, _, EFloat x, EFloat y] -> EFloat (x + y)
547 FunN "PrimMulS" [_, _, _, _, EFloat x, EFloat y] -> EFloat (x * y) 548 FunN "PrimMulS" [_, _, _, _, EFloat x, EFloat y] -> EFloat (x * y)
549 FunN "zeroComp" [TVec (Succ (Succ Zero)) t@TFloat, TT] -> VV2 t (EFloat 0) (EFloat 0)
550 FunN "zeroComp" [TVec (Succ (Succ (Succ Zero))) t@TFloat, TT] -> VV3 t (EFloat 0) (EFloat 0) (EFloat 0)
551 FunN "zeroComp" [TVec (Succ (Succ (Succ (Succ Zero)))) t@TFloat, TT] -> VV4 t (EFloat 0) (EFloat 0) (EFloat 0) (EFloat 0)
552 FunN "oneComp" [TVec (Succ (Succ Zero)) t@TFloat, TT] -> VV2 t (EFloat 1) (EFloat 1)
553 FunN "oneComp" [TVec (Succ (Succ (Succ Zero))) t@TFloat, TT] -> VV3 t (EFloat 1) (EFloat 1) (EFloat 1)
554 FunN "oneComp" [TVec (Succ (Succ (Succ (Succ Zero)))) t@TFloat, TT] -> VV4 t (EFloat 1) (EFloat 1) (EFloat 1) (EFloat 1)
555 FunN "oneComp" [TVec (Succ (Succ Zero)) t@TBool, TT] -> VV2 t VTrue VTrue
556 FunN "oneComp" [TVec (Succ (Succ (Succ Zero))) t@TBool, TT] -> VV3 t VTrue VTrue VTrue
557 FunN "oneComp" [TVec (Succ (Succ (Succ (Succ Zero)))) t@TBool, TT] -> VV4 t VTrue VTrue VTrue VTrue
548 558
549-- todo: elim 559-- todo: elim
550 Fun n@(FunName "natElim" _ _) [a, z, s, Succ x] -> let -- todo: replace let with better abstraction 560 Fun n@(FunName "natElim" _ _) [a, z, s, Succ x] -> let -- todo: replace let with better abstraction