diff options
Diffstat (limited to 'src/LambdaCube')
-rw-r--r-- | src/LambdaCube/Compiler/Core.hs | 6 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/DesugaredSource.hs | 19 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Infer.hs | 6 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Pretty.hs | 33 |
4 files changed, 44 insertions, 20 deletions
diff --git a/src/LambdaCube/Compiler/Core.hs b/src/LambdaCube/Compiler/Core.hs index b6a1c1c4..181d07e8 100644 --- a/src/LambdaCube/Compiler/Core.hs +++ b/src/LambdaCube/Compiler/Core.hs | |||
@@ -412,9 +412,9 @@ instance MkDoc ExpType where | |||
412 | 412 | ||
413 | instance MkDoc Exp where | 413 | instance MkDoc Exp where |
414 | mkDoc pr@(reduce, body) = \case | 414 | mkDoc pr@(reduce, body) = \case |
415 | Lam b -> shLam_ (usedVar 0 b) (BLam Visible) Nothing (mkDoc pr b) | 415 | Lam b -> shLam_ (usedVar' 0 b "") (BLam Visible) Nothing (mkDoc pr b) |
416 | Pi h TType b -> shLam_ (usedVar 0 b) (BPi h) Nothing (mkDoc pr b) | 416 | Pi h TType b -> shLam_ (usedVar' 0 b "") (BPi h) Nothing (mkDoc pr b) |
417 | Pi h a b -> shLam (usedVar 0 b) (BPi h) (mkDoc pr a) (mkDoc pr b) | 417 | Pi h a b -> shLam (usedVar' 0 b "") (BPi h) (mkDoc pr a) (mkDoc pr b) |
418 | ENat n -> pShow n | 418 | ENat n -> pShow n |
419 | Con s@(ConName _ i _) _ _ | body -> text $ "<<" ++ showNth i ++ " constructor of " ++ show (conTypeName s) ++ ">>" | 419 | Con s@(ConName _ i _) _ _ | body -> text $ "<<" ++ showNth i ++ " constructor of " ++ show (conTypeName s) ++ ">>" |
420 | ConN FHCons (xs: x: _{-2-}) -> foldl DApp (text "HCons") (mkDoc pr <$> [x, xs]) | 420 | ConN FHCons (xs: x: _{-2-}) -> foldl DApp (text "HCons") (mkDoc pr <$> [x, xs]) |
diff --git a/src/LambdaCube/Compiler/DesugaredSource.hs b/src/LambdaCube/Compiler/DesugaredSource.hs index db2c1f24..7bb5ba96 100644 --- a/src/LambdaCube/Compiler/DesugaredSource.hs +++ b/src/LambdaCube/Compiler/DesugaredSource.hs | |||
@@ -298,7 +298,7 @@ instance PShow Visibility where | |||
298 | Hidden -> "Hidden" | 298 | Hidden -> "Hidden" |
299 | Visible -> "Visible" | 299 | Visible -> "Visible" |
300 | 300 | ||
301 | dummyName s = SIName (debugSI s) ("v_" ++ s) | 301 | dummyName s = SIName (debugSI s) "" --("v_" ++ s) |
302 | dummyName' = SData . dummyName | 302 | dummyName' = SData . dummyName |
303 | sVar = SVar . dummyName | 303 | sVar = SVar . dummyName |
304 | 304 | ||
@@ -476,8 +476,8 @@ instance (HasFreeVars a, PShow a) => PShow (SExp' a) where | |||
476 | SApp h a b -> shApp h (pShow a) (pShow b) | 476 | SApp h a b -> shApp h (pShow a) (pShow b) |
477 | Wildcard SType -> text "_" | 477 | Wildcard SType -> text "_" |
478 | Wildcard t -> shAnn (text "_") (pShow t) | 478 | Wildcard t -> shAnn (text "_") (pShow t) |
479 | SBind_ _ h _ SType b -> shLam_ (usedVar 0 b) h Nothing (pShow b) | 479 | SBind_ _ h (SData n) SType b -> shLam_ (usedVar' 0 b $ sName n) h Nothing (pShow b) |
480 | SBind_ _ h _ a b -> shLam (usedVar 0 b) h (pShow a) (pShow b) | 480 | SBind_ _ h (SData n) a b -> shLam (usedVar' 0 b $ sName n) h (pShow a) (pShow b) |
481 | SLet _ a b -> shLet_ (pShow a) (pShow b) | 481 | SLet _ a b -> shLet_ (pShow a) (pShow b) |
482 | SLHS n x -> "_lhs" `DApp` pShow n `DApp` pShow x | 482 | SLHS n x -> "_lhs" `DApp` pShow n `DApp` pShow x |
483 | STyped a -> pShow a | 483 | STyped a -> pShow a |
@@ -487,12 +487,15 @@ instance (HasFreeVars a, PShow a) => PShow (SExp' a) where | |||
487 | shApp Visible a b = DApp a b | 487 | shApp Visible a b = DApp a b |
488 | shApp Hidden a b = DApp a (DAt b) | 488 | shApp Hidden a b = DApp a (DAt b) |
489 | 489 | ||
490 | usedVar' a b s | usedVar a b = Just s | ||
491 | | otherwise = Nothing | ||
492 | |||
490 | shLam usedVar h a b = shLam_ usedVar h (Just a) b | 493 | shLam usedVar h a b = shLam_ usedVar h (Just a) b |
491 | 494 | ||
492 | simpleFo (DExpand x _) = x | 495 | simpleFo (DExpand x _) = x |
493 | simpleFo x = x | 496 | simpleFo x = x |
494 | 497 | ||
495 | shLam_ False (BPi Hidden) (Just ((simpleFo -> DText "'CW") `DApp` a)) b = DFreshName False $ showContext (DUp 0 a) b | 498 | shLam_ Nothing (BPi Hidden) (Just ((simpleFo -> DText "'CW") `DApp` a)) b = DFreshName Nothing $ showContext (DUp 0 a) b |
496 | where | 499 | where |
497 | showContext x (DFreshName u d) = DFreshName u $ showContext (DUp 0 x) d | 500 | showContext x (DFreshName u d) = DFreshName u $ showContext (DUp 0 x) d |
498 | showContext x (DParContext xs y) = DParContext (DComma x xs) y | 501 | showContext x (DParContext xs y) = DParContext (DComma x xs) y |
@@ -503,10 +506,10 @@ shLam_ usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 <$> a) b | |||
503 | where | 506 | where |
504 | lam = case h of | 507 | lam = case h of |
505 | BPi Visible | 508 | BPi Visible |
506 | | usedVar -> showForall "->" | 509 | | isJust usedVar -> showForall "->" |
507 | | otherwise -> shArr | 510 | | otherwise -> shArr |
508 | BPi Hidden | 511 | BPi Hidden |
509 | | usedVar -> showForall "." | 512 | | isJust usedVar -> showForall "." |
510 | | otherwise -> showContext | 513 | | otherwise -> showContext |
511 | _ -> showLam | 514 | _ -> showLam |
512 | 515 | ||
@@ -518,7 +521,7 @@ shLam_ usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 <$> a) b | |||
518 | BLam Visible -> shAnn' (DVar 0) | 521 | BLam Visible -> shAnn' (DVar 0) |
519 | _ -> ann | 522 | _ -> ann |
520 | 523 | ||
521 | ann | usedVar = shAnn' (DVar 0) | 524 | ann | isJust usedVar = shAnn' (DVar 0) |
522 | | otherwise = fromMaybe (text "Type") | 525 | | otherwise = fromMaybe (text "Type") |
523 | 526 | ||
524 | showForall s x (DFreshName u d) = DFreshName u $ showForall s (DUp 0 x) d | 527 | showForall s x (DFreshName u d) = DFreshName u $ showForall s (DUp 0 x) d |
@@ -537,7 +540,7 @@ showLet x (DFreshName u d) = DFreshName u $ showLet (DUp 0 x) d | |||
537 | showLet x (DLet' xs y) = DLet' (DSemi x xs) y | 540 | showLet x (DLet' xs y) = DLet' (DSemi x xs) y |
538 | showLet x y = DLet' x y | 541 | showLet x y = DLet' x y |
539 | 542 | ||
540 | shLet_ a b = DFreshName True $ showLet (DLet "=" (shVar 0) $ DUp 0 a) b | 543 | shLet_ a b = DFreshName (Just "") $ showLet (DLet "=" (shVar 0) $ DUp 0 a) b |
541 | 544 | ||
542 | -------------------------------------------------------------------------------- statement | 545 | -------------------------------------------------------------------------------- statement |
543 | 546 | ||
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs index 5067487c..f2889dd0 100644 --- a/src/LambdaCube/Compiler/Infer.hs +++ b/src/LambdaCube/Compiler/Infer.hs | |||
@@ -83,8 +83,8 @@ showEnvSExpType e c t = show $ envDoc e $ underline $ (shAnn (pShow c) (pShow t) | |||
83 | envDoc :: Env -> Doc -> Doc | 83 | envDoc :: Env -> Doc -> Doc |
84 | envDoc x m = case x of | 84 | envDoc x m = case x of |
85 | EGlobal{} -> m | 85 | EGlobal{} -> m |
86 | EBind1 _ h ts b -> envDoc ts $ shLam (usedVar 0 b) h m (pShow b) | 86 | EBind1 _ h ts b -> envDoc ts $ shLam (usedVar' 0 b "") h m (pShow b) |
87 | EBind2 h a ts -> envDoc ts $ shLam True h (pShow a) m | 87 | EBind2 h a ts -> envDoc ts $ shLam (Just "") h (pShow a) m |
88 | EApp1 _ h ts b -> envDoc ts $ shApp h m (pShow b) | 88 | EApp1 _ h ts b -> envDoc ts $ shApp h m (pShow b) |
89 | EApp2 _ h (ET (Lam (Var 0)) (Pi Visible TType _)) ts -> envDoc ts $ shApp h (text "tyType") m | 89 | EApp2 _ h (ET (Lam (Var 0)) (Pi Visible TType _)) ts -> envDoc ts $ shApp h (text "tyType") m |
90 | EApp2 _ h a ts -> envDoc ts $ shApp h (pShow a) m | 90 | EApp2 _ h a ts -> envDoc ts $ shApp h (pShow a) m |
@@ -102,7 +102,7 @@ envDoc x m = case x of | |||
102 | instance MkDoc a => MkDoc (CEnv a) where | 102 | instance MkDoc a => MkDoc (CEnv a) where |
103 | mkDoc pr = \case | 103 | mkDoc pr = \case |
104 | MEnd a -> mkDoc pr a | 104 | MEnd a -> mkDoc pr a |
105 | Meta a b -> shLam True BMeta (mkDoc pr a) (mkDoc pr b) | 105 | Meta a b -> shLam (Just "") BMeta (mkDoc pr a) (mkDoc pr b) |
106 | Assign i (ET x _) e -> shLet i (mkDoc pr x) (mkDoc pr e) | 106 | Assign i (ET x _) e -> shLet i (mkDoc pr x) (mkDoc pr e) |
107 | 107 | ||
108 | -------------------------------------------------------------------------------- constraints env | 108 | -------------------------------------------------------------------------------- constraints env |
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs index 12964b6b..a82f7ce6 100644 --- a/src/LambdaCube/Compiler/Pretty.hs +++ b/src/LambdaCube/Compiler/Pretty.hs | |||
@@ -18,7 +18,7 @@ import Data.String | |||
18 | import Data.Char | 18 | import Data.Char |
19 | import Data.Monoid | 19 | import Data.Monoid |
20 | import qualified Data.Set as Set | 20 | import qualified Data.Set as Set |
21 | --import qualified Data.Map as Map | 21 | import qualified Data.Map.Strict as Map |
22 | import Control.Applicative | 22 | import Control.Applicative |
23 | import Control.Monad.Identity | 23 | import Control.Monad.Identity |
24 | import Control.Monad.Reader | 24 | import Control.Monad.Reader |
@@ -71,7 +71,7 @@ data Doc | |||
71 | | DInfix Fixity Doc DocAtom Doc | 71 | | DInfix Fixity Doc DocAtom Doc |
72 | | DPreOp Int DocAtom Doc | 72 | | DPreOp Int DocAtom Doc |
73 | 73 | ||
74 | | DFreshName Bool{-used-} Doc | 74 | | DFreshName (Maybe String){-used-} Doc |
75 | | DVar Int | 75 | | DVar Int |
76 | | DUp Int Doc | 76 | | DUp Int Doc |
77 | | DResetFreshNames Doc | 77 | | DResetFreshNames Doc |
@@ -104,6 +104,16 @@ plainShow = ($ "") . P.displayS . P.renderPretty 0.6 150 . P.plain . renderDoc . | |||
104 | simpleShow :: PShow a => a -> String | 104 | simpleShow :: PShow a => a -> String |
105 | simpleShow = ($ "") . P.displayS . P.renderPretty 0.4 200 . P.plain . renderDoc . pShow | 105 | simpleShow = ($ "") . P.displayS . P.renderPretty 0.4 200 . P.plain . renderDoc . pShow |
106 | 106 | ||
107 | mkFreshName :: MonadState (Map.Map String Int, [String]) m => String -> m String | ||
108 | mkFreshName n = state $ addIndex n | ||
109 | where | ||
110 | addIndex "" (m, (n:ns)) = add n (m, ns) | ||
111 | addIndex n (m, ns) = add n (m, ns) | ||
112 | |||
113 | add n (m, ns) = case Map.lookup n m of | ||
114 | Just i -> (n ++ (toSubscript <$> show (i+1)), (Map.insert n (i+1) m, ns)) | ||
115 | Nothing -> (n, (Map.insert n 0 m, ns)) | ||
116 | |||
107 | renderDoc :: Doc -> P.Doc | 117 | renderDoc :: Doc -> P.Doc |
108 | renderDoc | 118 | renderDoc |
109 | = render | 119 | = render |
@@ -114,8 +124,8 @@ renderDoc | |||
114 | . showVars | 124 | . showVars |
115 | . expand True | 125 | . expand True |
116 | where | 126 | where |
117 | freshNames = flip (:) <$> iterate ('\'':) "" <*> ['a'..'z'] | 127 | freshNames = (mempty, cycle $ (:[]) <$> ['a'..'z']) |
118 | freeNames = map ('_':) freshNames | 128 | freeNames = map ('_':) $ flip (:) <$> iterate ('\'':) "" <*> ['a'..'z'] |
119 | 129 | ||
120 | noexpand = expand False | 130 | noexpand = expand False |
121 | expand full = \case | 131 | expand full = \case |
@@ -139,8 +149,8 @@ renderDoc | |||
139 | DInfix pr x op y -> DInfix pr <$> showVars x <*> showVarA op <*> showVars y | 149 | DInfix pr x op y -> DInfix pr <$> showVars x <*> showVarA op <*> showVars y |
140 | DPreOp pr op y -> DPreOp pr <$> showVarA op <*> showVars y | 150 | DPreOp pr op y -> DPreOp pr <$> showVarA op <*> showVars y |
141 | DVar i -> asks $ text . (!! i) | 151 | DVar i -> asks $ text . (!! i) |
142 | DFreshName True x -> gets head >>= \n -> modify tail >> local (n:) (showVars x) | 152 | DFreshName (Just n) x -> mkFreshName n >>= \n -> local (n:) (showVars x) |
143 | DFreshName False x -> local ("_":) $ showVars x | 153 | DFreshName Nothing x -> local ("_":) $ showVars x |
144 | DResetFreshNames x -> do | 154 | DResetFreshNames x -> do |
145 | st <- get | 155 | st <- get |
146 | put freshNames | 156 | put freshNames |
@@ -421,3 +431,14 @@ showNth n = show n ++ f (n `div` 10 `mod` 10) (n `mod` 10) | |||
421 | f _ 3 = "rd" | 431 | f _ 3 = "rd" |
422 | f _ _ = "th" | 432 | f _ _ = "th" |
423 | 433 | ||
434 | toSubscript '0' = '₀' | ||
435 | toSubscript '1' = '₁' | ||
436 | toSubscript '2' = '₂' | ||
437 | toSubscript '3' = '₃' | ||
438 | toSubscript '4' = '₄' | ||
439 | toSubscript '5' = '₅' | ||
440 | toSubscript '6' = '₆' | ||
441 | toSubscript '7' = '₇' | ||
442 | toSubscript '8' = '₈' | ||
443 | toSubscript '9' = '₉' | ||
444 | toSubscript _ = error "toSubscript" | ||