summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCsaba Hruska <csaba.hruska@gmail.com>2017-05-28 22:16:40 -0600
committerCsaba Hruska <csaba.hruska@gmail.com>2017-05-28 22:16:40 -0600
commit7fd48262bde5664d1c543a45be948e0f22d18bd7 (patch)
tree9e97d82c6feade4152835ab84f951cfba9475040
parent233f1b8d67f3d5158792cb3f5b2cb17a03fdaf5b (diff)
type signatures
-rw-r--r--src/LambdaCube/Compiler/DesugaredSource.hs72
1 files changed, 70 insertions, 2 deletions
diff --git a/src/LambdaCube/Compiler/DesugaredSource.hs b/src/LambdaCube/Compiler/DesugaredSource.hs
index 9e0949b0..fef5a1d5 100644
--- a/src/LambdaCube/Compiler/DesugaredSource.hs
+++ b/src/LambdaCube/Compiler/DesugaredSource.hs
@@ -33,6 +33,7 @@ type SName = String
33pattern Ticked :: SName -> SName 33pattern Ticked :: SName -> SName
34pattern Ticked s = '\'': s 34pattern Ticked s = '\'': s
35 35
36untick :: SName -> SName
36untick (Ticked s) = s 37untick (Ticked s) = s
37untick s = s 38untick s = s
38 39
@@ -99,6 +100,7 @@ instance PShow Range
99 : map text (drop (r - 1) $ take r' $ lines $ fileContent n) 100 : map text (drop (r - 1) $ take r' $ lines $ fileContent n)
100 ++ [text $ replicate (c - 1) ' ' ++ replicate (c' - c) '^' | r' == r] 101 ++ [text $ replicate (c - 1) ' ' ++ replicate (c' - c) '^' | r' == r]
101 102
103showRangeWithoutFileName :: Range -> Doc
102showRangeWithoutFileName (Range _ b e) = pShow b <> "-" <> pShow e 104showRangeWithoutFileName (Range _ b e) = pShow b <> "-" <> pShow e
103 105
104joinRange :: Range -> Range -> Range 106joinRange :: Range -> Range -> Range
@@ -111,6 +113,7 @@ data SI
111 = NoSI (Set.Set String) -- no source info, attached debug info 113 = NoSI (Set.Set String) -- no source info, attached debug info
112 | RangeSI Range 114 | RangeSI Range
113 115
116getRange :: SI -> Maybe Range
114getRange (RangeSI r) = Just r 117getRange (RangeSI r) = Just r
115getRange _ = Nothing 118getRange _ = Nothing
116 119
@@ -132,8 +135,10 @@ instance PShow SI where
132hashPos :: FileInfo -> SPos -> Int 135hashPos :: FileInfo -> SPos -> Int
133hashPos fn (SPos_ p) = (1 + fileId fn) `shiftL` 32 .|. p 136hashPos fn (SPos_ p) = (1 + fileId fn) `shiftL` 32 .|. p
134 137
138debugSI :: String -> SI
135debugSI a = NoSI (Set.singleton a) 139debugSI a = NoSI (Set.singleton a)
136 140
141validate :: SI -> [SI] -> SI
137si@(RangeSI r) `validate` xs | r `notElem` [r | RangeSI r <- xs] = si 142si@(RangeSI r) `validate` xs | r `notElem` [r | RangeSI r <- xs] = si
138_ `validate` _ = mempty 143_ `validate` _ = mempty
139 144
@@ -155,9 +160,11 @@ class SetSourceInfo a where
155 160
156data SIName = SIName__ { nameHash :: Int, nameSI :: SI, nameFixity :: Maybe Fixity, sName :: SName } 161data SIName = SIName__ { nameHash :: Int, nameSI :: SI, nameFixity :: Maybe Fixity, sName :: SName }
157 162
163pattern SIName_ :: SI -> Maybe Fixity -> SName -> SIName
158pattern SIName_ si f n <- SIName__ _ si f n 164pattern SIName_ si f n <- SIName__ _ si f n
159 where SIName_ si f n = SIName__ (fnameHash si n) si f n 165 where SIName_ si f n = SIName__ (fnameHash si n) si f n
160 166
167pattern SIName_ :: SI -> SName -> SIName
161pattern SIName si n <- SIName_ si _ n 168pattern SIName si n <- SIName_ si _ n
162 where SIName si n = SIName_ si Nothing n 169 where SIName si n = SIName_ si Nothing n
163 170
@@ -220,6 +227,7 @@ data FNameTag
220 | F_rhs | F_section 227 | F_rhs | F_section
221 deriving (Eq, Ord, Show, Enum, Bounded) 228 deriving (Eq, Ord, Show, Enum, Bounded)
222 229
230tagName :: FNameTag -> String
223tagName FCons = ":" 231tagName FCons = ":"
224tagName t = case show t of 'F': s -> s 232tagName t = case show t of 'F': s -> s
225 233
@@ -227,15 +235,19 @@ pattern Tag :: FNameTag -> SIName
227pattern Tag t <- (toTag . nameHash -> Just t) 235pattern Tag t <- (toTag . nameHash -> Just t)
228 where Tag t = SIName__ (fromEnum t) (tagSI t) (tagFixity t) (tagName t) 236 where Tag t = SIName__ (fromEnum t) (tagSI t) (tagFixity t) (tagName t)
229 237
238pattern FTag :: FNameTag -> FName
230pattern FTag t = FName (Tag t) 239pattern FTag t = FName (Tag t)
231 240
241toTag :: Int -> Maybe FNameTag
232toTag i 242toTag i
233 | i <= fromEnum (maxBound :: FNameTag) = Just (toEnum i) 243 | i <= fromEnum (maxBound :: FNameTag) = Just (toEnum i)
234 | otherwise = Nothing 244 | otherwise = Nothing
235 245
246tagFixity :: FNameTag -> Maybe Fixity
236tagFixity FCons = Just $ InfixR 5 247tagFixity FCons = Just $ InfixR 5
237tagFixity _ = Nothing 248tagFixity _ = Nothing
238 249
250tagSI :: FNameTag -> SI
239tagSI t = NoSI $ Set.singleton $ tagName t 251tagSI t = NoSI $ Set.singleton $ tagName t
240 252
241fnameHash :: SI -> SName -> Int 253fnameHash :: SI -> SName -> Int
@@ -274,6 +286,7 @@ data SExp' a
274 | STyped a 286 | STyped a
275 deriving (Eq) 287 deriving (Eq)
276 288
289sLHS :: SIName -> SExp' a -> SExp' a
277sLHS _ (SRHS x) = x 290sLHS _ (SRHS x) = x
278sLHS n x = SLHS n x 291sLHS n x = SLHS n x
279 292
@@ -299,31 +312,57 @@ instance PShow Visibility where
299 Hidden -> "Hidden" 312 Hidden -> "Hidden"
300 Visible -> "Visible" 313 Visible -> "Visible"
301 314
315dummyName :: String -> SIName
302dummyName s = SIName (debugSI s) "" --("v_" ++ s) 316dummyName s = SIName (debugSI s) "" --("v_" ++ s)
317
318dummyName' :: String -> SData SIName
303dummyName' = SData . dummyName 319dummyName' = SData . dummyName
320
321sVar :: String -> Int -> SExp' a
304sVar = SVar . dummyName 322sVar = SVar . dummyName
305 323
324pattern SBind :: Binder -> SData SIName -> SExp' a -> SExp' a -> SExp' a
306pattern SBind v x a b <- SBind_ _ v x a b 325pattern SBind v x a b <- SBind_ _ v x a b
307 where SBind v x a b = SBind_ (sourceInfo a <> sourceInfo b) v x a b 326 where SBind v x a b = SBind_ (sourceInfo a <> sourceInfo b) v x a b
327
328pattern SPi :: Visibility -> SExp' a -> SExp' a -> SExp' a
308pattern SPi h a b <- SBind (BPi h) _ a b 329pattern SPi h a b <- SBind (BPi h) _ a b
309 where SPi h a b = SBind (BPi h) (dummyName' "SPi") a b 330 where SPi h a b = SBind (BPi h) (dummyName' "SPi") a b
331
332pattern SLam :: Visibility -> SExp' a -> SExp' a -> SExp' a
310pattern SLam h a b <- SBind (BLam h) _ a b 333pattern SLam h a b <- SBind (BLam h) _ a b
311 where SLam h a b = SBind (BLam h) (dummyName' "SLam") a b 334 where SLam h a b = SBind (BLam h) (dummyName' "SLam") a b
335
336pattern Wildcard :: SExp' a -> SExp' a
312pattern Wildcard t <- SBind BMeta _ t (SVar _ 0) 337pattern Wildcard t <- SBind BMeta _ t (SVar _ 0)
313 where Wildcard t = SBind BMeta (dummyName' "Wildcard") t (sVar "Wildcard2" 0) 338 where Wildcard t = SBind BMeta (dummyName' "Wildcard") t (sVar "Wildcard2" 0)
339
340pattern SLet :: SIName -> SExp' a -> SExp' a -> SExp' a
314pattern SLet n a b <- SLet_ _ (SData n) a b 341pattern SLet n a b <- SLet_ _ (SData n) a b
315 where SLet n a b = SLet_ (sourceInfo a <> sourceInfo b) (SData n) a b 342 where SLet n a b = SLet_ (sourceInfo a <> sourceInfo b) (SData n) a b
343
344pattern SLamV :: SExp' a -> SExp' a
316pattern SLamV a = SLam Visible (Wildcard SType) a 345pattern SLamV a = SLam Visible (Wildcard SType) a
346
347pattern SVar :: SIName -> Int -> SExp' a
317pattern SVar a b = SVar_ (SData a) b 348pattern SVar a b = SVar_ (SData a) b
318 349
350pattern SApp :: Visibility -> SExp' a -> SExp' a -> SExp' a
319pattern SApp h a b <- SApp_ _ h a b 351pattern SApp h a b <- SApp_ _ h a b
320 where SApp h a b = SApp_ (sourceInfo a <> sourceInfo b) h a b 352 where SApp h a b = SApp_ (sourceInfo a <> sourceInfo b) h a b
353
354pattern SAppH :: SExp' a -> SExp' a -> SExp' a
321pattern SAppH a b = SApp Hidden a b 355pattern SAppH a b = SApp Hidden a b
356
357pattern SAppV :: SExp' a -> SExp' a -> SExp' a
322pattern SAppV a b = SApp Visible a b 358pattern SAppV a b = SApp Visible a b
359
360pattern SAppV2 :: SExp' a -> SExp' a -> SExp' a -> SExp' a
323pattern SAppV2 f a b = f `SAppV` a `SAppV` b 361pattern SAppV2 f a b = f `SAppV` a `SAppV` b
324 362
325infixl 2 `SAppV`, `SAppH` 363infixl 2 `SAppV`, `SAppH`
326 364------------------------------------------------------------------------------------------------- ****************************************
365pattern SBuiltin :: FNameTag -> SExp' a
327pattern SBuiltin s = SGlobal (Tag s) 366pattern SBuiltin s = SGlobal (Tag s)
328 367
329pattern SRHS a = SBuiltin F_rhs `SAppV` a 368pattern SRHS a = SBuiltin F_rhs `SAppV` a
@@ -332,9 +371,11 @@ pattern SType = SBuiltin F'Type
332pattern SConstraint = SBuiltin F'Constraint 371pattern SConstraint = SBuiltin F'Constraint
333pattern Parens e = SBuiltin Fparens `SAppV` e 372pattern Parens e = SBuiltin Fparens `SAppV` e
334pattern SAnn a t = SBuiltin FtypeAnn `SAppH` t `SAppV` a 373pattern SAnn a t = SBuiltin FtypeAnn `SAppH` t `SAppV` a
335pattern TyType a = SAnn a SType
336pattern SCW a = SBuiltin F'CW `SAppV` a 374pattern SCW a = SBuiltin F'CW `SAppV` a
337 375
376pattern TyType :: SExp' a -> SExp' a
377pattern TyType a = SAnn a SType
378
338-- builtin heterogenous list 379-- builtin heterogenous list
339pattern HList a = SBuiltin F'HList `SAppV` a 380pattern HList a = SBuiltin F'HList `SAppV` a
340pattern HCons a b = SBuiltin FHCons `SAppV` a `SAppV` b 381pattern HCons a b = SBuiltin FHCons `SAppV` a `SAppV` b
@@ -345,15 +386,19 @@ pattern BList a = SBuiltin F'List `SAppV` a
345pattern BCons a b = SBuiltin FCons `SAppV` a `SAppV` b 386pattern BCons a b = SBuiltin FCons `SAppV` a `SAppV` b
346pattern BNil = SBuiltin FNil 387pattern BNil = SBuiltin FNil
347 388
389getTTuple :: SExp' a -> [SExp' a]
348getTTuple (HList (getList -> Just xs)) = xs 390getTTuple (HList (getList -> Just xs)) = xs
349getTTuple x = [x] 391getTTuple x = [x]
350 392
393getList :: SExp' a -> Maybe [SExp' a]
351getList BNil = Just [] 394getList BNil = Just []
352getList (BCons x (getList -> Just y)) = Just (x:y) 395getList (BCons x (getList -> Just y)) = Just (x:y)
353getList _ = Nothing 396getList _ = Nothing
354 397
398sLit :: Lit -> SExp' a
355sLit = SLit mempty 399sLit = SLit mempty
356 400
401isPi :: Binder -> Bool
357isPi (BPi _) = True 402isPi (BPi _) = True
358isPi _ = False 403isPi _ = False
359 404
@@ -361,6 +406,7 @@ pattern UncurryS :: [(Visibility, SExp' a)] -> SExp' a -> SExp' a
361pattern UncurryS ps t <- (getParamsS -> (ps, t)) 406pattern UncurryS ps t <- (getParamsS -> (ps, t))
362 where UncurryS ps t = foldr (uncurry SPi) t ps 407 where UncurryS ps t = foldr (uncurry SPi) t ps
363 408
409getParamsS :: SExp' a -> ([(Visibility, SExp' a)], SExp' a)
364getParamsS (SPi h t x) = first ((h, t):) $ getParamsS x 410getParamsS (SPi h t x) = first ((h, t):) $ getParamsS x
365getParamsS x = ([], x) 411getParamsS x = ([], x)
366 412
@@ -368,11 +414,13 @@ pattern AppsS :: SExp' a -> [(Visibility, SExp' a)] -> SExp' a
368pattern AppsS f args <- (getApps -> (f, args)) 414pattern AppsS f args <- (getApps -> (f, args))
369 where AppsS = foldl $ \a (v, b) -> SApp v a b 415 where AppsS = foldl $ \a (v, b) -> SApp v a b
370 416
417getApps :: SExp' a -> (SExp' a, [(Visibility, SExp' a)])
371getApps = second reverse . run where 418getApps = second reverse . run where
372 run (SApp h a b) = second ((h, b):) $ run a 419 run (SApp h a b) = second ((h, b):) $ run a
373 run x = (x, []) 420 run x = (x, [])
374 421
375-- todo: remove 422-- todo: remove
423downToS :: String -> Int -> Int -> [SExp' a]
376downToS err n m = [sVar (err ++ "_" ++ show i) (n + i) | i <- [m-1, m-2..0]] 424downToS err n m = [sVar (err ++ "_" ++ show i) (n + i) | i <- [m-1, m-2..0]]
377 425
378instance SourceInfo (SExp' a) where 426instance SourceInfo (SExp' a) where
@@ -417,6 +465,7 @@ foldS h g f = fs
417 SLHS _ x -> fs i x 465 SLHS _ x -> fs i x
418 STyped x -> h i x 466 STyped x -> h i x
419 467
468foldName :: Monoid m => (SIName -> m) -> SExp' Void -> m
420foldName f = foldS (\_ -> elimVoid) (\sn _ -> f sn) mempty 0 469foldName f = foldS (\_ -> elimVoid) (\sn _ -> f sn) mempty 0
421 470
422usedS :: SIName -> SExp -> Bool 471usedS :: SIName -> SExp -> Bool
@@ -485,19 +534,25 @@ instance (HasFreeVars a, PShow a) => PShow (SExp' a) where
485 SVar _ i -> shVar i 534 SVar _ i -> shVar i
486 SLit _ l -> pShow l 535 SLit _ l -> pShow l
487 536
537shApp :: Visibility -> Doc -> Doc -> Doc
488shApp Visible a b = DApp a b 538shApp Visible a b = DApp a b
489shApp Hidden a b = DApp a (DAt b) 539shApp Hidden a b = DApp a (DAt b)
490 540
541usedVar' :: HasFreeVars a => Int -> a -> x -> Maybe x
491usedVar' a b s | usedVar a b = Just s 542usedVar' a b s | usedVar a b = Just s
492 | otherwise = Nothing 543 | otherwise = Nothing
493 544
545shLam :: Maybe String -> Binder -> Doc -> Doc -> Doc
494shLam usedVar h a b = shLam_ usedVar h (Just a) b 546shLam usedVar h a b = shLam_ usedVar h (Just a) b
495 547
548simpleFo :: Doc -> Doc
496simpleFo (DExpand x _) = x 549simpleFo (DExpand x _) = x
497simpleFo x = x 550simpleFo x = x
498 551
552shLam_ :: Maybe String -> Binder -> Maybe Doc -> Doc -> Doc
499shLam_ Nothing (BPi Hidden) (Just ((simpleFo -> DText "'CW") `DApp` a)) b = DFreshName Nothing $ showContext (DUp 0 a) b 553shLam_ Nothing (BPi Hidden) (Just ((simpleFo -> DText "'CW") `DApp` a)) b = DFreshName Nothing $ showContext (DUp 0 a) b
500 where 554 where
555 showContext :: Doc -> Doc -> Doc
501 showContext x (DFreshName u d) = DFreshName u $ showContext (DUp 0 x) d 556 showContext x (DFreshName u d) = DFreshName u $ showContext (DUp 0 x) d
502 showContext x (DParContext xs y) = DParContext (DComma x xs) y 557 showContext x (DParContext xs y) = DParContext (DComma x xs) y
503 showContext x (DContext xs y) = DParContext (DComma x xs) y 558 showContext x (DContext xs y) = DParContext (DComma x xs) y
@@ -505,6 +560,7 @@ shLam_ Nothing (BPi Hidden) (Just ((simpleFo -> DText "'CW") `DApp` a)) b = DFre
505 560
506shLam_ usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 <$> a) b 561shLam_ usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 <$> a) b
507 where 562 where
563 lam :: Doc -> Doc -> Doc
508 lam = case h of 564 lam = case h of
509 BPi Visible 565 BPi Visible
510 | isJust usedVar -> showForall "->" 566 | isJust usedVar -> showForall "->"
@@ -514,33 +570,42 @@ shLam_ usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 <$> a) b
514 | otherwise -> showContext 570 | otherwise -> showContext
515 _ -> showLam 571 _ -> showLam
516 572
573 shAnn' :: Doc -> Maybe Doc -> Doc
517 shAnn' a = maybe a (shAnn a) 574 shAnn' a = maybe a (shAnn a)
518 575
576 p :: Maybe Doc -> Doc
519 p = case h of 577 p = case h of
520 BMeta -> shAnn' (blue $ DVar 0) 578 BMeta -> shAnn' (blue $ DVar 0)
521 BLam Hidden -> DAt . shAnn' (DVar 0) 579 BLam Hidden -> DAt . shAnn' (DVar 0)
522 BLam Visible -> shAnn' (DVar 0) 580 BLam Visible -> shAnn' (DVar 0)
523 _ -> ann 581 _ -> ann
524 582
583 ann :: Maybe Doc -> Doc
525 ann | isJust usedVar = shAnn' (DVar 0) 584 ann | isJust usedVar = shAnn' (DVar 0)
526 | otherwise = fromMaybe (text "Type") 585 | otherwise = fromMaybe (text "Type")
527 586
587 showForall :: String -> Doc -> Doc -> Doc
528 showForall s x (DFreshName u d) = DFreshName u $ showForall s (DUp 0 x) d 588 showForall s x (DFreshName u d) = DFreshName u $ showForall s (DUp 0 x) d
529 showForall s x (DForall s' xs y) | s == s' = DForall s (DSep (InfixR 11) x xs) y 589 showForall s x (DForall s' xs y) | s == s' = DForall s (DSep (InfixR 11) x xs) y
530 showForall s x y = DForall s x y 590 showForall s x y = DForall s x y
531 591
592 showContext :: Doc -> Doc -> Doc
532 showContext x y = DContext' x y 593 showContext x y = DContext' x y
533 594
595showLam :: Doc -> Doc -> Doc
534showLam x (DFreshName u d) = DFreshName u $ showLam (DUp 0 x) d 596showLam x (DFreshName u d) = DFreshName u $ showLam (DUp 0 x) d
535showLam x (DLam xs y) = DLam (DSep (InfixR 11) x xs) y 597showLam x (DLam xs y) = DLam (DSep (InfixR 11) x xs) y
536showLam x y = DLam x y 598showLam x y = DLam x y
537 599
600shLet :: Int -> Doc -> Doc -> Doc
538shLet i a b = DLet' (DLet "=" (blue $ shVar i) $ DUp i a) (DUp i b) 601shLet i a b = DLet' (DLet "=" (blue $ shVar i) $ DUp i a) (DUp i b)
539 602
603showLet :: Doc -> Doc -> Doc
540showLet x (DFreshName u d) = DFreshName u $ showLet (DUp 0 x) d 604showLet x (DFreshName u d) = DFreshName u $ showLet (DUp 0 x) d
541showLet x (DLet' xs y) = DLet' (DSemi x xs) y 605showLet x (DLet' xs y) = DLet' (DSemi x xs) y
542showLet x y = DLet' x y 606showLet x y = DLet' x y
543 607
608shLet_ :: Doc -> Doc -> Doc
544shLet_ a b = DFreshName (Just "") $ showLet (DLet "=" (shVar 0) $ DUp 0 a) b 609shLet_ a b = DFreshName (Just "") $ showLet (DLet "=" (shVar 0) $ DUp 0 a) b
545 610
546-------------------------------------------------------------------------------- statement 611-------------------------------------------------------------------------------- statement
@@ -550,12 +615,15 @@ data Stmt
550 | Data SIName [(Visibility, SExp)]{-parameters-} SExp{-type-} [(SIName, SExp)]{-constructor names and types-} 615 | Data SIName [(Visibility, SExp)]{-parameters-} SExp{-type-} [(SIName, SExp)]{-constructor names and types-}
551 | PrecDef SIName Fixity 616 | PrecDef SIName Fixity
552 617
618pattern StLet :: SIName -> Maybe (SExp' Void) -> SExp' Void -> Stmt
553pattern StLet n mt x <- StmtLet n (getSAnn -> (x, mt)) 619pattern StLet n mt x <- StmtLet n (getSAnn -> (x, mt))
554 where StLet n mt x = StmtLet n $ maybe x (SAnn x) mt 620 where StLet n mt x = StmtLet n $ maybe x (SAnn x) mt
555 621
622getSAnn :: SExp' a -> (SExp' a, Maybe (SExp' a))
556getSAnn (SAnn x t) = (x, Just t) 623getSAnn (SAnn x t) = (x, Just t)
557getSAnn x = (x, Nothing) 624getSAnn x = (x, Nothing)
558 625
626pattern Primitive :: SIName -> SExp' Void -> Stmt
559pattern Primitive n t = StLet n (Just t) (SBuiltin Fundefined) 627pattern Primitive n t = StLet n (Just t) (SBuiltin Fundefined)
560 628
561instance PShow Stmt where 629instance PShow Stmt where