summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-04-19 10:36:13 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-04-19 10:36:13 +0200
commit445da179fab22a16cd68aa9f1139d77a2e121543 (patch)
tree772983bf71a2ef70a26c40e90b667befd7a9d77f /src
parent94963d9fedafa68481dc0c1b9c0e14f2b4f233b5 (diff)
refactoring & fix switching to type namespace after @
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/Compiler/Infer.hs4
-rw-r--r--src/LambdaCube/Compiler/Parser.hs128
2 files changed, 74 insertions, 58 deletions
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs
index 7e41aaf2..c1964205 100644
--- a/src/LambdaCube/Compiler/Infer.hs
+++ b/src/LambdaCube/Compiler/Infer.hs
@@ -928,7 +928,7 @@ inferN_ tellTrace = infer where
928 infer te exp = tellTrace "infer" (showEnvSExp te exp) $ (if debug then fmap (fmap{-todo-} $ recheck' "infer" te) else id) $ case exp of 928 infer te exp = tellTrace "infer" (showEnvSExp te exp) $ (if debug then fmap (fmap{-todo-} $ recheck' "infer" te) else id) $ case exp of
929 Parens x -> infer te x 929 Parens x -> infer te x
930 SAnn x t -> checkN (CheckIType x te) t TType 930 SAnn x t -> checkN (CheckIType x te) t TType
931 SLabelEnd x -> infer (ELabelEnd te) x 931 SRHS x -> infer (ELabelEnd te) x
932 SVar (si, _) i -> focus_' te exp (Var i, snd $ varType "C2" i te) 932 SVar (si, _) i -> focus_' te exp (Var i, snd $ varType "C2" i te)
933 SLit si l -> focus_' te exp (ELit l, litType l) 933 SLit si l -> focus_' te exp (ELit l, litType l)
934 STyped si et -> focus_' te exp et 934 STyped si et -> focus_' te exp et
@@ -958,7 +958,7 @@ inferN_ tellTrace = infer where
958 , TVec (Var n') _ <- snd $ varType "xx" v te 958 , TVec (Var n') _ <- snd $ varType "xx" v te
959 = infer te $ x `SAppV` SLamV (SLamV (STyped mempty (subst (n'+2) (Var 1) $ up1_ (n'+3) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` SVar siv v 959 = infer te $ x `SAppV` SLamV (SLamV (STyped mempty (subst (n'+2) (Var 1) $ up1_ (n'+3) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` SVar siv v
960-} 960-}
961 | SLabelEnd x <- e = checkN (ELabelEnd te) x t 961 | SRHS x <- e = checkN (ELabelEnd te) x t
962 | SApp si h a b <- e = infer (CheckAppType si h t te b) a 962 | SApp si h a b <- e = infer (CheckAppType si h t te b) a
963 | SLam h a b <- e, Pi h' x y <- t, h == h' = do 963 | SLam h a b <- e, Pi h' x y <- t, h == h' = do
964 tellType e t 964 tellType e t
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs
index ceee43d0..0a0e64ae 100644
--- a/src/LambdaCube/Compiler/Parser.hs
+++ b/src/LambdaCube/Compiler/Parser.hs
@@ -13,7 +13,7 @@ module LambdaCube.Compiler.Parser
13 , sourceInfo, SI(..), debugSI 13 , sourceInfo, SI(..), debugSI
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 SRHS, pattern SLam, pattern Parens
17 , pattern TyType, pattern SLet 17 , pattern TyType, pattern SLet
18 , debug, isPi, iterateN, traceD 18 , debug, isPi, iterateN, traceD
19 , parseLC, runDefParser 19 , parseLC, runDefParser
@@ -41,7 +41,7 @@ import Control.Arrow hiding ((<+>))
41import Control.Applicative 41import Control.Applicative
42--import Debug.Trace 42--import Debug.Trace
43 43
44import qualified LambdaCube.Compiler.Pretty as P 44import qualified LambdaCube.Compiler.Pretty as BodyParser
45import LambdaCube.Compiler.Pretty hiding (Doc, braces, parens) 45import LambdaCube.Compiler.Pretty hiding (Doc, braces, parens)
46import LambdaCube.Compiler.Lexer 46import LambdaCube.Compiler.Lexer
47 47
@@ -58,6 +58,9 @@ data Void
58instance Show Void where show _ = error "show @Void" 58instance Show Void where show _ = error "show @Void"
59instance Eq Void where _ == _ = error "(==) @Void" 59instance Eq Void where _ == _ = error "(==) @Void"
60 60
61elimVoid :: Void -> a
62elimVoid _ = error "impossible"
63
61-- supplementary data: data with no semantic relevance 64-- supplementary data: data with no semantic relevance
62newtype SData a = SData a 65newtype SData a = SData a
63 66
@@ -122,7 +125,7 @@ infixl 2 `SAppV`, `SAppH`
122pattern SBuiltin s <- SGlobal (_, s) 125pattern SBuiltin s <- SGlobal (_, s)
123 where SBuiltin s = SGlobal (debugSI $ "builtin " ++ s, s) 126 where SBuiltin s = SGlobal (debugSI $ "builtin " ++ s, s)
124 127
125pattern SLabelEnd a = SBuiltin "^rhs" `SAppV` a 128pattern SRHS a = SBuiltin "^rhs" `SAppV` a
126pattern Section e = SBuiltin "^section" `SAppV` e 129pattern Section e = SBuiltin "^section" `SAppV` e
127pattern SType = SBuiltin "'Type" 130pattern SType = SBuiltin "'Type"
128pattern Parens e = SBuiltin "parens" `SAppV` e 131pattern Parens e = SBuiltin "parens" `SAppV` e
@@ -197,6 +200,7 @@ instance (Up a, Up b) => Up (a, b) where
197up n = up_ n 0 200up n = up_ n 0
198up1 = up1_ 0 201up1 = up1_ 0
199 202
203substS :: Up a => Int -> a -> SExp' a -> SExp' a
200substS j x = mapS' f2 ((+1) *** up 1) (j, x) 204substS j x = mapS' f2 ((+1) *** up 1) (j, x)
201 where 205 where
202 f2 sn i (j, x) = case compare i j of 206 f2 sn i (j, x) = case compare i j of
@@ -204,6 +208,14 @@ substS j x = mapS' f2 ((+1) *** up 1) (j, x)
204 LT -> SVar sn i 208 LT -> SVar sn i
205 EQ -> STyped (fst sn) x 209 EQ -> STyped (fst sn) x
206 210
211foldS
212 :: Monoid m
213 => (Int -> SI -> t -> m)
214 -> (SIName -> Int -> m)
215 -> (SIName -> Int -> Int -> m)
216 -> Int
217 -> SExp' t
218 -> m
207foldS h g f = fs 219foldS h g f = fs
208 where 220 where
209 fs i = \case 221 fs i = \case
@@ -215,11 +227,23 @@ foldS h g f = fs
215 SGlobal sn -> g sn i 227 SGlobal sn -> g sn i
216 x@SLit{} -> mempty 228 x@SLit{} -> mempty
217 229
230freeS :: SExp' t -> [SIName]
218freeS = nub . foldS (\_ _ _ -> error "freeS") (\sn _ -> [sn]) mempty 0 231freeS = nub . foldS (\_ _ _ -> error "freeS") (\sn _ -> [sn]) mempty 0
219 232
220usedS n = getAny . foldS (\_ _ _ -> error "usedS") (\sn _ -> Any $ n == sn) mempty 0 233usedS :: SIName -> SExp -> Bool
234usedS n = getAny . foldS (\_ _ -> elimVoid) (\sn _ -> Any $ n == sn) mempty 0
221 235
236mapS' :: (SIName -> Int -> t -> SExp' a) -> (t -> t) -> t -> SExp' a -> SExp' a
222mapS' = mapS__ (\_ _ _ -> error "mapS'") (const . SGlobal) 237mapS' = mapS__ (\_ _ _ -> error "mapS'") (const . SGlobal)
238
239mapS__
240 :: (t -> SI -> a -> SExp' a)
241 -> (SIName -> t -> SExp' a)
242 -> (SIName -> Int -> t -> SExp' a)
243 -> (t -> t)
244 -> t
245 -> SExp' a
246 -> SExp' a
223mapS__ hh gg f2 h = g where 247mapS__ hh gg f2 h = g where
224 g i = \case 248 g i = \case
225 SApp si v a b -> SApp si v (g i a) (g i b) 249 SApp si v a b -> SApp si v (g i a) (g i b)
@@ -232,17 +256,12 @@ mapS__ hh gg f2 h = g where
232 256
233rearrangeS :: (Int -> Int) -> SExp -> SExp 257rearrangeS :: (Int -> Int) -> SExp -> SExp
234rearrangeS f = mapS' (\sn j i -> SVar sn $ if j < i then j else i + f (j - i)) (+1) 0 258rearrangeS f = mapS' (\sn j i -> SVar sn $ if j < i then j else i + f (j - i)) (+1) 0
235{- 259
236substS'' :: Int -> Int -> SExp' a -> SExp' a 260substSG :: SIName -> Int -> SExp' a -> SExp' a
237substS'' j' x = mapS' f2 (+1) j'
238 where
239 f2 sn j i
240 | j < i = SVar sn j
241 | j == i = SVar sn $ x + (j - j')
242 | j > i = SVar sn $ j - 1
243-}
244substSG j = mapS__ (\_ _ _ -> error "substSG") (\sn x -> if sn == j then SVar sn x else SGlobal sn) (\sn j -> const $ SVar sn j) (+1) 261substSG j = mapS__ (\_ _ _ -> error "substSG") (\sn x -> if sn == j then SVar sn x else SGlobal sn) (\sn j -> const $ SVar sn j) (+1)
245substSG0 n = substSG n 0 . up1 262
263substSG0 :: Up a => SIName -> SExp' a -> SExp' a
264substSG0 n = substSG n 0 . up1 -- is up1 needed here?
246 265
247instance Up Void where 266instance Up Void where
248 up_ n i = error "up_ @Void" 267 up_ n i = error "up_ @Void"
@@ -256,16 +275,13 @@ instance Up a => Up (SExp' a) where
256 fold f = foldS (\i si x -> fold f i x) mempty $ \sn j i -> f j i 275 fold f = foldS (\i si x -> fold f i x) mempty $ \sn j i -> f j i
257 276
258dbf' = dbf_ 0 277dbf' = dbf_ 0
278dbf_ :: Int -> DBNames -> SExp -> SExp
259dbf_ j xs e = foldl (\e (i, sn) -> substSG sn i e) e $ zip [j..] xs 279dbf_ j xs e = foldl (\e (i, sn) -> substSG sn i e) e $ zip [j..] xs
260 280
281-- is this needed, or can be replaced by dbf' ?
261dbff :: DBNames -> SExp -> SExp 282dbff :: DBNames -> SExp -> SExp
262dbff ns e = foldr substSG0 e ns 283dbff ns e = foldr substSG0 e ns
263 284
264trSExp' = trSExp elimVoid
265
266elimVoid :: Void -> a
267elimVoid _ = error "impossible"
268
269trSExp :: (a -> b) -> SExp' a -> SExp' b 285trSExp :: (a -> b) -> SExp' a -> SExp' b
270trSExp f = g where 286trSExp f = g where
271 g = \case 287 g = \case
@@ -277,9 +293,14 @@ trSExp f = g where
277 SLit si l -> SLit si l 293 SLit si l -> SLit si l
278 STyped si a -> STyped si $ f a 294 STyped si a -> STyped si $ f a
279 295
296trSExp' :: SExp -> SExp' a
297trSExp' = trSExp elimVoid
298
280-------------------------------------------------------------------------------- parser type 299-------------------------------------------------------------------------------- parser type
281 300
282type P = Parse DesugarInfo PostponedCheck 301type BodyParser = Parse DesugarInfo PostponedCheck
302
303type PostponedCheck = Maybe LCParseError
283 304
284data LCParseError 305data LCParseError
285 = MultiplePatternVars [[SIName]] 306 = MultiplePatternVars [[SIName]]
@@ -292,18 +313,16 @@ instance Show LCParseError where
292 OperatorMismatch (op, f) (op', f') -> "Operator precedences don't match:\n" ++ show f ++ " at " ++ showSI (fst op) ++ "\n" ++ show f' ++ " at " ++ showSI (fst op') 313 OperatorMismatch (op, f) (op', f') -> "Operator precedences don't match:\n" ++ show f ++ " at " ++ showSI (fst op) ++ "\n" ++ show f' ++ " at " ++ showSI (fst op')
293 ParseError p -> show p 314 ParseError p -> show p
294 315
295type PostponedCheck = Maybe LCParseError
296
297getFixity :: DesugarInfo -> SName -> Fixity
298getFixity (fm, _) n = fromMaybe (InfixL, 9) $ Map.lookup n fm
299
300type DesugarInfo = (FixityMap, ConsMap) 316type DesugarInfo = (FixityMap, ConsMap)
301 317
302type ConsMap = Map.Map SName{-constructor name-} 318type ConsMap = Map.Map SName{-constructor name-}
303 (Either ((SName{-case eliminator name-}, Int{-num of indices-}), [(SName, Int)]{-constructors with arities-}) 319 (Either ((SName{-case eliminator name-}, Int{-num of indices-}), [(SName, Int)]{-constructors with arities-})
304 Int{-arity-}) 320 Int{-arity-})
305 321
306dsInfo :: P DesugarInfo 322getFixity :: DesugarInfo -> SName -> Fixity
323getFixity (fm, _) n = fromMaybe (InfixL, 9) $ Map.lookup n fm
324
325dsInfo :: BodyParser DesugarInfo
307dsInfo = asks desugarInfo 326dsInfo = asks desugarInfo
308 327
309-------------------------------------------------------------------------------- builtin precedences 328-------------------------------------------------------------------------------- builtin precedences
@@ -324,14 +343,14 @@ data Prec
324 343
325-------------------------------------------------------------------------------- expression parsing 344-------------------------------------------------------------------------------- expression parsing
326 345
327parseType mb = maybe id option mb (reservedOp "::" *> parseTTerm PrecLam) 346parseType mb = maybe id option mb (reservedOp "::" *> typeNS (parseTerm PrecLam))
328typedIds mb = (,) <$> commaSep1 upperLower <*> parseType mb 347typedIds mb = (,) <$> commaSep1 upperLower <*> parseType mb
329 348
330hiddenTerm p q = (,) Hidden <$ reservedOp "@" <*> p <|> (,) Visible <$> q 349hiddenTerm p q = (,) Hidden <$ reservedOp "@" <*> (typeNS p) <|> (,) Visible <$> q
331 350
332telescope mb = fmap dbfi $ many $ hiddenTerm 351telescope mb = fmap dbfi $ many $ hiddenTerm
333 (typedId <|> maybe empty (tvar . pure) mb) 352 (typedId <|> maybe empty (tvar . pure) mb)
334 (try "::" typedId <|> maybe ((,) <$> pure (mempty, "") <*> parseTTerm PrecAtom) (tvar . pure) mb) 353 (try "::" typedId <|> maybe ((,) (mempty, "") <$> typeNS (parseTerm PrecAtom)) (tvar . pure) mb)
335 where 354 where
336 tvar x = (,) <$> patVar <*> x 355 tvar x = (,) <$> patVar <*> x
337 typedId = parens $ tvar $ parseType mb 356 typedId = parens $ tvar $ parseType mb
@@ -341,24 +360,21 @@ dbfi = first reverse . unzip . go []
341 go _ [] = [] 360 go _ [] = []
342 go vs ((v, (n, e)): ts) = (n, (v, dbf' vs e)): go (n: vs) ts 361 go vs ((v, (n, e)): ts) = (n, (v, dbf' vs e)): go (n: vs) ts
343 362
344parseTTerm = typeNS . parseTerm
345parseETerm = expNS . parseTerm
346
347indentation p q = p >> q 363indentation p q = p >> q
348 364
349setSI' p = appRange $ flip setSI <$> p 365setSI' p = appRange $ flip setSI <$> p
350 366
351parseTerm = setSI' . parseTerm_ 367parseTerm = setSI' . parseTerm_
352 368
353parseTerm_ :: Prec -> P SExp 369parseTerm_ :: Prec -> BodyParser SExp
354parseTerm_ prec = case prec of 370parseTerm_ = \case
355 PrecLam -> 371 PrecLam ->
356 do level PrecAnn $ \t -> mkPi <$> (Visible <$ reservedOp "->" <|> Hidden <$ reservedOp "=>") <*> pure t <*> parseTTerm PrecLam 372 do level PrecAnn $ \t -> mkPi <$> (Visible <$ reservedOp "->" <|> Hidden <$ reservedOp "=>") <*> pure t <*> parseTerm PrecLam
357 <|> mkIf <$ reserved "if" <*> parseTerm PrecLam <* reserved "then" <*> parseTerm PrecLam <* reserved "else" <*> parseTerm PrecLam 373 <|> mkIf <$ reserved "if" <*> parseTerm PrecLam <* reserved "then" <*> parseTerm PrecLam <* reserved "else" <*> parseTerm PrecLam
358 <|> do reserved "forall" 374 <|> do reserved "forall"
359 (fe, ts) <- telescope (Just $ Wildcard SType) 375 (fe, ts) <- telescope (Just $ Wildcard SType)
360 f <- SPi . const Hidden <$ reservedOp "." <|> SPi . const Visible <$ reservedOp "->" 376 f <- SPi . const Hidden <$ reservedOp "." <|> SPi . const Visible <$ reservedOp "->"
361 t' <- dbf' fe <$> parseTTerm PrecLam 377 t' <- dbf' fe <$> parseTerm PrecLam
362 return $ foldr (uncurry f) t' ts 378 return $ foldr (uncurry f) t' ts
363 <|> do expNS $ do 379 <|> do expNS $ do
364 (fe, ts) <- reservedOp "\\" *> telescopePat <* reservedOp "->" 380 (fe, ts) <- reservedOp "\\" *> telescopePat <* reservedOp "->"
@@ -366,7 +382,7 @@ parseTerm_ prec = case prec of
366 t' <- dbf' fe <$> parseTerm PrecLam 382 t' <- dbf' fe <$> parseTerm PrecLam
367 ge <- dsInfo 383 ge <- dsInfo
368 return $ foldr (uncurry (patLam id ge)) t' ts 384 return $ foldr (uncurry (patLam id ge)) t' ts
369 <|> compileCase <$ reserved "case" <*> dsInfo <*> parseETerm PrecLam <* reserved "of" <*> do 385 <|> compileCase <$ reserved "case" <*> dsInfo <*> parseTerm PrecLam <* reserved "of" <*> do
370 identation False $ do 386 identation False $ do
371 (fe, p) <- longPattern 387 (fe, p) <- longPattern
372 (,) p <$> parseRHS (dbf' fe) "->" 388 (,) p <$> parseRHS (dbf' fe) "->"
@@ -380,7 +396,7 @@ parseTerm_ prec = case prec of
380 ex pr = pure . Right <$> parseTerm pr 396 ex pr = pure . Right <$> parseTerm pr
381 PrecApp -> 397 PrecApp ->
382 AppsS <$> try "record" ((SGlobal <$> upperCase) <* symbol "{") <*> commaSep (lowerCase *> reservedOp "=" *> ((,) Visible <$> parseTerm PrecLam)) <* symbol "}" 398 AppsS <$> try "record" ((SGlobal <$> upperCase) <* symbol "{") <*> commaSep (lowerCase *> reservedOp "=" *> ((,) Visible <$> parseTerm PrecLam)) <* symbol "}"
383 <|> AppsS <$> parseTerm PrecSwiz <*> many (hiddenTerm (parseTTerm PrecSwiz) $ parseTerm PrecSwiz) 399 <|> AppsS <$> parseTerm PrecSwiz <*> many (hiddenTerm (typeNS $ parseTerm PrecSwiz) $ parseTerm PrecSwiz)
384 PrecSwiz -> level PrecProj $ \t -> mkSwizzling t <$> lexeme (try "swizzling" $ char '%' *> manyNM 1 4 (satisfy (`elem` ("xyzwrgba" :: String)))) 400 PrecSwiz -> level PrecProj $ \t -> mkSwizzling t <$> lexeme (try "swizzling" $ char '%' *> manyNM 1 4 (satisfy (`elem` ("xyzwrgba" :: String))))
385 PrecProj -> level PrecAtom $ \t -> try "projection" $ mkProjection t <$ char '.' <*> sepBy1 (uncurry SLit . second LString <$> lowerCase) (char '.') 401 PrecProj -> level PrecAtom $ \t -> try "projection" $ mkProjection t <$ char '.' <*> sepBy1 (uncurry SLit . second LString <$> lowerCase) (char '.')
386 PrecAtom -> 402 PrecAtom ->
@@ -453,7 +469,7 @@ parseTerm_ prec = case prec of
453 469
454 mkDotDot e f = SBuiltin "fromTo" `SAppV` e `SAppV` f 470 mkDotDot e f = SBuiltin "fromTo" `SAppV` e `SAppV` f
455 471
456 calculatePrecs :: DesugarInfo -> [Either SIName SExp] -> P SExp 472 calculatePrecs :: DesugarInfo -> [Either SIName SExp] -> BodyParser SExp
457 calculatePrecs dcls = f where 473 calculatePrecs dcls = f where
458 f [] = error "impossible" 474 f [] = error "impossible"
459 f (Right t: xs) = join $ either (\(op, xs) -> calcPrec' t xs <&> \z -> Section $ SLamV $ SGlobal op `SAppV` up1 z `SAppV` sVar "calcPrec" 0) (calcPrec' t) <$> cont xs 475 f (Right t: xs) = join $ either (\(op, xs) -> calcPrec' t xs <&> \z -> Section $ SLamV $ SGlobal op `SAppV` up1 z `SAppV` sVar "calcPrec" 0) (calcPrec' t) <$> cont xs
@@ -470,7 +486,7 @@ parseTerm_ prec = case prec of
470 486
471 calcPrec' = (postponedCheck dcls .) . calcPrec (\op x y -> SGlobal op `SAppV` x `SAppV` y) (getFixity dcls . snd) 487 calcPrec' = (postponedCheck dcls .) . calcPrec (\op x y -> SGlobal op `SAppV` x `SAppV` y) (getFixity dcls . snd)
472 488
473 generator, letdecl, boolExpression :: P (SExp -> SExp) 489 generator, letdecl, boolExpression :: BodyParser (SExp -> SExp)
474 generator = do 490 generator = do
475 ge <- dsInfo 491 ge <- dsInfo
476 (dbs, pat) <- try "generator" $ longPattern <* reservedOp "<-" 492 (dbs, pat) <- try "generator" $ longPattern <* reservedOp "<-"
@@ -489,16 +505,16 @@ parseTerm_ prec = case prec of
489 boolExpression = (\pred e -> SBuiltin "primIfThenElse" `SAppV` pred `SAppV` e `SAppV` SBuiltin "Nil") <$> parseTerm PrecLam 505 boolExpression = (\pred e -> SBuiltin "primIfThenElse" `SAppV` pred `SAppV` e `SAppV` SBuiltin "Nil") <$> parseTerm PrecLam
490 506
491 507
492 mkPi Hidden (getTTuple' -> xs) b = foldr (sNonDepPi Hidden) b xs 508 mkPi Hidden xs b = foldr (sNonDepPi Hidden) b $ getTTuple' xs
493 mkPi h a b = sNonDepPi h a b 509 mkPi h a b = sNonDepPi h a b
494 510
495 sNonDepPi h a b = SPi h a $ up1 b 511 sNonDepPi h a b = SPi h a $ up1 b
496 512
497getTTuple' (SBuiltin "'HList" `SAppV` (getTTuple -> Just (n, xs))) | n == length xs = xs 513getTTuple' (SBuiltin "'HList" `SAppV` (getTTuple -> Just xs)) = xs
498getTTuple' x = [x] 514getTTuple' x = [x]
499 515
500getTTuple (SBuiltin "Nil") = Just (0, []) 516getTTuple (SBuiltin "Nil") = Just []
501getTTuple (SBuiltin "Cons" `SAppV` x `SAppV` (getTTuple -> Just (n, y))) = Just (n+1, x:y) 517getTTuple (SBuiltin "Cons" `SAppV` x `SAppV` (getTTuple -> Just y)) = Just (x:y)
502getTTuple _ = Nothing 518getTTuple _ = Nothing
503 519
504patLam :: (SExp -> SExp) -> DesugarInfo -> (Visibility, SExp) -> Pat -> SExp -> SExp 520patLam :: (SExp -> SExp) -> DesugarInfo -> (Visibility, SExp) -> Pat -> SExp -> SExp
@@ -564,7 +580,7 @@ instance SourceInfo Pat where
564 580
565-------------------------------------------------------------------------------- pattern parsing 581-------------------------------------------------------------------------------- pattern parsing
566 582
567parsePat :: Prec -> P Pat 583parsePat :: Prec -> BodyParser Pat
568parsePat = \case 584parsePat = \case
569 PrecAnn -> 585 PrecAnn ->
570 patType <$> parsePat PrecOp <*> parseType (Just $ Wildcard SType) 586 patType <$> parsePat PrecOp <*> parseType (Just $ Wildcard SType)
@@ -626,7 +642,7 @@ telescopePat = fmap (getPPVars . ParPat . map snd &&& id) $ many $ uncurry f <$>
626 f h (PatType (ParPat [p]) t) = ((h, t), p) 642 f h (PatType (ParPat [p]) t) = ((h, t), p)
627 f h p = ((h, Wildcard SType), p) 643 f h p = ((h, Wildcard SType), p)
628 644
629checkPattern :: DBNames -> P () 645checkPattern :: DBNames -> BodyParser ()
630checkPattern ns = lift $ tell $ pure $ 646checkPattern ns = lift $ tell $ pure $
631 case [ns' | ns' <- group . sort . filter (not . null . snd) $ ns 647 case [ns' | ns' <- group . sort . filter (not . null . snd) $ ns
632 , not . null . tail $ ns'] of 648 , not . null . tail $ ns'] of
@@ -696,8 +712,8 @@ compilePatts ps gu = cp [] ps
696 vs' = map (fromMaybe 0) vs_ 712 vs' = map (fromMaybe 0) vs_
697 s = sum vs 713 s = sum vs
698 714
699compileGuardTrees ulend ge alts = compileGuardTree ulend SLabelEnd ge $ Alts alts 715compileGuardTrees ulend ge alts = compileGuardTree ulend SRHS ge $ Alts alts
700compileGuardTrees' ge alts = foldr1 (SAppV2 $ SBuiltin "parEval" `SAppV` Wildcard SType) $ compileGuardTree id SLabelEnd ge <$> alts 716compileGuardTrees' ge alts = foldr1 (SAppV2 $ SBuiltin "parEval" `SAppV` Wildcard SType) $ compileGuardTree id SRHS ge <$> alts
701 717
702compileGuardTree :: (SExp -> SExp) -> (SExp -> SExp) -> DesugarInfo -> GuardTree -> SExp 718compileGuardTree :: (SExp -> SExp) -> (SExp -> SExp) -> DesugarInfo -> GuardTree -> SExp
703compileGuardTree ulend lend adts t = (\x -> traceD (" ! :" ++ ppShow x) x) $ guardTreeToCases t 719compileGuardTree ulend lend adts t = (\x -> traceD (" ! :" ++ ppShow x) x) $ guardTreeToCases t
@@ -786,7 +802,7 @@ instance PShow Stmt where
786 802
787-------------------------------------------------------------------------------- declaration parsing 803-------------------------------------------------------------------------------- declaration parsing
788 804
789parseDef :: P [Stmt] 805parseDef :: BodyParser [Stmt]
790parseDef = 806parseDef =
791 do reserved "data" *> do 807 do reserved "data" *> do
792 x <- typeNS upperCase 808 x <- typeNS upperCase
@@ -839,7 +855,7 @@ parseDef =
839 <|> pure <$> funAltDef varId 855 <|> pure <$> funAltDef varId
840 <|> valueDef 856 <|> valueDef
841 where 857 where
842 telescopeDataFields :: P ([SIName], [(Visibility, SExp)]) 858 telescopeDataFields :: BodyParser ([SIName], [(Visibility, SExp)])
843 telescopeDataFields = dbfi <$> commaSep ((,) Visible <$> ((,) <$> lowerCase <*> parseType Nothing)) 859 telescopeDataFields = dbfi <$> commaSep ((,) Visible <$> ((,) <$> lowerCase <*> parseType Nothing))
844 860
845 mkData ge x ts t af cs = Data x ts t af (second snd <$> cs): concatMap mkProj (nub $ concat [fs | (_, (Just fs, _)) <- cs]) 861 mkData ge x ts t af cs = Data x ts t af (second snd <$> cs): concatMap mkProj (nub $ concat [fs | (_, (Just fs, _)) <- cs])
@@ -874,11 +890,11 @@ funAltDef parseName = do -- todo: use ns to determine parseName
874 checkPattern fee 890 checkPattern fee
875 FunAlt n tss <$> parseRHS (dbf' fee) "=" 891 FunAlt n tss <$> parseRHS (dbf' fee) "="
876 892
877valueDef :: P [Stmt] 893valueDef :: BodyParser [Stmt]
878valueDef = do 894valueDef = do
879 (dns, p) <- try "pattern" $ longPattern <* reservedOp "=" 895 (dns, p) <- try "pattern" $ longPattern <* reservedOp "="
880 checkPattern dns 896 checkPattern dns
881 e <- parseETerm PrecLam 897 e <- parseTerm PrecLam
882 ds <- dsInfo 898 ds <- dsInfo
883 return $ desugarValueDef ds p e 899 return $ desugarValueDef ds p e
884 900
@@ -971,7 +987,7 @@ desugarMutual ds xs = xs
971-} 987-}
972 988
973 989
974compileFunAlts' ds = fmap concat . sequence $ map (compileFunAlts (compileGuardTrees SLabelEnd) ds) $ groupBy h ds where 990compileFunAlts' ds = fmap concat . sequence $ map (compileFunAlts (compileGuardTrees SRHS) ds) $ groupBy h ds where
975 h (FunAlt n _ _) (FunAlt m _ _) = m == n 991 h (FunAlt n _ _) (FunAlt m _ _) = m == n
976 h _ _ = False 992 h _ _ = False
977 993
@@ -1044,7 +1060,7 @@ mkDesugarInfo ss =
1044 1060
1045data Export = ExportModule SIName | ExportId SIName 1061data Export = ExportModule SIName | ExportId SIName
1046 1062
1047--parseExport :: P Export 1063--parseExport :: BodyParser Export
1048parseExport = 1064parseExport =
1049 ExportModule <$ reserved "module" <*> moduleName 1065 ExportModule <$ reserved "module" <*> moduleName
1050 <|> ExportId <$> varId 1066 <|> ExportId <$> varId
@@ -1069,7 +1085,7 @@ data Extension
1069extensionMap :: Map.Map String Extension 1085extensionMap :: Map.Map String Extension
1070extensionMap = Map.fromList $ map (show &&& id) [toEnum 0 .. ] 1086extensionMap = Map.fromList $ map (show &&& id) [toEnum 0 .. ]
1071 1087
1072--parseExtensions :: P [Extension] 1088--parseExtensions :: BodyParser [Extension]
1073parseExtensions 1089parseExtensions
1074 = try "pragma" (symbol "{-#") *> symbol "LANGUAGE" *> commaSep (lexeme ext) <* symbolWithoutSpace "#-}" <* simpleSpace 1090 = try "pragma" (symbol "{-#") *> symbol "LANGUAGE" *> commaSep (lexeme ext) <* symbolWithoutSpace "#-}" <* simpleSpace
1075 where 1091 where
@@ -1153,7 +1169,7 @@ type NameDB a = StateT [String] (Reader [String]) a
1153showDoc :: Doc -> String 1169showDoc :: Doc -> String
1154showDoc = str . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) 1170showDoc = str . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z'])
1155 1171
1156showDoc_ :: Doc -> P.Doc 1172showDoc_ :: Doc -> BodyParser.Doc
1157showDoc_ = text . str . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) 1173showDoc_ = text . str . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z'])
1158 1174
1159sExpDoc :: Up a => SExp' a -> Doc 1175sExpDoc :: Up a => SExp' a -> Doc