summaryrefslogtreecommitdiff
path: root/src/LambdaCube
diff options
context:
space:
mode:
Diffstat (limited to 'src/LambdaCube')
-rw-r--r--src/LambdaCube/Compiler/Core.hs6
-rw-r--r--src/LambdaCube/Compiler/DesugaredSource.hs19
-rw-r--r--src/LambdaCube/Compiler/Infer.hs6
-rw-r--r--src/LambdaCube/Compiler/Pretty.hs33
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
413instance MkDoc Exp where 413instance 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
301dummyName s = SIName (debugSI s) ("v_" ++ s) 301dummyName s = SIName (debugSI s) "" --("v_" ++ s)
302dummyName' = SData . dummyName 302dummyName' = SData . dummyName
303sVar = SVar . dummyName 303sVar = 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
487shApp Visible a b = DApp a b 487shApp Visible a b = DApp a b
488shApp Hidden a b = DApp a (DAt b) 488shApp Hidden a b = DApp a (DAt b)
489 489
490usedVar' a b s | usedVar a b = Just s
491 | otherwise = Nothing
492
490shLam usedVar h a b = shLam_ usedVar h (Just a) b 493shLam usedVar h a b = shLam_ usedVar h (Just a) b
491 494
492simpleFo (DExpand x _) = x 495simpleFo (DExpand x _) = x
493simpleFo x = x 496simpleFo x = x
494 497
495shLam_ False (BPi Hidden) (Just ((simpleFo -> DText "'CW") `DApp` a)) b = DFreshName False $ showContext (DUp 0 a) b 498shLam_ 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
537showLet x (DLet' xs y) = DLet' (DSemi x xs) y 540showLet x (DLet' xs y) = DLet' (DSemi x xs) y
538showLet x y = DLet' x y 541showLet x y = DLet' x y
539 542
540shLet_ a b = DFreshName True $ showLet (DLet "=" (shVar 0) $ DUp 0 a) b 543shLet_ 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)
83envDoc :: Env -> Doc -> Doc 83envDoc :: Env -> Doc -> Doc
84envDoc x m = case x of 84envDoc 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
102instance MkDoc a => MkDoc (CEnv a) where 102instance 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
18import Data.Char 18import Data.Char
19import Data.Monoid 19import Data.Monoid
20import qualified Data.Set as Set 20import qualified Data.Set as Set
21--import qualified Data.Map as Map 21import qualified Data.Map.Strict as Map
22import Control.Applicative 22import Control.Applicative
23import Control.Monad.Identity 23import Control.Monad.Identity
24import Control.Monad.Reader 24import 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 .
104simpleShow :: PShow a => a -> String 104simpleShow :: PShow a => a -> String
105simpleShow = ($ "") . P.displayS . P.renderPretty 0.4 200 . P.plain . renderDoc . pShow 105simpleShow = ($ "") . P.displayS . P.renderPretty 0.4 200 . P.plain . renderDoc . pShow
106 106
107mkFreshName :: MonadState (Map.Map String Int, [String]) m => String -> m String
108mkFreshName 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
107renderDoc :: Doc -> P.Doc 117renderDoc :: Doc -> P.Doc
108renderDoc 118renderDoc
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
434toSubscript '0' = '₀'
435toSubscript '1' = '₁'
436toSubscript '2' = '₂'
437toSubscript '3' = '₃'
438toSubscript '4' = '₄'
439toSubscript '5' = '₅'
440toSubscript '6' = '₆'
441toSubscript '7' = '₇'
442toSubscript '8' = '₈'
443toSubscript '9' = '₉'
444toSubscript _ = error "toSubscript"