summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/Compiler/DesugaredSource.hs7
-rw-r--r--src/LambdaCube/Compiler/Parser.hs15
-rw-r--r--src/LambdaCube/Compiler/Statements.hs47
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
473sortDefs :: [Stmt] -> [Stmt] 473sortDefs :: [Stmt] -> [[Stmt]]
474sortDefs xs = concatMap (desugarMutual . map snValue) $ scc snId snChildren snRevChildren nodes 474sortDefs 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
497desugarMutual [x] = [x]
498desugarMutual xs = xs
499
500-------------------------------------------------------------------------------- module 497-------------------------------------------------------------------------------- module
501 498
502data Module_ a = Module 499data 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]
500valueDef = do 500valueDef = 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
514mangleNames 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
17import qualified Data.Set as Set 17import qualified Data.Set as Set
18import Control.Monad.Writer 18import Control.Monad.Writer
19import Control.Arrow hiding ((<+>)) 19import Control.Arrow hiding ((<+>))
20--import Debug.Trace 20import Debug.Trace
21 21
22import LambdaCube.Compiler.DeBruijn 22import LambdaCube.Compiler.DeBruijn
23import LambdaCube.Compiler.Pretty hiding (braces, parens) 23import LambdaCube.Compiler.Pretty hiding (braces, parens)
@@ -47,14 +47,17 @@ instance DeBruijnify SIName PreStmt where
47mkLets :: [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-}
48mkLets = mkLets_ SLet 48mkLets = mkLets_ SLet
49 49
50mkLets_ mkLet = mkLets' . sortDefs where 50mkLets_ mkLet = mkLets' mkLet . concatMap desugarMutual . sortDefs
51 mkLets' [] e = e 51
52 mkLets' (Let n mt x: ds) e 52mkLets' 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
58addFix n x
59 | usedS n x = SBuiltin "primFix" `SAppV` SLamV (deBruijnify [n] x)
60 | otherwise = x
58 61
59type DefinedSet = Set.Set SName 62type 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
119funAlt' n ts x gt = FunAlt n ts $ compilePatts x gt 122funAlt' n ts x gt = FunAlt n ts $ compilePatts x gt
120 123
124desugarValueDef :: MonadWriter [ParseCheck] m => ParPat -> SExp -> m [PreStmt]
125desugarValueDef 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
134getLet (Let x Nothing (SRHS dx)) = Just (x, dx)
135getLet _ = Nothing
136
137desugarMutual :: {-MonadWriter [ParseCheck] m => -} [Stmt] -> [Stmt]
138desugarMutual [x] = [x]
139desugarMutual (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
145desugarMutual xs = error "desugarMutual"
146
147mangleNames xs = SIName (foldMap sourceInfo xs) $ "_" ++ intercalate "_" (sName <$> xs)
148
149