diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/Compiler/DesugaredSource.hs | 7 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Parser.hs | 15 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Statements.hs | 47 |
3 files changed, 42 insertions, 27 deletions
diff --git a/src/LambdaCube/Compiler/DesugaredSource.hs b/src/LambdaCube/Compiler/DesugaredSource.hs index f0af5437..a2c4e591 100644 --- a/src/LambdaCube/Compiler/DesugaredSource.hs +++ b/src/LambdaCube/Compiler/DesugaredSource.hs | |||
@@ -470,8 +470,8 @@ data StmtNode = StmtNode | |||
470 | , snRevChildren :: [StmtNode] | 470 | , snRevChildren :: [StmtNode] |
471 | } | 471 | } |
472 | 472 | ||
473 | sortDefs :: [Stmt] -> [Stmt] | 473 | sortDefs :: [Stmt] -> [[Stmt]] |
474 | sortDefs xs = concatMap (desugarMutual . map snValue) $ scc snId snChildren snRevChildren nodes | 474 | sortDefs xs = map snValue <$> scc snId snChildren snRevChildren nodes |
475 | where | 475 | where |
476 | nodes = zipWith mkNode [0..] xs | 476 | nodes = zipWith mkNode [0..] xs |
477 | where | 477 | where |
@@ -494,9 +494,6 @@ sortDefs xs = concatMap (desugarMutual . map snValue) $ scc snId snChildren snRe | |||
494 | Let n _ _ -> [n] | 494 | Let n _ _ -> [n] |
495 | Data n _ _ cs -> n: map fst cs | 495 | Data n _ _ cs -> n: map fst cs |
496 | 496 | ||
497 | desugarMutual [x] = [x] | ||
498 | desugarMutual xs = xs | ||
499 | |||
500 | -------------------------------------------------------------------------------- module | 497 | -------------------------------------------------------------------------------- module |
501 | 498 | ||
502 | data Module_ a = Module | 499 | data Module_ a = Module |
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs index 8f457e49..50a1632b 100644 --- a/src/LambdaCube/Compiler/Parser.hs +++ b/src/LambdaCube/Compiler/Parser.hs | |||
@@ -500,18 +500,7 @@ valueDef :: BodyParser [PreStmt] | |||
500 | valueDef = do | 500 | valueDef = do |
501 | (dns, p) <- try "pattern" $ longPattern <* reservedOp "=" | 501 | (dns, p) <- try "pattern" $ longPattern <* reservedOp "=" |
502 | checkPattern dns | 502 | checkPattern dns |
503 | desugarValueDef p =<< setR parseTermLam | 503 | runCheck . desugarValueDef p =<< setR parseTermLam |
504 | where | ||
505 | desugarValueDef p e = runCheck $ sequence | ||
506 | $ pure (FunAlt n [] $ noGuards e) | ||
507 | : [ FunAlt x [] . noGuards <$> compileCase (SGlobal n) [(p, noGuards $ SVar x i)] | ||
508 | | (i, x) <- zip [0..] dns | ||
509 | ] | ||
510 | where | ||
511 | dns = reverse $ getPVars p | ||
512 | n = mangleNames dns | ||
513 | |||
514 | mangleNames xs = SIName (foldMap sourceInfo xs) $ "_" ++ intercalate "_" (sName <$> xs) | ||
515 | 504 | ||
516 | -------------------------------------------------------------------------------- modules | 505 | -------------------------------------------------------------------------------- modules |
517 | 506 | ||
@@ -587,5 +576,5 @@ runDefParser ds_ dp = do | |||
587 | f (Uncovered' si x) | not $ null $ filter (not . null . fst) x = Just $ Uncovered si x | 576 | f (Uncovered' si x) | not $ null $ filter (not . null . fst) x = Just $ Uncovered si x |
588 | f _ = Nothing | 577 | f _ = Nothing |
589 | 578 | ||
590 | return (sortDefs defs, catMaybes [f w | Right w <- dns], ds) | 579 | return (concatMap desugarMutual $ sortDefs defs, catMaybes [f w | Right w <- dns], ds) |
591 | 580 | ||
diff --git a/src/LambdaCube/Compiler/Statements.hs b/src/LambdaCube/Compiler/Statements.hs index c9dfa1f7..2f1b5859 100644 --- a/src/LambdaCube/Compiler/Statements.hs +++ b/src/LambdaCube/Compiler/Statements.hs | |||
@@ -17,7 +17,7 @@ import Data.Function | |||
17 | import qualified Data.Set as Set | 17 | import qualified Data.Set as Set |
18 | import Control.Monad.Writer | 18 | import Control.Monad.Writer |
19 | import Control.Arrow hiding ((<+>)) | 19 | import Control.Arrow hiding ((<+>)) |
20 | --import Debug.Trace | 20 | import Debug.Trace |
21 | 21 | ||
22 | import LambdaCube.Compiler.DeBruijn | 22 | import LambdaCube.Compiler.DeBruijn |
23 | import LambdaCube.Compiler.Pretty hiding (braces, parens) | 23 | import LambdaCube.Compiler.Pretty hiding (braces, parens) |
@@ -47,14 +47,17 @@ instance DeBruijnify SIName PreStmt where | |||
47 | mkLets :: [Stmt]{-where block-} -> SExp{-main expression-} -> SExp{-big let with lambdas; replaces global names with de bruijn indices-} | 47 | mkLets :: [Stmt]{-where block-} -> SExp{-main expression-} -> SExp{-big let with lambdas; replaces global names with de bruijn indices-} |
48 | mkLets = mkLets_ SLet | 48 | mkLets = mkLets_ SLet |
49 | 49 | ||
50 | mkLets_ mkLet = mkLets' . sortDefs where | 50 | mkLets_ mkLet = mkLets' mkLet . concatMap desugarMutual . sortDefs |
51 | mkLets' [] e = e | 51 | |
52 | mkLets' (Let n mt x: ds) e | 52 | mkLets' mkLet = f where |
53 | = mkLet n (maybe id (flip SAnn) mt x') (deBruijnify [n] $ mkLets' ds e) | 53 | f [] e = e |
54 | where | 54 | f (Let n mt x: ds) e = mkLet n (maybe id (flip SAnn) mt (addFix n x)) (deBruijnify [n] $ f ds e) |
55 | x' = if usedS n x then SBuiltin "primFix" `SAppV` SLamV (deBruijnify [n] x) else x | 55 | f (PrecDef{}: ds) e = f ds e |
56 | mkLets' (PrecDef{}: ds) e = mkLets' ds e | 56 | f (x: ds) e = error $ "mkLets: " ++ ppShow x |
57 | mkLets' (x: ds) e = error $ "mkLets: " ++ ppShow x | 57 | |
58 | addFix n x | ||
59 | | usedS n x = SBuiltin "primFix" `SAppV` SLamV (deBruijnify [n] x) | ||
60 | | otherwise = x | ||
58 | 61 | ||
59 | type DefinedSet = Set.Set SName | 62 | type DefinedSet = Set.Set SName |
60 | 63 | ||
@@ -118,3 +121,29 @@ funAlt n pats gt = FunAlt n (fst <$> pats) $ compilePatts (map snd pats) gt | |||
118 | 121 | ||
119 | funAlt' n ts x gt = FunAlt n ts $ compilePatts x gt | 122 | funAlt' n ts x gt = FunAlt n ts $ compilePatts x gt |
120 | 123 | ||
124 | desugarValueDef :: MonadWriter [ParseCheck] m => ParPat -> SExp -> m [PreStmt] | ||
125 | desugarValueDef p e = sequence | ||
126 | $ pure (FunAlt n [] $ noGuards e) | ||
127 | : [ FunAlt x [] . noGuards <$> compileCase (SGlobal n) [(p, noGuards $ SVar x i)] | ||
128 | | (i, x) <- zip [0..] dns | ||
129 | ] | ||
130 | where | ||
131 | dns = reverse $ getPVars p | ||
132 | n = mangleNames dns | ||
133 | |||
134 | getLet (Let x Nothing (SRHS dx)) = Just (x, dx) | ||
135 | getLet _ = Nothing | ||
136 | |||
137 | desugarMutual :: {-MonadWriter [ParseCheck] m => -} [Stmt] -> [Stmt] | ||
138 | desugarMutual [x] = [x] | ||
139 | desugarMutual (traverse getLet -> Just (unzip -> (ns, ds))) = fst{-TODO-} $ runWriter $ do | ||
140 | ss <- compileStmt' =<< desugarValueDef (foldr cHCons cHNil $ PVarSimp <$> ns) (SGlobal xy) | ||
141 | return $ | ||
142 | Let xy Nothing (addFix xy $ SRHS $ mkLets' SLet ss $ foldr HCons HNil ds) : ss | ||
143 | where | ||
144 | xy = mangleNames ns | ||
145 | desugarMutual xs = error "desugarMutual" | ||
146 | |||
147 | mangleNames xs = SIName (foldMap sourceInfo xs) $ "_" ++ intercalate "_" (sName <$> xs) | ||
148 | |||
149 | |||