diff options
author | Péter Diviánszky <divipp@gmail.com> | 2015-12-18 18:31:50 +0100 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2015-12-18 18:31:50 +0100 |
commit | 90b12679223bdd65d622209a0d9eb7a305e534fb (patch) | |
tree | 22750d04bfbd70abefb145195ee86b171b8e6f3f /prototypes | |
parent | 9489ddf4c0fced7ccf2611bbe4fe672ed26a60bb (diff) |
Graphics.lc compiles now
Diffstat (limited to 'prototypes')
-rw-r--r-- | prototypes/CGExp.hs | 44 | ||||
-rw-r--r-- | prototypes/Infer.hs | 14 |
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 | ||
33 | data Exp_ a | 33 | data 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 | ||
45 | instance PShow Exp where pShowPrec p = text . show | 46 | instance PShow Exp where pShowPrec p = text . show |
46 | 47 | ||
47 | pattern Bind a b c d = Exp (Bind_ a b c d) | 48 | pattern Pi h n a b = Exp (Pi_ h n a b) |
49 | pattern Lam h n a b = Exp (Lam_ h n a b) | ||
48 | pattern Con a b = Exp (Con_ a b) | 50 | pattern Con a b = Exp (Con_ a b) |
49 | pattern ELit a = Exp (ELit_ a) | 51 | pattern ELit a = Exp (ELit_ a) |
50 | pattern Fun a b = Exp (Fun_ a b) | 52 | pattern 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 | ||
127 | type Ty = Exp | 131 | type Ty = Exp |
128 | 132 | ||
129 | tyOf :: Exp -> Ty | 133 | tyOf :: Exp -> Ty |
130 | tyOf = \case | 134 | tyOf = \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 | |||
145 | substE n x = \case | 149 | substE 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 | ||
156 | app' (Lam _ n _ x) b = substE n b x | 161 | app' (Lam _ (PVar _ n) _ x) b = substE n b x |
157 | app' a b = EApp a b | 162 | app' a b = EApp a b |
158 | 163 | ||
159 | -------------------------------------------------------------------------------- | 164 | -------------------------------------------------------------------------------- |
@@ -165,23 +170,29 @@ data Pat | |||
165 | 170 | ||
166 | instance PShow Pat where pShowPrec p = text . show | 171 | instance PShow Pat where pShowPrec p = text . show |
167 | 172 | ||
173 | patVars (PVar _ n) = [n] | ||
174 | patVars (PTuple ps) = concatMap patVars ps | ||
175 | |||
176 | patTy (PVar t _) = t | ||
177 | patTy (PTuple ps) = Con ("Tuple" ++ show (length ps), tupTy $ length ps) $ map patTy ps | ||
178 | |||
179 | tupTy n = foldr (:~>) Type $ replicate n Type | ||
180 | |||
168 | ------------- | 181 | ------------- |
169 | 182 | ||
170 | pattern EVar n <- Var n _ | 183 | pattern EVar n <- Var n _ |
171 | pattern TVar t n = Var n t | 184 | pattern TVar t n = Var n t |
172 | 185 | ||
173 | pattern Pi h n a b = Bind (BPi h) n a b | ||
174 | pattern Lam h n a b = Bind (BLam h) n a b | ||
175 | pattern ELam n b <- (mkLam -> Just (n, b)) where ELam n b = eLam n b | 186 | pattern ELam n b <- (mkLam -> Just (n, b)) where ELam n b = eLam n b |
176 | 187 | ||
177 | pattern a :~> b = Pi Visible "" a b | 188 | pattern a :~> b = Pi Visible "" a b |
178 | infixr 1 :~> | 189 | infixr 1 :~> |
179 | 190 | ||
180 | eLam (PVar t n) x = Lam Visible n t x | 191 | eLam p x = Lam Visible p (patTy p) x |
181 | 192 | ||
182 | mkLam (Lam Visible n t (Fun ("Tuple2Case", _) [_, _, motive, Lam Visible n1 t1 (Lam Visible n2 t2 body), Var n' _])) | n == n' | 193 | mkLam (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) |
184 | mkLam (Lam Visible n t b) = Just (PVar t n, b) | 195 | mkLam (Lam Visible p t b) = Just (p, b) |
185 | mkLam _ = Nothing | 196 | mkLam _ = Nothing |
186 | 197 | ||
187 | pattern PrimN n xs <- Fun (n, t) (filterRelevant (n, 0) t -> xs) where PrimN n xs = Fun (n, hackType n) xs | 198 | pattern 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 | ||
272 | pattern ERecord a <- (const Nothing -> Just a) | 286 | pattern 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 @@ | |||
12 | module Infer | 12 | module 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) | |||
266 | pattern EFloat a = ELit (LFloat a) | 266 | pattern EFloat a = ELit (LFloat a) |
267 | 267 | ||
268 | mkBool False = TCon "False" 0 TBool [] | 268 | mkBool False = TCon "False" 0 TBool [] |
269 | mkBool True = TCon "True" 1 TBool [] | 269 | mkBool True = VTrue |
270 | pattern VTrue = TCon "True" 1 TBool [] | ||
270 | 271 | ||
271 | pattern LCon <- (isCon -> True) | 272 | pattern LCon <- (isCon -> True) |
272 | pattern CFun <- (caseFunName -> True) | 273 | pattern 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 |