summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-04-18 13:42:01 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-04-18 13:42:01 +0200
commitb400cc4488eb32d15074de18ce0696addd6d01e5 (patch)
tree1f9e2cb06368a1e4b6bcae7777707a26967a6402 /src
parent708347e096898017a2ca66c6e96535881a527688 (diff)
refactoring
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/Compiler/CoreToIR.hs3
-rw-r--r--src/LambdaCube/Compiler/Infer.hs61
-rw-r--r--src/LambdaCube/Compiler/Parser.hs45
3 files changed, 58 insertions, 51 deletions
diff --git a/src/LambdaCube/Compiler/CoreToIR.hs b/src/LambdaCube/Compiler/CoreToIR.hs
index 0d1557c3..6fe8992f 100644
--- a/src/LambdaCube/Compiler/CoreToIR.hs
+++ b/src/LambdaCube/Compiler/CoreToIR.hs
@@ -910,9 +910,10 @@ instance Up ExpTV where
910 up_ n i (ExpTV x xt vs) = error "up @ExpTV" --ExpTV (up_ n i x) (up_ n i xt) (up_ n i <$> vs) 910 up_ n i (ExpTV x xt vs) = error "up @ExpTV" --ExpTV (up_ n i x) (up_ n i xt) (up_ n i <$> vs)
911 used i (ExpTV x xt vs) = used i x || used i xt -- -|| any (used i) vs{-?-} 911 used i (ExpTV x xt vs) = used i x || used i xt -- -|| any (used i) vs{-?-}
912 fold = error "fold @ExpTV" 912 fold = error "fold @ExpTV"
913 maxDB_ (ExpTV a b cs) = maxDB_ a <> maxDB_ b -- <> foldMap maxDB_ cs{-?-}
914 closedExp (ExpTV a b cs) = ExpTV (closedExp a) (closedExp b) cs 913 closedExp (ExpTV a b cs) = ExpTV (closedExp a) (closedExp b) cs
915 914
915-- maxDB_ (ExpTV a b cs) = maxDB_ a <> maxDB_ b -- <> foldMap maxDB_ cs{-?-}
916
916instance PShow ExpTV where 917instance PShow ExpTV where
917 pShowPrec p (ExpTV x t _) = pShowPrec p (x, t) 918 pShowPrec p (ExpTV x t _) = pShowPrec p (x, t)
918 919
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs
index aba0f74a..9cac1299 100644
--- a/src/LambdaCube/Compiler/Infer.hs
+++ b/src/LambdaCube/Compiler/Infer.hs
@@ -15,13 +15,13 @@
15{-# OPTIONS_GHC -fno-warn-unused-binds #-} -- TODO: remove 15{-# OPTIONS_GHC -fno-warn-unused-binds #-} -- TODO: remove
16-- {-# OPTIONS_GHC -O0 #-} 16-- {-# OPTIONS_GHC -O0 #-}
17module LambdaCube.Compiler.Infer 17module LambdaCube.Compiler.Infer
18 ( Binder (..), SName, Lit(..), Visibility(..) 18 ( SName, Lit(..), Visibility(..)
19 , Exp (..), Neutral (..), ExpType, GlobalEnv 19 , Exp (..), Neutral (..), ExpType, GlobalEnv
20 , pattern Var, pattern CaseFun, pattern TyCaseFun, pattern App_, app_ 20 , pattern Var, pattern CaseFun, pattern TyCaseFun, pattern App_, app_
21 , pattern Con, pattern TyCon, pattern Pi, pattern Lam, pattern Fun, pattern ELit, pattern Func, pattern LabelEnd, pattern FL, pattern UFL, unFunc_ 21 , pattern Con, pattern TyCon, pattern Pi, pattern Lam, pattern Fun, pattern ELit, pattern Func, pattern LabelEnd, pattern FL, pattern UFL, unFunc_
22 , outputType, boolType, trueExp 22 , outputType, boolType, trueExp
23 , down, Subst (..), free, subst 23 , down, Subst (..), free, subst
24 , initEnv, Env(..), pattern EBind2 24 , initEnv, Env(..)
25 , SI(..), Range(..) -- todo: remove 25 , SI(..), Range(..) -- todo: remove
26 , Info(..), Infos, listAllInfos, listTypeInfos, listTraceInfos 26 , Info(..), Infos, listAllInfos, listTypeInfos, listTraceInfos
27 , inference, IM 27 , inference, IM
@@ -113,13 +113,6 @@ data FName
113 | FVecS 113 | FVecS
114 | FEmpty 114 | FEmpty
115 | FHList 115 | FHList
116 | FEq
117 | FOrd
118 | FNum
119 | FSigned
120 | FComponent
121 | FIntegral
122 | FFloating
123 | FOutput 116 | FOutput
124 | FType 117 | FType
125 | FHCons 118 | FHCons
@@ -135,6 +128,14 @@ data FName
135 | FNil 128 | FNil
136 | FCons 129 | FCons
137 | FSplit 130 | FSplit
131 -- todo: elim
132 | FEq
133 | FOrd
134 | FNum
135 | FSigned
136 | FComponent
137 | FIntegral
138 | FFloating
138 deriving (Eq, Ord) 139 deriving (Eq, Ord)
139 140
140cFName (RangeSI (Range fn p _), s) = fromMaybe (CFName (hashPos fn p) $ SData s) $ lookup s fntable 141cFName (RangeSI (Range fn p _), s) = fromMaybe (CFName (hashPos fn p) $ SData s) $ lookup s fntable
@@ -413,6 +414,16 @@ instance Up Exp where
413 ELit{} -> mempty 414 ELit{} -> mempty
414 Neut x -> fold f i x 415 Neut x -> fold f i x
415 416
417 closedExp = \case
418 Lam_ _ c -> Lam_ mempty c
419 Pi_ _ a b c -> Pi_ mempty a (closedExp b) c
420 Con_ _ a b c -> Con_ mempty a b (closedExp <$> c)
421 TyCon_ _ a b -> TyCon_ mempty a (closedExp <$> b)
422 e@TType{} -> e
423 e@ELit{} -> e
424 Neut a -> Neut $ closedExp a
425
426instance HasMaxDB Exp where
416 maxDB_ = \case 427 maxDB_ = \case
417 Lam_ c _ -> c 428 Lam_ c _ -> c
418 Pi_ c _ _ _ -> c 429 Pi_ c _ _ _ -> c
@@ -423,15 +434,6 @@ instance Up Exp where
423 ELit{} -> mempty 434 ELit{} -> mempty
424 Neut x -> maxDB_ x 435 Neut x -> maxDB_ x
425 436
426 closedExp = \case
427 Lam_ _ c -> Lam_ mempty c
428 Pi_ _ a b c -> Pi_ mempty a (closedExp b) c
429 Con_ _ a b c -> Con_ mempty a b (closedExp <$> c)
430 TyCon_ _ a b -> TyCon_ mempty a (closedExp <$> b)
431 e@TType{} -> e
432 e@ELit{} -> e
433 Neut a -> Neut $ closedExp a
434
435instance Subst Exp Exp where 437instance Subst Exp Exp where
436 subst_ i0 dx x = f i0 438 subst_ i0 dx x = f i0
437 where 439 where
@@ -480,15 +482,6 @@ instance Up Neutral where
480 LabelEnd_ x -> fold f i x 482 LabelEnd_ x -> fold f i x
481 Delta{} -> mempty 483 Delta{} -> mempty
482 484
483 maxDB_ = \case
484 Var_ k -> varDB k
485 CaseFun__ c _ _ _ -> c
486 TyCaseFun__ c _ _ _ -> c
487 App__ c a b -> c
488 Fun_ c _ _ _ _ _ -> c
489 LabelEnd_ x -> maxDB_ x
490 Delta{} -> mempty
491
492 closedExp = \case 485 closedExp = \case
493 x@Var_{} -> error "impossible" 486 x@Var_{} -> error "impossible"
494 CaseFun__ _ a as n -> CaseFun__ mempty a (closedExp <$> as) (closedExp n) 487 CaseFun__ _ a as n -> CaseFun__ mempty a (closedExp <$> as) (closedExp n)
@@ -498,6 +491,16 @@ instance Up Neutral where
498 LabelEnd_ a -> LabelEnd_ (closedExp a) 491 LabelEnd_ a -> LabelEnd_ (closedExp a)
499 d@Delta{} -> d 492 d@Delta{} -> d
500 493
494instance HasMaxDB Neutral where
495 maxDB_ = \case
496 Var_ k -> varDB k
497 CaseFun__ c _ _ _ -> c
498 TyCaseFun__ c _ _ _ -> c
499 App__ c a b -> c
500 Fun_ c _ _ _ _ _ -> c
501 LabelEnd_ x -> maxDB_ x
502 Delta{} -> mempty
503
501instance (Subst x a, Subst x b) => Subst x (a, b) where 504instance (Subst x a, Subst x b) => Subst x (a, b) where
502 subst_ i dx x (a, b) = (subst_ i dx x a, subst_ i dx x b) 505 subst_ i dx x (a, b) = (subst_ i dx x a, subst_ i dx x b)
503 506
@@ -702,7 +705,7 @@ instance (Subst Exp a, Up a) => Up (CEnv a) where
702 705
703 fold _ _ _ = error "fold @(CEnv _)" 706 fold _ _ _ = error "fold @(CEnv _)"
704 707
705 maxDB_ _ = error "maxDB_ @(CEnv _)" 708-- maxDB_ _ = error "maxDB_ @(CEnv _)"
706 709
707instance (Subst Exp a, Up a) => Subst Exp (CEnv a) where 710instance (Subst Exp a, Up a) => Subst Exp (CEnv a) where
708 subst_ i dx x = \case 711 subst_ i dx x = \case
@@ -868,8 +871,8 @@ inferN_ tellTrace = infer where
868 SLit si l -> focus_' te exp (ELit l, litType l) 871 SLit si l -> focus_' te exp (ELit l, litType l)
869 STyped si et -> focus_' te exp et 872 STyped si et -> focus_' te exp et
870 SGlobal (si, s) -> focus_' te exp =<< getDef te si s 873 SGlobal (si, s) -> focus_' te exp =<< getDef te si s
871 SApp si h a b -> infer (EApp1 (si `validate` [sourceInfo a, sourceInfo b]) h te b) a
872 SLet le a b -> infer (ELet1 le te b{-in-}) a{-let-} -- infer te SLamV b `SAppV` a) 874 SLet le a b -> infer (ELet1 le te b{-in-}) a{-let-} -- infer te SLamV b `SAppV` a)
875 SApp si h a b -> infer (EApp1 (si `validate` [sourceInfo a, sourceInfo b]) h te b) a
873 SBind si h _ a b -> infer ((if h /= BMeta then CheckType_ (sourceInfo exp) TType else id) $ EBind1 si h te $ (if isPi h then TyType else id) b) a 876 SBind si h _ a b -> infer ((if h /= BMeta then CheckType_ (sourceInfo exp) TType else id) $ EBind1 si h te $ (if isPi h then TyType else id) b) a
874 877
875 checkN :: Env -> SExp2 -> Type -> IM m ExpType' 878 checkN :: Env -> SExp2 -> Type -> IM m ExpType'
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs
index 43d92f22..1c2569f7 100644
--- a/src/LambdaCube/Compiler/Parser.hs
+++ b/src/LambdaCube/Compiler/Parser.hs
@@ -14,11 +14,11 @@ module LambdaCube.Compiler.Parser
14 , Module(..), Visibility(..), Binder(..), SExp'(..), Extension(..), Extensions 14 , Module(..), Visibility(..), Binder(..), SExp'(..), Extension(..), Extensions
15 , pattern SVar, pattern SType, pattern Wildcard, pattern SAppV, pattern SLamV, pattern SAnn 15 , pattern SVar, pattern SType, pattern Wildcard, pattern SAppV, pattern SLamV, pattern SAnn
16 , pattern SBuiltin, pattern SPi, pattern Primitive, pattern SLabelEnd, pattern SLam, pattern Parens 16 , pattern SBuiltin, pattern SPi, pattern Primitive, pattern SLabelEnd, pattern SLam, pattern Parens
17 , pattern TyType, pattern Wildcard_ 17 , pattern TyType, pattern Wildcard_, pattern SLet
18 , debug, isPi, varDB, lowerDB, upDB, cmpDB, MaxDB(..), iterateN, traceD 18 , debug, isPi, varDB, lowerDB, upDB, cmpDB, MaxDB(..), iterateN, traceD
19 , parseLC, runDefParser 19 , parseLC, runDefParser
20 , getParamsS, addParamsS, getApps, apps', downToS, addForalls 20 , getParamsS, addParamsS, getApps, apps', downToS, addForalls
21 , Up (..), up1, up 21 , Up (..), up1, up, HasMaxDB (..)
22 , Doc, shLam, shApp, shLet, shLet_, shAtom, shAnn, shVar, epar, showDoc, showDoc_, sExpDoc, shCstr, shTuple 22 , Doc, shLam, shApp, shLet, shLet_, shAtom, shAnn, shVar, epar, showDoc, showDoc_, sExpDoc, shCstr, shTuple
23 , mtrace, sortDefs 23 , mtrace, sortDefs
24 , trSExp', usedS, substSG0, substS 24 , trSExp', usedS, substSG0, substS
@@ -91,12 +91,12 @@ instance Show Void where show _ = error "show @Void"
91instance Eq Void where _ == _ = error "(==) @Void" 91instance Eq Void where _ == _ = error "(==) @Void"
92 92
93data SExp' a 93data SExp' a
94 = SGlobal SIName 94 = SLit SI Lit
95 | SBind SI Binder (SData SIName{-parameter's name-}) (SExp' a) (SExp' a) 95 | SGlobal SIName
96 | SApp SI Visibility (SExp' a) (SExp' a) 96 | SApp SI Visibility (SExp' a) (SExp' a)
97 | SLet SIName (SExp' a) (SExp' a) -- let x = e in f --> SLet e f{-x is Var 0-} 97 | SBind SI Binder (SData SIName){-parameter name-} (SExp' a) (SExp' a)
98 | SVar_ (SData SIName) !Int 98 | SVar_ (SData SIName) !Int
99 | SLit SI Lit 99 | SLet_ SI (SData SIName) (SExp' a) (SExp' a) -- let x = e in f --> SLet e f{-x is Var 0-}
100 | STyped SI a 100 | STyped SI a
101 deriving (Eq, Show) 101 deriving (Eq, Show)
102 102
@@ -117,6 +117,7 @@ pattern SLam h a b <- SBind _ (BLam h) _ a b where SLam h a b = sBind (BLam h) (
117pattern Wildcard t <- SBind _ BMeta _ t (SVar _ 0) where Wildcard t = sBind BMeta (SData (debugSI "pattern Wildcard2", "pattern_wildcard_name")) t (SVar (debugSI "pattern Wildcard2", ".wc") 0) 117pattern Wildcard t <- SBind _ BMeta _ t (SVar _ 0) where Wildcard t = sBind BMeta (SData (debugSI "pattern Wildcard2", "pattern_wildcard_name")) t (SVar (debugSI "pattern Wildcard2", ".wc") 0)
118pattern Wildcard_ si t <- SBind _ BMeta _ t (SVar (si, _) 0) 118pattern Wildcard_ si t <- SBind _ BMeta _ t (SVar (si, _) 0)
119pattern SLamV a = SLam Visible (Wildcard SType) a 119pattern SLamV a = SLam Visible (Wildcard SType) a
120pattern SLet n a b <- SLet_ _ (SData n) a b where SLet n a b = SLet_ (sourceInfo a <> sourceInfo b) (SData n) a b
120 121
121pattern SApp' h a b <- SApp _ h a b where SApp' h a b = sApp h a b 122pattern SApp' h a b <- SApp _ h a b where SApp' h a b = sApp h a b
122pattern SAppH a b = SApp' Hidden a b 123pattern SAppH a b = SApp' Hidden a b
@@ -124,15 +125,14 @@ pattern SAppV a b = SApp' Visible a b
124pattern SAppV2 f a b = f `SAppV` a `SAppV` b 125pattern SAppV2 f a b = f `SAppV` a `SAppV` b
125 126
126pattern SType = SBuiltin "'Type" 127pattern SType = SBuiltin "'Type"
128pattern SLabelEnd a = SBuiltin "labelend" `SAppV` a
129pattern Section e = SBuiltin "^section" `SAppV` e
130pattern Parens e = SBuiltin "parens" `SAppV` e
127pattern SAnn a t = SBuiltin "typeAnn" `SAppH` t `SAppV` a 131pattern SAnn a t = SBuiltin "typeAnn" `SAppH` t `SAppV` a
128pattern TyType a = SAnn a SType 132pattern TyType a = SAnn a SType
129pattern SLabelEnd a = SBuiltin "labelend" `SAppV` a
130 133
131pattern SBuiltin s <- SGlobal (_, s) where SBuiltin s = SGlobal (debugSI $ "builtin " ++ s, s) 134pattern SBuiltin s <- SGlobal (_, s) where SBuiltin s = SGlobal (debugSI $ "builtin " ++ s, s)
132 135
133pattern Section e = SBuiltin "^section" `SAppV` e
134pattern Parens e = SBuiltin "parens" `SAppV` e
135
136sApp v a b = SApp (sourceInfo a <> sourceInfo b) v a b 136sApp v a b = SApp (sourceInfo a <> sourceInfo b) v a b
137sBind v x a b = SBind (sourceInfo a <> sourceInfo b) v x a b 137sBind v x a b = SBind (sourceInfo a <> sourceInfo b) v x a b
138 138
@@ -160,7 +160,7 @@ instance SourceInfo (SExp' a) where
160 SGlobal (si, _) -> si 160 SGlobal (si, _) -> si
161 SBind si _ _ e1 e2 -> si 161 SBind si _ _ e1 e2 -> si
162 SApp si _ e1 e2 -> si 162 SApp si _ e1 e2 -> si
163 SLet _ e1 e2 -> sourceInfo e1 <> sourceInfo e2 163 SLet_ si _ e1 e2 -> si
164 SVar (si, _) _ -> si 164 SVar (si, _) _ -> si
165 STyped si _ -> si 165 STyped si _ -> si
166 SLit si _ -> si 166 SLit si _ -> si
@@ -169,7 +169,7 @@ instance SetSourceInfo (SExp' a) where
169 setSI si = \case 169 setSI si = \case
170 SBind _ a b c d -> SBind si a b c d 170 SBind _ a b c d -> SBind si a b c d
171 SApp _ a b c -> SApp si a b c 171 SApp _ a b c -> SApp si a b c
172 SLet le a b -> SLet le a b 172 SLet_ _ le a b -> SLet_ si le a b
173 SVar (_, n) i -> SVar (si, n) i 173 SVar (_, n) i -> SVar (si, n) i
174 STyped _ t -> STyped si t 174 STyped _ t -> STyped si t
175 SGlobal (_, n) -> SGlobal (si, n) 175 SGlobal (_, n) -> SGlobal (si, n)
@@ -229,6 +229,12 @@ upDB x (MaxDB i) = MaxDB $ ad x i where
229-} 229-}
230-------------------------------------------------------------------------------- low-level toolbox 230-------------------------------------------------------------------------------- low-level toolbox
231 231
232class HasMaxDB a where
233 maxDB_ :: a -> MaxDB
234
235instance (HasMaxDB a, HasMaxDB b) => HasMaxDB (a, b) where
236 maxDB_ (a, b) = maxDB_ a <> maxDB_ b
237
232class Up a where 238class Up a where
233 up_ :: Int -> Int -> a -> a 239 up_ :: Int -> Int -> a -> a
234 up_ n i = iterateN n $ up1_ i 240 up_ n i = iterateN n $ up1_ i
@@ -240,8 +246,6 @@ class Up a where
240 used :: Int -> a -> Bool 246 used :: Int -> a -> Bool
241 used = (getAny .) . fold ((Any .) . (==)) 247 used = (getAny .) . fold ((Any .) . (==))
242 248
243 maxDB_ :: a -> MaxDB
244
245 closedExp :: a -> a 249 closedExp :: a -> a
246 closedExp a = a 250 closedExp a = a
247 251
@@ -249,7 +253,6 @@ instance (Up a, Up b) => Up (a, b) where
249 up_ n i (a, b) = (up_ n i a, up_ n i b) 253 up_ n i (a, b) = (up_ n i a, up_ n i b)
250 used i (a, b) = used i a || used i b 254 used i (a, b) = used i a || used i b
251 fold f i (a, b) = fold f i a <> fold f i b 255 fold f i (a, b) = fold f i a <> fold f i b
252 maxDB_ (a, b) = maxDB_ a <> maxDB_ b
253 closedExp (a, b) = (closedExp a, closedExp b) 256 closedExp (a, b) = (closedExp a, closedExp b)
254 257
255up n = up_ n 0 258up n = up_ n 0
@@ -281,7 +284,7 @@ mapS' = mapS__ (\_ _ _ -> error "mapS'") (const . SGlobal)
281mapS__ hh gg f2 h = g where 284mapS__ hh gg f2 h = g where
282 g i = \case 285 g i = \case
283 SApp si v a b -> SApp si v (g i a) (g i b) 286 SApp si v a b -> SApp si v (g i a) (g i b)
284 SLet x a b -> SLet x (g i a) (g (h i) b) 287 SLet_ si x a b -> SLet_ si x (g i a) (g (h i) b)
285 SBind si k si' a b -> SBind si k si' (g i a) (g (h i) b) 288 SBind si k si' a b -> SBind si k si' (g i a) (g (h i) b)
286 SVar sn j -> f2 sn j i 289 SVar sn j -> f2 sn j i
287 SGlobal sn -> gg sn i 290 SGlobal sn -> gg sn i
@@ -305,7 +308,7 @@ substSG0 n = substSG n 0 . up1
305instance Up Void where 308instance Up Void where
306 up_ n i = error "up_ @Void" 309 up_ n i = error "up_ @Void"
307 fold _ = error "fold_ @Void" 310 fold _ = error "fold_ @Void"
308 maxDB_ _ = error "maxDB @Void" 311-- maxDB_ _ = error "maxDB @Void"
309 312
310instance Up a => Up (SExp' a) where 313instance Up a => Up (SExp' a) where
311 up_ n = mapS' (\sn j i -> SVar sn $ if j < i then j else j+n) (+1) 314 up_ n = mapS' (\sn j i -> SVar sn $ if j < i then j else j+n) (+1)
@@ -313,7 +316,7 @@ instance Up a => Up (SExp' a) where
313 mapS' = mapS__ (\i si x -> STyped si $ up_ n i x) (const . SGlobal) 316 mapS' = mapS__ (\i si x -> STyped si $ up_ n i x) (const . SGlobal)
314 317
315 fold f = foldS (\i si x -> fold f i x) mempty $ \sn j i -> f j i 318 fold f = foldS (\i si x -> fold f i x) mempty $ \sn j i -> f j i
316 maxDB_ _ = error "maxDB @SExp" 319-- maxDB_ _ = error "maxDB @SExp"
317 320
318dbf' = dbf_ 0 321dbf' = dbf_ 0
319dbf_ j xs e = foldl (\e (i, sn) -> substSG sn i e) e $ zip [j..] xs 322dbf_ j xs e = foldl (\e (i, sn) -> substSG sn i e) e $ zip [j..] xs
@@ -330,7 +333,7 @@ trSExp :: (a -> b) -> SExp' a -> SExp' b
330trSExp f = g where 333trSExp f = g where
331 g = \case 334 g = \case
332 SApp si v a b -> SApp si v (g a) (g b) 335 SApp si v a b -> SApp si v (g a) (g b)
333 SLet x a b -> SLet x (g a) (g b) 336 SLet_ si x a b -> SLet_ si x (g a) (g b)
334 SBind si k si' a b -> SBind si k si' (g a) (g b) 337 SBind si k si' a b -> SBind si k si' (g a) (g b)
335 SVar sn j -> SVar sn j 338 SVar sn j -> SVar sn j
336 SGlobal sn -> SGlobal sn 339 SGlobal sn -> SGlobal sn