summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-04-30 00:00:50 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-04-30 00:15:39 +0200
commitb08f6a129c452526b7c2667dcd629028d66ebf0a (patch)
tree7f66ff7bbc8a41ef9f5550dc3338a51f18e3f563 /src
parent37042ee075b68675257270c4c5b2a9152f2bf8e2 (diff)
refactoring
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/Compiler/CoreToIR.hs13
-rw-r--r--src/LambdaCube/Compiler/DeBruijn.hs3
-rw-r--r--src/LambdaCube/Compiler/DesugaredSource.hs66
-rw-r--r--src/LambdaCube/Compiler/Infer.hs69
-rw-r--r--src/LambdaCube/Compiler/Lexer.hs17
-rw-r--r--src/LambdaCube/Compiler/Parser.hs6
-rw-r--r--src/LambdaCube/Compiler/Patterns.hs4
-rw-r--r--src/LambdaCube/Compiler/Pretty.hs24
-rw-r--r--src/LambdaCube/Compiler/Statements.hs8
-rw-r--r--src/LambdaCube/Compiler/Utils.hs21
10 files changed, 111 insertions, 120 deletions
diff --git a/src/LambdaCube/Compiler/CoreToIR.hs b/src/LambdaCube/Compiler/CoreToIR.hs
index 0c8b8006..7e4a938e 100644
--- a/src/LambdaCube/Compiler/CoreToIR.hs
+++ b/src/LambdaCube/Compiler/CoreToIR.hs
@@ -576,7 +576,7 @@ genGLSLs backend
576 reds x = error $ "red: " ++ ppShow x 576 reds x = error $ "red: " ++ ppShow x
577 genGLSL' err vertOuts (ps, o) 577 genGLSL' err vertOuts (ps, o)
578 | length ps == length vertOuts = genGLSL (reverse vertOuts) o 578 | length ps == length vertOuts = genGLSL (reverse vertOuts) o
579 | otherwise = error $ "makeSubst illegal input " ++ err ++ " " ++ show ps ++ "\n" ++ show vertOuts 579 | otherwise = error $ "makeSubst illegal input " ++ err ++ " " ++ ppShow ps ++ "\n" ++ ppShow vertOuts
580 580
581 noUnit TTuple0 = False 581 noUnit TTuple0 = False
582 noUnit _ = True 582 noUnit _ = True
@@ -623,7 +623,6 @@ data Uniform
623 = UUniform 623 = UUniform
624 | UTexture2DSlot 624 | UTexture2DSlot
625 | UTexture2D Integer Integer ExpTV 625 | UTexture2D Integer Integer ExpTV
626 deriving (Show)
627 626
628type Uniforms = Map String (Uniform, IR.InputType) 627type Uniforms = Map String (Uniform, IR.InputType)
629 628
@@ -638,7 +637,7 @@ simpleExpr = \case
638genGLSL :: [SName] -> ExpTV -> WriterT (Uniforms, Map.Map SName (ExpTV, ExpTV, [ExpTV])) (State [String]) Doc 637genGLSL :: [SName] -> ExpTV -> WriterT (Uniforms, Map.Map SName (ExpTV, ExpTV, [ExpTV])) (State [String]) Doc
639genGLSL dns e = case e of 638genGLSL dns e = case e of
640 639
641 ELit a -> pure $ text $ show a 640 ELit a -> pure $ pShow a
642 Var i _ -> pure $ text $ dns !! i 641 Var i _ -> pure $ text $ dns !! i
643 642
644 Func fn def ty xs | not (simpleExpr def) -> tell (mempty, Map.singleton fn (def, ty, map tyOf xs)) >> call fn xs 643 Func fn def ty xs | not (simpleExpr def) -> tell (mempty, Map.singleton fn (def, ty, map tyOf xs)) >> call fn xs
@@ -822,7 +821,7 @@ genGLSL dns e = case e of
822 821
823-- expression + type + type of local variables 822-- expression + type + type of local variables
824data ExpTV = ExpTV_ Exp Exp [Exp] 823data ExpTV = ExpTV_ Exp Exp [Exp]
825 deriving (Show, Eq) 824 deriving (Eq)
826 825
827pattern ExpTV a b c <- ExpTV_ a b c where ExpTV a b c = ExpTV_ (a) (unLab' b) c 826pattern ExpTV a b c <- ExpTV_ a b c where ExpTV a b c = ExpTV_ (a) (unLab' b) c
828 827
@@ -888,7 +887,7 @@ chain vs t xs = map snd $ chain' vs t xs
888 887
889chain' vs t [] = [] 888chain' vs t [] = []
890chain' vs t@(I.Pi b at y) (a: as) = (b, ExpTV a at vs): chain' vs (appTy t a) as 889chain' vs t@(I.Pi b at y) (a: as) = (b, ExpTV a at vs): chain' vs (appTy t a) as
891chain' vs t _ = error $ "chain: " ++ show t 890chain' vs t _ = error $ "chain: " ++ ppShow t
892 891
893mkTVar i (ExpTV t _ vs) = ExpTV (I.Var i) t vs 892mkTVar i (ExpTV t _ vs) = ExpTV (I.Var i) t vs
894 893
@@ -1001,7 +1000,7 @@ getTuple _ = Nothing
1001genHLSL :: [SName] -> ExpTV -> WriterT (Uniforms, Map.Map SName (ExpTV, ExpTV, [ExpTV])) (State [String]) Doc 1000genHLSL :: [SName] -> ExpTV -> WriterT (Uniforms, Map.Map SName (ExpTV, ExpTV, [ExpTV])) (State [String]) Doc
1002genHLSL dns e = case e of 1001genHLSL dns e = case e of
1003 1002
1004 ELit a -> pure $ text $ show a 1003 ELit a -> pure $ pShow a
1005 Var i _ -> pure $ text $ dns !! i 1004 Var i _ -> pure $ text $ dns !! i
1006 1005
1007 Func fn def ty xs | not (simpleExpr def) -> tell (mempty, Map.singleton fn (def, ty, map tyOf xs)) >> call fn xs 1006 Func fn def ty xs | not (simpleExpr def) -> tell (mempty, Map.singleton fn (def, ty, map tyOf xs)) >> call fn xs
@@ -1288,7 +1287,7 @@ genHLSLs backend
1288 reds x = error $ "red: " ++ ppShow x 1287 reds x = error $ "red: " ++ ppShow x
1289 genHLSL' err vertOuts (ps, o) 1288 genHLSL' err vertOuts (ps, o)
1290 | length ps == length vertOuts = genHLSL (reverse vertOuts) o 1289 | length ps == length vertOuts = genHLSL (reverse vertOuts) o
1291 | otherwise = error $ "makeSubst illegal input " ++ err ++ " " ++ show ps ++ "\n" ++ show vertOuts 1290 | otherwise = error $ "makeSubst illegal input " ++ err ++ " " ++ ppShow ps ++ "\n" ++ ppShow vertOuts
1292 1291
1293 noUnit TTuple0 = False 1292 noUnit TTuple0 = False
1294 noUnit _ = True 1293 noUnit _ = True
diff --git a/src/LambdaCube/Compiler/DeBruijn.hs b/src/LambdaCube/Compiler/DeBruijn.hs
index 4cf286fc..c0fce31e 100644
--- a/src/LambdaCube/Compiler/DeBruijn.hs
+++ b/src/LambdaCube/Compiler/DeBruijn.hs
@@ -46,13 +46,14 @@ instance Rearrange Void where
46 46
47-------------------------------------------------------------------------------- fold De Bruijn indices 47-------------------------------------------------------------------------------- fold De Bruijn indices
48 48
49class Up a where 49class Up{-TODO: rename-} a where
50 50
51 foldVar :: Monoid e => (Int{-level-} -> Int{-index-} -> e) -> Int -> a -> e 51 foldVar :: Monoid e => (Int{-level-} -> Int{-index-} -> e) -> Int -> a -> e
52 52
53 usedVar :: Int -> a -> Bool 53 usedVar :: Int -> a -> Bool
54 usedVar = (getAny .) . foldVar ((Any .) . (==)) 54 usedVar = (getAny .) . foldVar ((Any .) . (==))
55 55
56 -- TODO: remove
56 closedExp :: a -> a 57 closedExp :: a -> a
57 closedExp a = a 58 closedExp a = a
58 59
diff --git a/src/LambdaCube/Compiler/DesugaredSource.hs b/src/LambdaCube/Compiler/DesugaredSource.hs
index b3880a3d..4c52d5f1 100644
--- a/src/LambdaCube/Compiler/DesugaredSource.hs
+++ b/src/LambdaCube/Compiler/DesugaredSource.hs
@@ -24,7 +24,7 @@ import qualified Data.Set as Set
24import qualified Data.IntMap as IM 24import qualified Data.IntMap as IM
25import Control.Arrow hiding ((<+>)) 25import Control.Arrow hiding ((<+>))
26import Control.DeepSeq 26import Control.DeepSeq
27import Debug.Trace 27--import Debug.Trace
28 28
29import LambdaCube.Compiler.Utils 29import LambdaCube.Compiler.Utils
30import LambdaCube.Compiler.DeBruijn 30import LambdaCube.Compiler.DeBruijn
@@ -92,7 +92,6 @@ data Range = Range !FileInfo !SPos !SPos
92instance NFData Range where 92instance NFData Range where
93 rnf Range{} = () 93 rnf Range{} = ()
94 94
95-- short version
96instance PShow Range 95instance PShow Range
97 where 96 where
98 pShow (Range n b@(SPos r c) e@(SPos r' c')) = expand (pShow n <+> pShow b <> "-" <> pShow e) 97 pShow (Range n b@(SPos r c) e@(SPos r' c')) = expand (pShow n <+> pShow b <> "-" <> pShow e)
@@ -100,8 +99,6 @@ instance PShow Range
100 : map text (drop (r - 1) $ take r' $ lines $ fileContent n) 99 : map text (drop (r - 1) $ take r' $ lines $ fileContent n)
101 ++ [text $ replicate (c - 1) ' ' ++ replicate (c' - c) '^' | r' == r] 100 ++ [text $ replicate (c - 1) ' ' ++ replicate (c' - c) '^' | r' == r]
102 101
103instance Show Range where show = ppShow
104
105joinRange :: Range -> Range -> Range 102joinRange :: Range -> Range -> Range
106joinRange (Range n b e) (Range n' b' e') {- | n == n' -} = Range n (min b b') (max e e') 103joinRange (Range n b e) (Range n' b' e') {- | n == n' -} = Range n (min b b') (max e e')
107 104
@@ -142,7 +139,9 @@ debugSI a = NoSI (Set.singleton a)
142si@(RangeSI r) `validate` xs | r `notElem` [r | RangeSI r <- xs] = si 139si@(RangeSI r) `validate` xs | r `notElem` [r | RangeSI r <- xs] = si
143_ `validate` _ = mempty 140_ `validate` _ = mempty
144 141
145data SIName = SIName_ SI (Maybe Fixity) SName 142-------------------------------------------------------------------------------- name with source info
143
144data SIName = SIName_ { siName :: SI, getFixity :: Maybe Fixity, sName :: SName }
146 145
147pattern SIName si n <- SIName_ si _ n 146pattern SIName si n <- SIName_ si _ n
148 where SIName si n = SIName_ si Nothing n 147 where SIName si n = SIName_ si Nothing n
@@ -156,13 +155,6 @@ instance PShow SIName
156 NoSI{} -> text n 155 NoSI{} -> text n
157 _ -> pShow si 156 _ -> pShow si
158 157
159sName (SIName _ s) = s
160
161--appName f (SIName si n) = SIName si $ f n
162
163getFixity_ (SIName_ _ f _) = f
164--getFixity (SIName_ _ f _) = fromMaybe (InfixL 9) f
165
166------------- 158-------------
167 159
168class SourceInfo a where 160class SourceInfo a where
@@ -192,12 +184,12 @@ data Lit
192 | LString String 184 | LString String
193 deriving (Eq) 185 deriving (Eq)
194 186
195instance Show Lit where 187instance PShow Lit where
196 show = \case 188 pShow = \case
197 LFloat x -> show x 189 LFloat x -> pShow x
198 LString x -> show x 190 LString x -> text $ show x
199 LInt x -> show x 191 LInt x -> pShow x
200 LChar x -> show x 192 LChar x -> pShow x
201 193
202-------------------------------------------------------------------------------- expression 194-------------------------------------------------------------------------------- expression
203 195
@@ -209,7 +201,7 @@ data SExp' a
209 | SVar_ (SData SIName) !Int 201 | SVar_ (SData SIName) !Int
210 | SLet_ SI (SData SIName) (SExp' a) (SExp' a) -- let x = e in f --> SLet e f{-x is Var 0-} 202 | SLet_ SI (SData SIName) (SExp' a) (SExp' a) -- let x = e in f --> SLet e f{-x is Var 0-}
211 | STyped a 203 | STyped a
212 deriving (Eq, Show) 204 deriving (Eq)
213 205
214type SExp = SExp' Void 206type SExp = SExp' Void
215 207
@@ -385,25 +377,22 @@ trSExp f = g where
385trSExp' :: SExp -> SExp' a 377trSExp' :: SExp -> SExp' a
386trSExp' = trSExp elimVoid 378trSExp' = trSExp elimVoid
387 379
388instance Up a => PShow (SExp' a) where 380instance (Up a, PShow a) => PShow (SExp' a) where
389 pShow = sExpDoc 381 pShow = \case
390 382 SGlobal ns -> pShow ns
391sExpDoc :: Up a => SExp' a -> Doc 383 SAnn a b -> shAnn False (pShow a) (pShow b)
392sExpDoc = \case 384 TyType a -> text "tyType" `DApp` pShow a
393 SGlobal ns -> text $ sName ns 385 SGlobal op `SAppV` a `SAppV` b | Just p <- getFixity op -> DOp (sName op) p (pShow a) (pShow b)
394 SAnn a b -> shAnn False (sExpDoc a) (sExpDoc b) 386 SApp h a b -> shApp h (pShow a) (pShow b)
395 TyType a -> shApp Visible (text "tyType") (sExpDoc a) 387 Wildcard t -> shAnn True (text "_") (pShow t)
396 SGlobal op `SAppV` a `SAppV` b | Just p <- getFixity_ op -> DOp (sName op) p (pShow a) (pShow b) 388 SBind_ _ h _ a b -> shLam (usedVar 0 b) h (pShow a) (pShow b)
397 SApp h a b -> shApp h (sExpDoc a) (sExpDoc b) 389 SLet _ a b -> shLet_ (pShow a) (pShow b)
398 Wildcard t -> shAnn True (text "_") (sExpDoc t) 390 STyped a -> pShow a
399 SBind_ _ h _ a b -> shLam (usedVar 0 b) h (sExpDoc a) (sExpDoc b) 391 SVar _ i -> shVar i
400 SLet _ a b -> shLet_ (sExpDoc a) (sExpDoc b) 392 SLit _ l -> pShow l
401 STyped _{-(e,t)-} -> text "<<>>" -- todo: expDoc e
402 SVar _ i -> shVar i
403 SLit _ l -> text $ show l
404 393
405shApp Visible a b = DApp a b 394shApp Visible a b = DApp a b
406shApp Hidden a b = DApp a (DGlue (InfixR 20) "@" b) 395shApp Hidden a b = DApp a (DAt b)
407 396
408shLam True (BPi Hidden) a b = DFreshName True $ showForall (shAnn True (DVar 0) $ DUp 0 a) b 397shLam True (BPi Hidden) a b = DFreshName True $ showForall (shAnn True (DVar 0) $ DUp 0 a) b
409shLam False (BPi Hidden) a b = showContext a $ DFreshName False b 398shLam False (BPi Hidden) a b = showContext a $ DFreshName False b
@@ -418,7 +407,7 @@ shLam usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 a) b
418 BLam h -> vpar h 407 BLam h -> vpar h
419 BPi h -> vpar h 408 BPi h -> vpar h
420 409
421 vpar Hidden = (\p -> DBrace p) . shAnn True (green $ DVar 0) 410 vpar Hidden = DAt . ann (green $ DVar 0)
422 vpar Visible = ann (green $ DVar 0) 411 vpar Visible = ann (green $ DVar 0)
423 412
424 ann | usedVar = shAnn False 413 ann | usedVar = shAnn False
@@ -432,7 +421,6 @@ data Stmt
432 = Let SIName (Maybe SExp) SExp 421 = Let SIName (Maybe SExp) SExp
433 | Data SIName [(Visibility, SExp)]{-parameters-} SExp{-type-} [(SIName, SExp)]{-constructor names and types-} 422 | Data SIName [(Visibility, SExp)]{-parameters-} SExp{-type-} [(SIName, SExp)]{-constructor names and types-}
434 | PrecDef SIName Fixity 423 | PrecDef SIName Fixity
435 deriving (Show)
436 424
437pattern Primitive n t = Let n (Just t) (SBuiltin "undefined") 425pattern Primitive n t = Let n (Just t) (SBuiltin "undefined")
438 426
@@ -445,7 +433,7 @@ instance PShow Stmt where
445instance DeBruijnify SIName Stmt where 433instance DeBruijnify SIName Stmt where
446 deBruijnify_ k v = \case 434 deBruijnify_ k v = \case
447 Let sn mt e -> Let sn (deBruijnify_ k v <$> mt) (deBruijnify_ k v e) 435 Let sn mt e -> Let sn (deBruijnify_ k v <$> mt) (deBruijnify_ k v e)
448 x -> error $ "deBruijnify @ " ++ show x 436 x -> error $ "deBruijnify @ " ++ ppShow x
449 437
450-------------------------------------------------------------------------------- statement with dependencies 438-------------------------------------------------------------------------------- statement with dependencies
451 439
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs
index ef490fc7..ad7eeae6 100644
--- a/src/LambdaCube/Compiler/Infer.hs
+++ b/src/LambdaCube/Compiler/Infer.hs
@@ -61,7 +61,6 @@ data Exp
61 | Pi_ !MaxDB Visibility Exp Exp 61 | Pi_ !MaxDB Visibility Exp Exp
62 | Lam_ !MaxDB Exp 62 | Lam_ !MaxDB Exp
63 | Neut Neutral 63 | Neut Neutral
64 deriving (Show)
65 64
66data Freq = CompileTime | RunTime 65data Freq = CompileTime | RunTime
67 deriving (Eq, Show) 66 deriving (Eq, Show)
@@ -78,7 +77,6 @@ data Neutral
78 | Var_ !Int -- De Bruijn variable 77 | Var_ !Int -- De Bruijn variable
79 | LabelEnd_ Exp -- not neut? 78 | LabelEnd_ Exp -- not neut?
80 | Delta (SData ([Exp] -> Exp)) -- not neut? 79 | Delta (SData ([Exp] -> Exp)) -- not neut?
81 deriving (Show)
82 80
83data ConName = ConName FName Int{-ordinal number, e.g. Zero:0, Succ:1-} Type 81data ConName = ConName FName Int{-ordinal number, e.g. Zero:0, Succ:1-} Type
84 82
@@ -95,14 +93,19 @@ type ExpType = (Exp, Type)
95type SExp2 = SExp' ExpType 93type SExp2 = SExp' ExpType
96 94
97instance Show ConName where show (ConName n _ _) = show n 95instance Show ConName where show (ConName n _ _) = show n
96instance PShow ConName where pShow (ConName n _ _) = pShow n
98instance Eq ConName where ConName _ n _ == ConName _ n' _ = n == n' 97instance Eq ConName where ConName _ n _ == ConName _ n' _ = n == n'
99instance Show TyConName where show (TyConName n _ _ _ _) = show n 98instance Show TyConName where show (TyConName n _ _ _ _) = show n
99instance PShow TyConName where pShow (TyConName n _ _ _ _) = pShow n
100instance Eq TyConName where TyConName n _ _ _ _ == TyConName n' _ _ _ _ = n == n' 100instance Eq TyConName where TyConName n _ _ _ _ == TyConName n' _ _ _ _ = n == n'
101instance Show FunName where show (FunName n _ _) = show n 101instance Show FunName where show (FunName n _ _) = show n
102instance PShow FunName where pShow (FunName n _ _) = pShow n
102instance Eq FunName where FunName n _ _ == FunName n' _ _ = n == n' 103instance Eq FunName where FunName n _ _ == FunName n' _ _ = n == n'
103instance Show CaseFunName where show (CaseFunName n _ _) = CaseName $ show n 104instance Show CaseFunName where show (CaseFunName n _ _) = CaseName $ show n
105instance PShow CaseFunName where pShow (CaseFunName n _ _) = text $ CaseName $ ppShow n
104instance Eq CaseFunName where CaseFunName n _ _ == CaseFunName n' _ _ = n == n' 106instance Eq CaseFunName where CaseFunName n _ _ == CaseFunName n' _ _ = n == n'
105instance Show TyCaseFunName where show (TyCaseFunName n _) = MatchName $ show n 107instance Show TyCaseFunName where show (TyCaseFunName n _) = MatchName $ show n
108instance PShow TyCaseFunName where pShow (TyCaseFunName n _) = text $ MatchName $ ppShow n
106instance Eq TyCaseFunName where TyCaseFunName n _ == TyCaseFunName n' _ = n == n' 109instance Eq TyCaseFunName where TyCaseFunName n _ == TyCaseFunName n' _ = n == n'
107 110
108data FName 111data FName
@@ -195,7 +198,10 @@ fntable =
195 198
196instance Show FName where 199instance Show FName where
197 show (CFName _ (SData s)) = s 200 show (CFName _ (SData s)) = s
198 show s = fromMaybe (error "show") $ lookup s $ map (\(a, b) -> (b, a)) fntable 201 show s = maybe (error "show") id $ lookup s $ map (\(a, b) -> (b, a)) fntable
202instance PShow FName where
203 pShow (CFName _ (SData s)) = text s
204 pShow s = maybe (error "show") text $ lookup s $ map (\(a, b) -> (b, a)) fntable
199 205
200-------------------------------------------------------------------------------- auxiliary functions and patterns 206-------------------------------------------------------------------------------- auxiliary functions and patterns
201 207
@@ -328,7 +334,7 @@ pattern LabelEnd x = Neut (LabelEnd_ x)
328--pmLabel' :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp 334--pmLabel' :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp
329pmLabel' _ (FunName _ _ _) _ 0 as (Neut (Delta (SData f))) = f $ reverse as 335pmLabel' _ (FunName _ _ _) _ 0 as (Neut (Delta (SData f))) = f $ reverse as
330pmLabel' md f vs i xs (unfixlabel -> Neut y) = Neut $ Fun_ md f vs i xs y 336pmLabel' md f vs i xs (unfixlabel -> Neut y) = Neut $ Fun_ md f vs i xs y
331pmLabel' _ f _ i xs y = error $ "pmLabel: " ++ show (f, i, length xs, y) 337pmLabel' _ f _ i xs y = error $ "pmLabel: " ++ ppShow f --show (f, i, length xs, y)
332 338
333pmLabel :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp 339pmLabel :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp
334pmLabel f vs i xs e = pmLabel' (foldMap maxDB_ vs <> foldMap maxDB_ xs) f vs (i + numLams e) xs (Neut $ dropLams e) 340pmLabel f vs i xs e = pmLabel' (foldMap maxDB_ vs <> foldMap maxDB_ xs) f vs (i + numLams e) xs (Neut $ dropLams e)
@@ -536,11 +542,11 @@ evalCaseFun a ps (Con n@(ConName _ i _) _ vs)
536 | i /= (-1) = foldl app_ (ps !!! (i + 1)) vs 542 | i /= (-1) = foldl app_ (ps !!! (i + 1)) vs
537 | otherwise = error "evcf" 543 | otherwise = error "evcf"
538 where 544 where
539 xs !!! i | i >= length xs = error $ "!!! " ++ show a ++ " " ++ show i ++ " " ++ show n ++ "\n" ++ ppShow ps 545 xs !!! i | i >= length xs = error $ "!!! " ++ ppShow a ++ " " ++ show i ++ " " ++ ppShow n ++ "\n" ++ ppShow ps
540 xs !!! i = xs !! i 546 xs !!! i = xs !! i
541evalCaseFun a b (FL c) = evalCaseFun a b c 547evalCaseFun a b (FL c) = evalCaseFun a b c
542evalCaseFun a b (Neut c) = CaseFun a b c 548evalCaseFun a b (Neut c) = CaseFun a b c
543evalCaseFun a b x = error $ "evalCaseFun: " ++ show (a, x) 549evalCaseFun a b x = error $ "evalCaseFun: " ++ ppShow (a, x)
544 550
545evalTyCaseFun a b (FL c) = evalTyCaseFun a b c 551evalTyCaseFun a b (FL c) = evalTyCaseFun a b c
546evalTyCaseFun a b (Neut c) = TyCaseFun a b c 552evalTyCaseFun a b (Neut c) = TyCaseFun a b c
@@ -705,7 +711,7 @@ data CEnv a
705 = MEnd a 711 = MEnd a
706 | Meta Exp (CEnv a) 712 | Meta Exp (CEnv a)
707 | Assign !Int ExpType (CEnv a) -- De Bruijn index decreasing assign reservedOp, only for metavariables (non-recursive) 713 | Assign !Int ExpType (CEnv a) -- De Bruijn index decreasing assign reservedOp, only for metavariables (non-recursive)
708 deriving (Show, Functor) 714 deriving (Functor)
709 715
710instance (Subst Exp a, Up a) => Up (CEnv a) where 716instance (Subst Exp a, Up a) => Up (CEnv a) where
711 usedVar i a = error "usedVar @(CEnv _)" 717 usedVar i a = error "usedVar @(CEnv _)"
@@ -822,7 +828,6 @@ data Env
822 | CheckIType SExp2 Env 828 | CheckIType SExp2 Env
823-- | CheckSame Exp Env 829-- | CheckSame Exp Env
824 | CheckAppType SI Visibility Type Env SExp2 --pattern CheckAppType _ h t te b = EApp1 _ h (CheckType t te) b 830 | CheckAppType SI Visibility Type Env SExp2 --pattern CheckAppType _ h t te b = EApp1 _ h (CheckType t te) b
825 deriving Show
826 831
827pattern EBind2 b e env <- EBind2_ _ b e env where EBind2 b e env = EBind2_ (debugSI "6") b e env 832pattern EBind2 b e env <- EBind2_ _ b e env where EBind2 b e env = EBind2_ (debugSI "6") b e env
828pattern CheckType e env <- CheckType_ _ e env where CheckType e env = CheckType_ (debugSI "7") e env 833pattern CheckType e env <- CheckType_ _ e env where CheckType e env = CheckType_ (debugSI "7") e env
@@ -877,7 +882,7 @@ mkExpTypes t [] = []
877mkExpTypes t@(Pi _ a _) (x: xs) = (x, t): mkExpTypes (appTy t x) xs 882mkExpTypes t@(Pi _ a _) (x: xs) = (x, t): mkExpTypes (appTy t x) xs
878 883
879appTy (Pi _ a b) x = subst 0 x b 884appTy (Pi _ a b) x = subst 0 x b
880appTy t x = error $ "appTy: " ++ show t 885appTy t x = error $ "appTy: " ++ ppShow t
881 886
882-------------------------------------------------------------------------------- error messages 887-------------------------------------------------------------------------------- error messages
883 888
@@ -981,7 +986,7 @@ inferN_ tellTrace = infer where
981 | SLam h a b <- e, Pi h' x y <- t, h == h' = do 986 | SLam h a b <- e, Pi h' x y <- t, h == h' = do
982 tellType e t 987 tellType e t
983 let same = checkSame te a x 988 let same = checkSame te a x
984 if same then checkN (EBind2 (BLam h) x te) b y else error $ "checkSame:\n" ++ show a ++ "\nwith\n" ++ showEnvExp te (x, TType) 989 if same then checkN (EBind2 (BLam h) x te) b y else error $ "checkSame:\n" ++ ppShow a ++ "\nwith\n" ++ showEnvExp te (x, TType)
985 | Pi Hidden a b <- t = do 990 | Pi Hidden a b <- t = do
986 bb <- notHiddenLam e 991 bb <- notHiddenLam e
987 if bb then checkN (EBind2 (BLam Hidden) a te) (up1 e) b 992 if bb then checkN (EBind2 (BLam Hidden) a te) (up1 e) b
@@ -1008,7 +1013,7 @@ inferN_ tellTrace = infer where
1008 checkSame te (SGlobal (sName -> "'Type")) TType = True 1013 checkSame te (SGlobal (sName -> "'Type")) TType = True
1009 checkSame te SType TType = True 1014 checkSame te SType TType = True
1010 checkSame te (SBind_ _ BMeta _ SType (STyped (Var 0, _))) a = True 1015 checkSame te (SBind_ _ BMeta _ SType (STyped (Var 0, _))) a = True
1011 checkSame te a b = error $ "checkSame: " ++ show (a, b) 1016 checkSame te a b = error $ "checkSame: " ++ ppShow (a, b)
1012 1017
1013 hArgs (Pi Hidden _ b) = 1 + hArgs b 1018 hArgs (Pi Hidden _ b) = 1 + hArgs b
1014 hArgs _ = 0 1019 hArgs _ = 0
@@ -1177,10 +1182,10 @@ recheck' msg' e (x, xt) = (recheck_ "main" (checkEnv e) (x, xt), xt)
1177 (Neut (App__ md a b), zt) 1182 (Neut (App__ md a b), zt)
1178 | (Neut a', at) <- recheck'' "app1" te (Neut a, neutType te a) 1183 | (Neut a', at) <- recheck'' "app1" te (Neut a, neutType te a)
1179 -> checkApps "a" [] zt (Neut . App__ md a' . head) te at [b] 1184 -> checkApps "a" [] zt (Neut . App__ md a' . head) te at [b]
1180 (Con_ md s n as, zt) -> checkApps (show s) [] zt (Con_ md s n . drop (conParams s)) te (conType zt s) $ mkConPars n zt ++ as 1185 (Con_ md s n as, zt) -> checkApps (ppShow s) [] zt (Con_ md s n . drop (conParams s)) te (conType zt s) $ mkConPars n zt ++ as
1181 (TyCon_ md s as, zt) -> checkApps (show s) [] zt (TyCon_ md s) te (nType s) as 1186 (TyCon_ md s as, zt) -> checkApps (ppShow s) [] zt (TyCon_ md s) te (nType s) as
1182 (CaseFun s@(CaseFunName _ t pars) as n, zt) -> checkApps (show s) [] zt (\xs -> evalCaseFun s (init $ drop pars xs) (last xs)) te (nType s) (makeCaseFunPars te n ++ as ++ [Neut n]) 1187 (CaseFun s@(CaseFunName _ t pars) as n, zt) -> checkApps (ppShow s) [] zt (\xs -> evalCaseFun s (init $ drop pars xs) (last xs)) te (nType s) (makeCaseFunPars te n ++ as ++ [Neut n])
1183 (TyCaseFun s [m, t, f] n, zt) -> checkApps (show s) [] zt (\[m, t, n, f] -> evalTyCaseFun s [m, t, f] n) te (nType s) [m, t, Neut n, f] 1188 (TyCaseFun s [m, t, f] n, zt) -> checkApps (ppShow s) [] zt (\[m, t, n, f] -> evalTyCaseFun s [m, t, f] n) te (nType s) [m, t, Neut n, f]
1184 (Neut (Fun_ md f vs@[] i a x), zt) -> checkApps "lab" [] zt (\xs -> Neut $ Fun_ md f vs i (reverse xs) x) te (nType f) $ reverse a -- TODO: recheck x 1189 (Neut (Fun_ md f vs@[] i a x), zt) -> checkApps "lab" [] zt (\xs -> Neut $ Fun_ md f vs i (reverse xs) x) te (nType f) $ reverse a -- TODO: recheck x
1185 -- TODO 1190 -- TODO
1186 (r@(Neut (Fun' f vs i a x)), zt) -> r 1191 (r@(Neut (Fun' f vs i a x)), zt) -> r
@@ -1200,7 +1205,7 @@ recheck' msg' e (x, xt) = (recheck_ "main" (checkEnv e) (x, xt), xt)
1200ambiguityCheck :: String -> Exp -> Maybe String 1205ambiguityCheck :: String -> Exp -> Maybe String
1201ambiguityCheck s ty = case ambigVars ty of 1206ambiguityCheck s ty = case ambigVars ty of
1202 [] -> Nothing 1207 [] -> Nothing
1203 err -> Just $ s ++ " has ambiguous type:\n" ++ ppShow ty ++ "\nproblematic vars:\n" ++ show err 1208 err -> Just $ s ++ " has ambiguous type:\n" ++ ppShow ty ++ "\nproblematic vars:\n" ++ ppShow err
1204 1209
1205ambigVars :: Exp -> [(Int, Exp)] 1210ambigVars :: Exp -> [(Int, Exp)]
1206ambigVars ty = [(n, c) | (n, c) <- hid, not $ any (`Set.member` defined) $ Set.insert n $ free c] 1211ambigVars ty = [(n, c) | (n, c) <- hid, not $ any (`Set.member` defined) $ Set.insert n $ free c]
@@ -1383,7 +1388,7 @@ handleStmt = \case
1383 e3 <- addToEnv (SIName (sourceInfo s) $ MatchName (sName s)) (lamify t $ \[m, tr, n, f] -> evalTyCaseFun (TyCaseFunName sint t) [m, tr, f] n, t) 1388 e3 <- addToEnv (SIName (sourceInfo s) $ MatchName (sName s)) (lamify t $ \[m, tr, n, f] -> evalTyCaseFun (TyCaseFunName sint t) [m, tr, f] n, t)
1384 return (e1 <> e2 <> e3 <> es) 1389 return (e1 <> e2 <> e3 <> es)
1385 1390
1386 stmt -> error $ "handleStmt: " ++ show stmt 1391 stmt -> error $ "handleStmt: " ++ ppShow stmt
1387 1392
1388withEnv e = local $ second (<> e) 1393withEnv e = local $ second (<> e)
1389 1394
@@ -1464,21 +1469,21 @@ instance PShow Env where
1464showEnvExp :: Env -> ExpType -> String 1469showEnvExp :: Env -> ExpType -> String
1465showEnvExp e c = show $ envDoc e $ underline $ mkDoc False False c 1470showEnvExp e c = show $ envDoc e $ underline $ mkDoc False False c
1466 1471
1467showEnvSExp :: Up a => Env -> SExp' a -> String 1472showEnvSExp :: (PShow a, Up a) => Env -> SExp' a -> String
1468showEnvSExp e c = show $ envDoc e $ underline $ sExpDoc c 1473showEnvSExp e c = show $ envDoc e $ underline $ pShow c
1469 1474
1470showEnvSExpType :: Up a => Env -> SExp' a -> Exp -> String 1475showEnvSExpType :: (PShow a, Up a) => Env -> SExp' a -> Exp -> String
1471showEnvSExpType e c t = show $ envDoc e $ underline $ (shAnn False (sExpDoc c) (mkDoc False False (t, TType))) 1476showEnvSExpType e c t = show $ envDoc e $ underline $ (shAnn False (pShow c) (mkDoc False False (t, TType)))
1472 1477
1473envDoc :: Env -> Doc -> Doc 1478envDoc :: Env -> Doc -> Doc
1474envDoc x m = case x of 1479envDoc x m = case x of
1475 EGlobal{} -> m 1480 EGlobal{} -> m
1476 EBind1 _ h ts b -> envDoc ts $ shLam (usedVar 0 b) h m (sExpDoc b) 1481 EBind1 _ h ts b -> envDoc ts $ shLam (usedVar 0 b) h m (pShow b)
1477 EBind2 h a ts -> envDoc ts $ shLam True h (mkDoc False ts' (a, TType)) m 1482 EBind2 h a ts -> envDoc ts $ shLam True h (mkDoc False ts' (a, TType)) m
1478 EApp1 _ h ts b -> envDoc ts $ shApp h m (sExpDoc b) 1483 EApp1 _ h ts b -> envDoc ts $ shApp h m (pShow b)
1479 EApp2 _ h (Lam (Var 0), Pi Visible TType _) ts -> envDoc ts $ shApp h (text "tyType") m 1484 EApp2 _ h (Lam (Var 0), Pi Visible TType _) ts -> envDoc ts $ shApp h (text "tyType") m
1480 EApp2 _ h a ts -> envDoc ts $ shApp h (mkDoc False ts' a) m 1485 EApp2 _ h a ts -> envDoc ts $ shApp h (mkDoc False ts' a) m
1481 ELet1 _ ts b -> envDoc ts $ shLet_ m (sExpDoc b) 1486 ELet1 _ ts b -> envDoc ts $ shLet_ m (pShow b)
1482 ELet2 _ x ts -> envDoc ts $ shLet_ (mkDoc False ts' x) m 1487 ELet2 _ x ts -> envDoc ts $ shLet_ (mkDoc False ts' x) m
1483 EAssign i x ts -> envDoc ts $ shLet i (mkDoc False ts' x) m 1488 EAssign i x ts -> envDoc ts $ shLet i (mkDoc False ts' x) m
1484 CheckType t ts -> envDoc ts $ shAnn False m $ mkDoc False ts' (t, TType) 1489 CheckType t ts -> envDoc ts $ shAnn False m $ mkDoc False ts' (t, TType)
@@ -1486,7 +1491,7 @@ envDoc x m = case x of
1486-- CheckSame t ts -> envDoc ts $ shCstr <$> m <*> mkDoc ts' t 1491-- CheckSame t ts -> envDoc ts $ shCstr <$> m <*> mkDoc ts' t
1487 CheckAppType si h t te b -> envDoc (EApp1 si h (CheckType_ (sourceInfo b) t te) b) m 1492 CheckAppType si h t te b -> envDoc (EApp1 si h (CheckType_ (sourceInfo b) t te) b) m
1488 ELabelEnd ts -> envDoc ts $ shApp Visible (text "labEnd") m 1493 ELabelEnd ts -> envDoc ts $ shApp Visible (text "labEnd") m
1489 x -> error $ "envDoc: " ++ show x 1494 x -> error $ "envDoc: " ++ ppShow x
1490 where 1495 where
1491 ts' = False 1496 ts' = False
1492 1497
@@ -1503,13 +1508,13 @@ instance MkDoc Exp where
1503-- Lam h a b -> join $ shLam (usedVar 0 b) (BLam h) <$> f a <*> pure (f b) 1508-- Lam h a b -> join $ shLam (usedVar 0 b) (BLam h) <$> f a <*> pure (f b)
1504 Lam b -> shLam True (BLam Visible) (f TType{-todo!-}) (f b) 1509 Lam b -> shLam True (BLam Visible) (f TType{-todo!-}) (f b)
1505 Pi h a b -> shLam (usedVar 0 b) (BPi h) (f a) (f b) 1510 Pi h a b -> shLam (usedVar 0 b) (BPi h) (f a) (f b)
1506 ENat' n -> text $ show n 1511 ENat' n -> text $ ppShow n
1507 (getTTup -> Just xs) -> shTuple $ f <$> xs 1512 (getTTup -> Just xs) -> shTuple $ f <$> xs
1508 (getTup -> Just xs) -> shTuple $ f <$> xs 1513 (getTup -> Just xs) -> shTuple $ f <$> xs
1509 Con s _ xs -> foldl (shApp Visible) (text_ $ show s) (f <$> xs) 1514 Con s _ xs -> foldl (shApp Visible) (text_ $ ppShow s) (f <$> xs)
1510 TyConN s xs -> foldl (shApp Visible) (text_ $ show s) (f <$> xs) 1515 TyConN s xs -> foldl (shApp Visible) (text_ $ ppShow s) (f <$> xs)
1511 TType -> text "Type" 1516 TType -> text "Type"
1512 ELit l -> text $ show l 1517 ELit l -> pShow l
1513 Neut x -> mkDoc pr ts x 1518 Neut x -> mkDoc pr ts x
1514 1519
1515 text_ = text . if ts then switchTick else id 1520 text_ = text . if ts then switchTick else id
@@ -1521,11 +1526,11 @@ instance MkDoc Neutral where
1521 f = \case 1526 f = \case
1522 CstrT' t a b -> shCstr (g (a, t)) (g (b, t)) 1527 CstrT' t a b -> shCstr (g (a, t)) (g (b, t))
1523 FL' a | pr -> g a 1528 FL' a | pr -> g a
1524 Fun' s vs i (mkExpTypes (nType s) . reverse -> xs) _ -> foldl (shApp Visible) (text_ $ show s) (g <$> xs) 1529 Fun' s vs i (mkExpTypes (nType s) . reverse -> xs) _ -> foldl (shApp Visible) (text_ $ ppShow s) (g <$> xs)
1525 Var_ k -> shVar k 1530 Var_ k -> shVar k
1526 App_ a b -> shApp Visible (g a) (g b) 1531 App_ a b -> shApp Visible (g a) (g b)
1527 CaseFun_ s xs n -> foldl (shApp Visible) (text_ $ show s) (map g $ {-mkExpTypes (nType s) $ makeCaseFunPars te n ++ -} xs ++ [Neut n]) 1532 CaseFun_ s xs n -> foldl (shApp Visible) (text_ $ ppShow s) (map g $ {-mkExpTypes (nType s) $ makeCaseFunPars te n ++ -} xs ++ [Neut n])
1528 TyCaseFun_ s [m, t, f] n -> foldl (shApp Visible) (text_ $ show s) (g <$> mkExpTypes (nType s) [m, t, Neut n, f]) 1533 TyCaseFun_ s [m, t, f] n -> foldl (shApp Visible) (text_ $ ppShow s) (g <$> mkExpTypes (nType s) [m, t, Neut n, f])
1529 TyCaseFun_ s _ n -> error $ "mkDoc TyCaseFun" 1534 TyCaseFun_ s _ n -> error $ "mkDoc TyCaseFun"
1530 LabelEnd_ x -> shApp Visible (text "labend") (g x) 1535 LabelEnd_ x -> shApp Visible (text "labend") (g x)
1531 Delta{} -> text "^delta" 1536 Delta{} -> text "^delta"
diff --git a/src/LambdaCube/Compiler/Lexer.hs b/src/LambdaCube/Compiler/Lexer.hs
index fcbd356d..46c44086 100644
--- a/src/LambdaCube/Compiler/Lexer.hs
+++ b/src/LambdaCube/Compiler/Lexer.hs
@@ -22,7 +22,6 @@ import Control.Applicative
22 22
23import Text.Megaparsec hiding (State) 23import Text.Megaparsec hiding (State)
24import qualified Text.Megaparsec as P 24import qualified Text.Megaparsec as P
25import qualified Text.Megaparsec.Prim as P
26import Text.Megaparsec as ParseUtils hiding (try, Message, State) 25import Text.Megaparsec as ParseUtils hiding (try, Message, State)
27import Text.Megaparsec.Lexer hiding (lexeme, symbol, negate) 26import Text.Megaparsec.Lexer hiding (lexeme, symbol, negate)
28 27
@@ -35,22 +34,6 @@ import LambdaCube.Compiler.DesugaredSource
35-- see http://blog.ezyang.com/2014/05/parsec-try-a-or-b-considered-harmful/comment-page-1/#comment-6602 34-- see http://blog.ezyang.com/2014/05/parsec-try-a-or-b-considered-harmful/comment-page-1/#comment-6602
36try_ s m = try m <?> s 35try_ s m = try m <?> s
37 36
38instance (Monoid w, P.MonadParsec st m t) => P.MonadParsec st (RWST r w s m) t where
39 failure = lift . failure
40 label n (RWST m) = RWST $ \r s -> label n $ m r s
41 try (RWST m) = RWST $ \r s -> try $ m r s
42 lookAhead (RWST m) = RWST $ \r s ->
43 (\(a, _, _) -> (a, s, mempty)) <$> lookAhead (m r s)
44 notFollowedBy (RWST m) = RWST $ \r s ->
45 notFollowedBy ((\(a, _, _) -> a) <$> m r s) >> return ((), s, mempty)
46 withRecovery rec (RWST m) = RWST $ \r s ->
47 withRecovery (\e -> runRWST (rec e) r s) (m r s)
48 eof = lift eof
49 token f e = lift $ token f e
50 tokens f e ts = lift $ tokens f e ts
51 getParserState = lift getParserState
52 updateParserState f = lift $ updateParserState f
53
54toSPos :: SourcePos -> SPos 37toSPos :: SourcePos -> SPos
55toSPos p = SPos (sourceLine p) (sourceColumn p) 38toSPos p = SPos (sourceLine p) (sourceColumn p)
56 39
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs
index bc2cbbd4..633368f8 100644
--- a/src/LambdaCube/Compiler/Parser.hs
+++ b/src/LambdaCube/Compiler/Parser.hs
@@ -71,7 +71,7 @@ instance PShow LCParseError where
71 pShow = \case 71 pShow = \case
72 MultiplePatternVars xs -> vcat $ "multiple pattern vars:": 72 MultiplePatternVars xs -> vcat $ "multiple pattern vars:":
73 concat [(shortForm (pShow $ head ns) <+> "is defined at"): map pShow ns | ns <- xs] 73 concat [(shortForm (pShow $ head ns) <+> "is defined at"): map pShow ns | ns <- xs]
74 OperatorMismatch op op' -> "Operator precedences don't match:" <$$> pShow (fromJust $ getFixity_ op) <+> "at" <+> pShow op <$$> pShow (fromJust $ getFixity_ op') <+> "at" <+> pShow op' 74 OperatorMismatch op op' -> "Operator precedences don't match:" <$$> pShow (fromJust $ getFixity op) <+> "at" <+> pShow op <$$> pShow (fromJust $ getFixity op') <+> "at" <+> pShow op'
75 UndefinedConstructor n -> "Constructor" <+> shortForm (pShow n) <+> "is not defined at" <+> pShow n 75 UndefinedConstructor n -> "Constructor" <+> shortForm (pShow n) <+> "is not defined at" <+> pShow n
76 ParseError p -> text $ show p 76 ParseError p -> text $ show p
77 77
@@ -275,7 +275,7 @@ calculatePrecs = go where
275 waitOp lsec e acc [] = calcPrec' e acc 275 waitOp lsec e acc [] = calcPrec' e acc
276 waitOp lsec e acc _ = error "impossible @ Parser 488" 276 waitOp lsec e acc _ = error "impossible @ Parser 488"
277 277
278 calcPrec' e = postponedCheck id . calcPrec (\op x y -> SGlobal op `SAppV` x `SAppV` y) (fromJust . getFixity_) e . reverse 278 calcPrec' e = postponedCheck id . calcPrec (\op x y -> SGlobal op `SAppV` x `SAppV` y) (fromJust . getFixity) e . reverse
279 279
280generator, letdecl, boolExpression :: BodyParser (SExp -> ErrorFinder SExp) 280generator, letdecl, boolExpression :: BodyParser (SExp -> ErrorFinder SExp)
281generator = do 281generator = do
@@ -354,7 +354,7 @@ mkTup ps = foldr cHCons cHNil ps
354patType p (Wildcard SType) = p 354patType p (Wildcard SType) = p
355patType p t = PatTypeSimp p t 355patType p t = PatTypeSimp p t
356 356
357calculatePatPrecs e xs = postponedCheck fst $ calcPrec (\op x y -> PConSimp op [x, y]) (fromJust . getFixity_ . fst) e xs 357calculatePatPrecs e xs = postponedCheck fst $ calcPrec (\op x y -> PConSimp op [x, y]) (fromJust . getFixity . fst) e xs
358 358
359longPattern = setR parsePatAnn <&> (reverse . getPVars &&& id) 359longPattern = setR parsePatAnn <&> (reverse . getPVars &&& id)
360 360
diff --git a/src/LambdaCube/Compiler/Patterns.hs b/src/LambdaCube/Compiler/Patterns.hs
index 9a986fd6..a2bc3831 100644
--- a/src/LambdaCube/Compiler/Patterns.hs
+++ b/src/LambdaCube/Compiler/Patterns.hs
@@ -42,13 +42,11 @@ data Pat_ c
42 | PCon_ SI (SIName, c) [ParPat_ c] 42 | PCon_ SI (SIName, c) [ParPat_ c]
43 | ViewPat_ SI SExp (ParPat_ c) 43 | ViewPat_ SI SExp (ParPat_ c)
44 | PatType_ SI (ParPat_ c) SExp 44 | PatType_ SI (ParPat_ c) SExp
45 deriving Show
46 45
47type ParPat = ParPat_ ConsInfo 46type ParPat = ParPat_ ConsInfo
48 47
49-- parallel patterns like v@(f -> [])@(Just x) 48-- parallel patterns like v@(f -> [])@(Just x)
50data ParPat_ c = ParPat_ SI [Pat_ c] 49data ParPat_ c = ParPat_ SI [Pat_ c]
51 deriving Show
52 50
53pattern ParPat ps <- ParPat_ _ ps 51pattern ParPat ps <- ParPat_ _ ps
54 where ParPat ps = ParPat_ (foldMap sourceInfo ps) ps 52 where ParPat ps = ParPat_ (foldMap sourceInfo ps) ps
@@ -198,7 +196,6 @@ data Lets a
198 = LLet SIName SExp (Lets a) 196 = LLet SIName SExp (Lets a)
199 | LTypeAnn SExp (Lets a) -- TODO: eliminate if not used 197 | LTypeAnn SExp (Lets a) -- TODO: eliminate if not used
200 | In a 198 | In a
201 deriving Show
202 199
203lLet sn (SVar sn' i) l = rSubst 0 i l 200lLet sn (SVar sn' i) l = rSubst 0 i l
204lLet sn e l = LLet sn e l 201lLet sn e l = LLet sn e l
@@ -223,7 +220,6 @@ data GuardTree
223 = GuardNode SExp (SIName, ConsInfo) [SIName] GuardTrees GuardTrees 220 = GuardNode SExp (SIName, ConsInfo) [SIName] GuardTrees GuardTrees
224 | GTSuccess SExp 221 | GTSuccess SExp
225 | GTFailure 222 | GTFailure
226 deriving Show
227 223
228instance DeBruijnify SIName GuardTree where 224instance DeBruijnify SIName GuardTree where
229 deBruijnify_ l ns = mapGT (`deBruijnify_` ns) (`deBruijnify_` ns) l 225 deBruijnify_ l ns = mapGT (`deBruijnify_` ns) (`deBruijnify_` ns) l
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs
index 4b266fea..511e57f6 100644
--- a/src/LambdaCube/Compiler/Pretty.hs
+++ b/src/LambdaCube/Compiler/Pretty.hs
@@ -13,7 +13,7 @@ module LambdaCube.Compiler.Pretty
13 ( module LambdaCube.Compiler.Pretty 13 ( module LambdaCube.Compiler.Pretty
14 ) where 14 ) where
15 15
16import Data.Monoid 16--import Data.Monoid
17import Data.String 17import Data.String
18import Data.Char 18import Data.Char
19--import qualified Data.Set as Set 19--import qualified Data.Set as Set
@@ -39,9 +39,9 @@ data Fixity
39 39
40instance PShow Fixity where 40instance PShow Fixity where
41 pShow = \case 41 pShow = \case
42 Infix i -> "infix" `DApp` pShow i 42 Infix i -> "infix" <+> pShow i
43 InfixL i -> "infixl" `DApp` pShow i 43 InfixL i -> "infixl" <+> pShow i
44 InfixR i -> "infixr" `DApp` pShow i 44 InfixR i -> "infixr" <+> pShow i
45 45
46precedence, leftPrecedence, rightPrecedence :: Fixity -> Int 46precedence, leftPrecedence, rightPrecedence :: Fixity -> Int
47 47
@@ -98,12 +98,6 @@ strip = \case
98 DFreshName _ x -> strip x 98 DFreshName _ x -> strip x
99 x -> x 99 x -> x
100 100
101simple :: Doc -> Bool
102simple x = case strip x of
103 DAtom{} -> True
104 DVar{} -> True
105 _ -> False
106
107instance Show Doc where 101instance Show Doc where
108 show = show . renderDoc 102 show = show . renderDoc
109 103
@@ -232,6 +226,7 @@ pattern DBrace x = DPar "{" x "}"
232pattern DOp s f l r = DInfix f l (SimpleAtom s) r 226pattern DOp s f l r = DInfix f l (SimpleAtom s) r
233pattern DSep p a b = DOp " " p a b 227pattern DSep p a b = DOp " " p a b
234pattern DGlue p a b = DOp "" p a b 228pattern DGlue p a b = DOp "" p a b
229pattern DAt x = DGlue (InfixR 20) (DText "@") x
235 230
236pattern DArr_ s x y = DOp s (InfixR (-1)) x y 231pattern DArr_ s x y = DOp s (InfixR (-1)) x y
237pattern DArr x y = DArr_ "->" x y 232pattern DArr x y = DArr_ "->" x y
@@ -246,9 +241,9 @@ shTuple [] = "()"
246shTuple [x] = DParen $ DParen x 241shTuple [x] = DParen $ DParen x
247shTuple xs = DParen $ foldr1 DComma xs 242shTuple xs = DParen $ foldr1 DComma xs
248 243
249shLet i a b = shLam' (shLet' (blue $ shVar i) $ DUp i a) (DUp i b) 244shLet i a b = shLam' (DLet (blue $ shVar i) $ DUp i a) (DUp i b)
250shLet_ a b = DFreshName True $ shLam' (shLet' (shVar 0) $ DUp 0 a) b 245shLet_ a b = DFreshName True $ shLam' (DLet (shVar 0) $ DUp 0 a) b
251shLet' = DOp ":=" (Infix (-4)) 246pattern DLet x y = DOp ":=" (Infix (-4)) x y
252 247
253shAnn True x (strip -> DText "Type") = x 248shAnn True x (strip -> DText "Type") = x
254shAnn _ x y = DOp "::" (InfixR (-3)) x y 249shAnn _ x y = DOp "::" (InfixR (-3)) x y
@@ -293,6 +288,9 @@ instance PShow () where pShow _ = "()"
293instance PShow Bool where 288instance PShow Bool where
294 pShow b = if b then "True" else "False" 289 pShow b = if b then "True" else "False"
295 290
291instance PShow Void where
292 pShow = elimVoid
293
296instance (PShow a, PShow b) => PShow (Either a b) where 294instance (PShow a, PShow b) => PShow (Either a b) where
297 pShow = either (("Left" `DApp`) . pShow) (("Right" `DApp`) . pShow) 295 pShow = either (("Left" `DApp`) . pShow) (("Right" `DApp`) . pShow)
298 296
diff --git a/src/LambdaCube/Compiler/Statements.hs b/src/LambdaCube/Compiler/Statements.hs
index 6a0ac767..2112dd69 100644
--- a/src/LambdaCube/Compiler/Statements.hs
+++ b/src/LambdaCube/Compiler/Statements.hs
@@ -35,12 +35,14 @@ data PreStmt
35 | FunAlt SIName [(Visibility, SExp)]{-TODO: remove-} GuardTrees 35 | FunAlt SIName [(Visibility, SExp)]{-TODO: remove-} GuardTrees
36 | Class SIName [SExp]{-parameters-} [(SIName, SExp)]{-method names and types-} 36 | Class SIName [SExp]{-parameters-} [(SIName, SExp)]{-method names and types-}
37 | Instance SIName [ParPat]{-parameter patterns-} [SExp]{-constraints-} [Stmt]{-method definitions-} 37 | Instance SIName [ParPat]{-parameter patterns-} [SExp]{-constraints-} [Stmt]{-method definitions-}
38 deriving (Show) 38
39instance PShow PreStmt where
40 pShow _ = text "PreStmt - TODO"
39 41
40instance DeBruijnify SIName PreStmt where 42instance DeBruijnify SIName PreStmt where
41 deBruijnify_ k v = \case 43 deBruijnify_ k v = \case
42 FunAlt n ts gue -> FunAlt n (map (second $ deBruijnify_ k v) ts) $ deBruijnify_ k v gue 44 FunAlt n ts gue -> FunAlt n (map (second $ deBruijnify_ k v) ts) $ deBruijnify_ k v gue
43 x -> error $ "deBruijnify @ " ++ show x 45 x -> error $ "deBruijnify @ " ++ ppShow x
44 46
45mkLets :: [Stmt]{-where block-} -> SExp{-main expression-} -> SExp{-big let with lambdas; replaces global names with de bruijn indices-} 47mkLets :: [Stmt]{-where block-} -> SExp{-main expression-} -> SExp{-big let with lambdas; replaces global names with de bruijn indices-}
46mkLets = mkLets_ SLet 48mkLets = mkLets_ SLet
@@ -52,7 +54,7 @@ mkLets_ mkLet = mkLets' . sortDefs where
52 where 54 where
53 x' = if usedS n x then SBuiltin "primFix" `SAppV` SLamV (deBruijnify [n] x) else x 55 x' = if usedS n x then SBuiltin "primFix" `SAppV` SLamV (deBruijnify [n] x) else x
54 mkLets' (PrecDef{}: ds) e = mkLets' ds e 56 mkLets' (PrecDef{}: ds) e = mkLets' ds e
55 mkLets' (x: ds) e = error $ "mkLets: " ++ show x 57 mkLets' (x: ds) e = error $ "mkLets: " ++ ppShow x
56 58
57type DefinedSet = Set.Set SName 59type DefinedSet = Set.Set SName
58 60
diff --git a/src/LambdaCube/Compiler/Utils.hs b/src/LambdaCube/Compiler/Utils.hs
index 36dbe050..8e5dbee5 100644
--- a/src/LambdaCube/Compiler/Utils.hs
+++ b/src/LambdaCube/Compiler/Utils.hs
@@ -1,6 +1,9 @@
1{-# LANGUAGE NoMonomorphismRestriction #-} 1{-# LANGUAGE NoMonomorphismRestriction #-}
2{-# LANGUAGE ScopedTypeVariables #-} 2{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE EmptyCase #-} 3{-# LANGUAGE EmptyCase #-}
4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE UndecidableInstances #-}
6{-# LANGUAGE MultiParamTypeClasses #-}
4module LambdaCube.Compiler.Utils where 7module LambdaCube.Compiler.Utils where
5 8
6import qualified Data.IntSet as IS 9import qualified Data.IntSet as IS
@@ -8,8 +11,11 @@ import qualified Data.Text as T
8import qualified Text.Show.Pretty as PP 11import qualified Text.Show.Pretty as PP
9import Control.Monad.Catch 12import Control.Monad.Catch
10import Control.Monad.Except 13import Control.Monad.Except
14import Control.Monad.RWS
11import System.Directory 15import System.Directory
12import qualified Data.Text.IO as TIO 16import qualified Data.Text.IO as TIO
17import qualified Text.Megaparsec as P
18import qualified Text.Megaparsec.Prim as P
13 19
14------------------------------------------------------- general functions 20------------------------------------------------------- general functions
15 21
@@ -106,10 +112,23 @@ readFileIfExists fname = do
106 b <- doesFileExist fname 112 b <- doesFileExist fname
107 return $ if b then Just $ readFileStrict fname else Nothing 113 return $ if b then Just $ readFileStrict fname else Nothing
108 114
109------------------------------------------------------- misc 115------------------------------------------------------- missing instances
110 116
111instance MonadMask m => MonadMask (ExceptT e m) where 117instance MonadMask m => MonadMask (ExceptT e m) where
112 mask f = ExceptT $ mask $ \u -> runExceptT $ f (mapExceptT u) 118 mask f = ExceptT $ mask $ \u -> runExceptT $ f (mapExceptT u)
113 uninterruptibleMask = error "not implemented: uninterruptibleMask for ExcpetT" 119 uninterruptibleMask = error "not implemented: uninterruptibleMask for ExcpetT"
114 120
121instance (Monoid w, P.MonadParsec st m t) => P.MonadParsec st (RWST r w s m) t where
122 failure = lift . P.failure
123 label = mapRWST . P.label
124 try = mapRWST P.try
125 lookAhead (RWST m) = RWST $ \r s -> (\(a, _, _) -> (a, s, mempty)) <$> P.lookAhead (m r s)
126 notFollowedBy (RWST m) = RWST $ \r s -> P.notFollowedBy ((\(a, _, _) -> a) <$> m r s) >> return ((), s, mempty)
127 withRecovery rec (RWST m) = RWST $ \r s -> P.withRecovery (\e -> runRWST (rec e) r s) (m r s)
128 eof = lift P.eof
129 token f e = lift $ P.token f e
130 tokens f e ts = lift $ P.tokens f e ts
131 getParserState = lift P.getParserState
132 updateParserState f = lift $ P.updateParserState f
133
115 134