summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/LambdaCube/Compiler/DesugaredSource.hs219
-rw-r--r--src/LambdaCube/Compiler/Parser.hs4
-rw-r--r--src/LambdaCube/Compiler/Pretty.hs151
3 files changed, 187 insertions, 187 deletions
diff --git a/src/LambdaCube/Compiler/DesugaredSource.hs b/src/LambdaCube/Compiler/DesugaredSource.hs
index 60807cc6..0511988a 100644
--- a/src/LambdaCube/Compiler/DesugaredSource.hs
+++ b/src/LambdaCube/Compiler/DesugaredSource.hs
@@ -8,7 +8,10 @@
8{-# LANGUAGE DeriveFunctor #-} 8{-# LANGUAGE DeriveFunctor #-}
9{-# LANGUAGE ScopedTypeVariables #-} 9{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE MultiParamTypeClasses #-} 10{-# LANGUAGE MultiParamTypeClasses #-}
11module LambdaCube.Compiler.DesugaredSource where 11module LambdaCube.Compiler.DesugaredSource
12 ( module LambdaCube.Compiler.DesugaredSource
13 , FixityDir(..), Fixity(..)
14 )where
12 15
13import Data.Monoid 16import Data.Monoid
14import Data.Maybe 17import Data.Maybe
@@ -19,8 +22,6 @@ import Data.Bits
19import qualified Data.Map as Map 22import qualified Data.Map as Map
20import qualified Data.Set as Set 23import qualified Data.Set as Set
21import qualified Data.IntMap as IM 24import qualified Data.IntMap as IM
22import Control.Monad.Reader
23import Control.Monad.State
24import Control.Arrow hiding ((<+>)) 25import Control.Arrow hiding ((<+>))
25import Control.DeepSeq 26import Control.DeepSeq
26 27
@@ -53,14 +54,6 @@ pattern MatchName :: SName -> SName
53pattern MatchName cs <- 'm':'a':'t':'c':'h':cs where MatchName cs = "match" ++ cs 54pattern MatchName cs <- 'm':'a':'t':'c':'h':cs where MatchName cs = "match" ++ cs
54 55
55 56
56-------------------------------------------------------------------------------- fixity
57
58data FixityDir = Infix | InfixL | InfixR
59 deriving (Eq, Show)
60
61data Fixity = Fixity !FixityDir !Int
62 deriving (Eq, Show)
63
64-------------------------------------------------------------------------------- file position 57-------------------------------------------------------------------------------- file position
65 58
66-- source position without file name 59-- source position without file name
@@ -392,6 +385,39 @@ trSExp f = g where
392trSExp' :: SExp -> SExp' a 385trSExp' :: SExp -> SExp' a
393trSExp' = trSExp elimVoid 386trSExp' = trSExp elimVoid
394 387
388instance Up a => PShow (SExp' a) where
389 pShowPrec _ = showDoc_ . sExpDoc
390
391sExpDoc :: Up a => SExp' a -> NDoc
392sExpDoc = \case
393 SGlobal ns -> shAtom $ sName ns
394 SAnn a b -> shAnn ":" False (sExpDoc a) (sExpDoc b)
395 TyType a -> shApp Visible (shAtom "tyType") (sExpDoc a)
396 SApp h a b -> shApp h (sExpDoc a) (sExpDoc b)
397 Wildcard t -> shAnn ":" True (shAtom "_") (sExpDoc t)
398 SBind_ _ h _ a b -> shLam (usedVar 0 b) h (sExpDoc a) (sExpDoc b)
399 SLet _ a b -> shLet_ (sExpDoc a) (sExpDoc b)
400 STyped _{-(e,t)-} -> shAtom "<<>>" -- todo: expDoc e
401 SVar _ i -> shVar i
402 SLit _ l -> shAtom $ show l
403
404shLam usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 a) b
405 where
406 lam = case h of
407 BPi{} -> shArr
408 _ -> shLam'
409
410 p = case h of
411 BMeta -> cpar . shAnn ":" True (inBlue' $ DVar 0)
412 BLam h -> vpar h
413 BPi h -> vpar h
414
415 vpar Hidden = (\p -> DBrace p) . shAnn ":" True (inGreen' $ DVar 0)
416 vpar Visible = ann (inGreen' $ DVar 0)
417
418 ann | usedVar = shAnn ":" False
419 | otherwise = const id
420
395-------------------------------------------------------------------------------- statement 421-------------------------------------------------------------------------------- statement
396 422
397data Stmt 423data Stmt
@@ -474,174 +500,3 @@ data Extension
474extensionMap :: Map.Map String Extension 500extensionMap :: Map.Map String Extension
475extensionMap = Map.fromList $ map (show &&& id) [toEnum 0 .. ] 501extensionMap = Map.fromList $ map (show &&& id) [toEnum 0 .. ]
476 502
477-------------------------------------------------------------------------------- pretty print
478
479data NDoc
480 = DAtom String
481 | DOp Fixity NDoc String NDoc
482 | DPar String NDoc String
483 | DLam String [NDoc] String NDoc
484 | DVar Int
485 | DFreshName Bool{-False: dummy-} NDoc
486 | DUp Int NDoc
487 | DColor Color NDoc
488 -- add wl-pprint combinators as necessary here
489 deriving (Eq)
490
491pattern DParen x = DPar "(" x ")"
492pattern DBrace x = DPar "{" x "}"
493pattern DArr x y = DOp (Fixity InfixR (-1)) x "->" y
494pattern DAnn x y = DOp (Fixity InfixR (-3)) x ":" y
495
496data Color = Green | Blue | Underlined
497 deriving (Eq)
498
499inGreen' = DColor Green
500inBlue' = DColor Blue
501epar = DColor Underlined
502
503strip = \case
504 DColor _ x -> strip x
505 DUp _ x -> strip x
506 DFreshName _ x -> strip x
507 x -> x
508
509simple x = case strip x of
510 DAtom{} -> True
511 DVar{} -> True
512 DPar{} -> True
513 _ -> False
514
515renderDocX :: NDoc -> Doc
516renderDocX = render . addPar (-10) . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) . showVars
517 where
518 showVars x = case x of
519 DAtom s -> pure x
520 DColor c x -> DColor c <$> showVars x
521 DPar l x r -> DPar l <$> showVars x <*> pure r
522 DOp pr x s y -> DOp pr <$> showVars x <*> pure s <*> showVars y
523 DVar i -> asks $ DAtom . lookupVarName i
524 DFreshName True x -> gets head >>= \n -> modify tail >> local (n:) (showVars x)
525 DFreshName False x -> local ("_":) $ showVars x
526 DUp i x -> local (dropNth i) $ showVars x
527 DLam lam vs arr e -> DLam lam <$> (mapM showVars vs) <*> pure arr <*> showVars e
528 where
529 lookupVarName i xs | i < length xs = xs !! i
530 lookupVarName i _ = ((\s n -> n: '_': s) <$> iterate ('\'':) "" <*> ['a'..'z']) !! i
531
532 addPar :: Int -> NDoc -> NDoc
533 addPar pr x = case x of
534 DAtom{} -> x
535 DColor c x -> DColor c $ addPar pr x
536 DPar l x r -> DPar l (addPar (-20) x) r
537 DOp pr' x s y -> paren $ DOp pr' (addPar (precL pr') x) s (addPar (precR pr') y)
538 DLam lam vs arr e -> paren $ DLam lam (addPar 10 <$> vs) arr (addPar (-10) e)
539 where
540 paren d
541 | protect x = DParen d
542 | otherwise = d
543 where
544 protect x = case x of
545 DAtom{} -> False
546 DPar{} -> False
547 DOp (Fixity _ pr') _ _ _ -> pr' < pr
548 DLam{} -> -10 < pr
549
550 precL (Fixity Infix i) = i+1
551 precL (Fixity InfixL i) = i
552 precL (Fixity InfixR i) = i+1
553 precR (Fixity Infix i) = i+1
554 precR (Fixity InfixL i) = i+1
555 precR (Fixity InfixR i) = i
556
557 render x = case x of
558 DColor Green x -> text $ inGreen $ show $ render x -- TODO
559 DColor Blue x -> text $ inBlue $ show $ render x -- TODO
560 DColor Underlined x -> text $ underlined $ show $ render x -- TODO
561 DAtom s -> text s
562 DPar l x r -> text l <> render x <> text r
563 DOp _ x s y -> case s of
564 "" -> render x <+> render y
565 _ | simple x && simple y && s /= "," -> render x <> text s <> render y
566 | otherwise -> (render x <++> s) <+> render y
567 DLam lam vs arr e -> text lam <> hsep (render <$> vs) <+> text arr <+> render e
568 where
569 x <++> "," = x <> text ","
570 x <++> s = x <+> text s
571
572instance Up a => PShow (SExp' a) where
573 pShowPrec _ = showDoc_ . sExpDoc
574
575-- name De Bruijn indices
576type NameDB a = StateT [String] (Reader [String]) a
577
578showDoc :: NDoc -> String
579showDoc = show . renderDocX
580
581showDoc_ :: NDoc -> Doc
582showDoc_ = renderDocX
583
584sExpDoc :: Up a => SExp' a -> NDoc
585sExpDoc = \case
586 SGlobal ns -> shAtom $ sName ns
587 SAnn a b -> shAnn ":" False (sExpDoc a) (sExpDoc b)
588 TyType a -> shApp Visible (shAtom "tyType") (sExpDoc a)
589 SApp h a b -> shApp h (sExpDoc a) (sExpDoc b)
590 Wildcard t -> shAnn ":" True (shAtom "_") (sExpDoc t)
591 SBind_ _ h _ a b -> shLam (usedVar 0 b) h (sExpDoc a) (sExpDoc b)
592 SLet _ a b -> shLet_ (sExpDoc a) (sExpDoc b)
593 STyped _{-(e,t)-} -> shAtom "<<>>" -- todo: expDoc e
594 SVar _ i -> shVar i
595 SLit _ l -> shAtom $ show l
596
597shVar = DVar
598
599shLet i a b = shLam' (cpar . shLet' (inBlue' $ shVar i) $ DUp i a) (DUp i b)
600shLet_ a b = DFreshName True $ shLam' (cpar . shLet' (shVar 0) $ DUp 0 a) b
601
602shLam usedVar h a b = DFreshName usedVar $ lam (p $ DUp 0 a) b
603 where
604 lam = case h of
605 BPi{} -> shArr
606 _ -> shLam'
607
608 p = case h of
609 BMeta -> cpar . shAnn ":" True (inBlue' $ DVar 0)
610 BLam h -> vpar h
611 BPi h -> vpar h
612
613 vpar Hidden = (\p -> DBrace p) . shAnn ":" True (inGreen' $ DVar 0)
614 vpar Visible = ann (inGreen' $ DVar 0)
615
616 ann | usedVar = shAnn ":" False
617 | otherwise = const id
618
619-----------------------------------------
620
621shAtom = DAtom
622
623shTuple [] = DAtom "()"
624shTuple [x] = DParen $ DParen x
625shTuple xs = DParen $ foldr1 (\x y -> DOp (Fixity InfixR (-20)) x "," y) xs
626
627shAnn _ True x y | strip y == DAtom "Type" = x
628shAnn s _ x y = DOp (Fixity InfixR (-3)) x s y
629
630shApp _ x y = DOp (Fixity InfixL 10) x "" y
631
632shArr = DArr
633
634shCstr x y = DOp (Fixity Infix (-2)) x "~" y
635
636shLet' x y = DOp (Fixity Infix (-4)) x ":=" y
637
638getFN (DFreshName True a) = first (+1) $ getFN a
639getFN a = (0, a)
640
641shLam' x (getFN -> (i, DLam "\\" xs "->" y)) = iterateN i (DFreshName True) $ DLam "\\" (iterateN i (DUp 0) x: xs) "->" y
642shLam' x y = DLam "\\" [x] "->" y
643
644cpar s | simple s = s
645cpar s = DParen s
646
647
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs
index 8a9e56c1..fd2cdbfd 100644
--- a/src/LambdaCube/Compiler/Parser.hs
+++ b/src/LambdaCube/Compiler/Parser.hs
@@ -146,18 +146,14 @@ mkDesugarInfo ss = DesugarInfo
146 146
147data Prec 147data Prec
148 = PrecAtom -- ( _ ) ... -- 20 148 = PrecAtom -- ( _ ) ... -- 20
149 | PrecAtom'
150 | PrecAt -- _@_ {assoc} -- in patterns -- 13 149 | PrecAt -- _@_ {assoc} -- in patterns -- 13
151 | PrecProj -- _ ._ {left} -- 12 150 | PrecProj -- _ ._ {left} -- 12
152 | PrecSwiz -- _%_ {left} -- 11 151 | PrecSwiz -- _%_ {left} -- 11
153 | PrecApp -- _ _ {left} -- 10 152 | PrecApp -- _ _ {left} -- 10
154 | PrecOp -- 0 - 9 153 | PrecOp -- 0 - 9
155 | PrecArr -- _ -> _ {right} -- -1 154 | PrecArr -- _ -> _ {right} -- -1
156 | PrecEq -- _ ~ _ -- -2
157 | PrecAnn -- _ :: _ {right} -- -3 155 | PrecAnn -- _ :: _ {right} -- -3
158 | PrecLet -- _ := _ -- -4
159 | PrecLam -- \ _ -> _ {right} {accum} -- -10 156 | PrecLam -- \ _ -> _ {right} {accum} -- -10
160 -- _ , _ {right} -- -20
161 deriving (Eq, Ord) 157 deriving (Eq, Ord)
162 158
163-------------------------------------------------------------------------------- expression parsing 159-------------------------------------------------------------------------------- expression parsing
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs
index e1d41fc1..5044eecf 100644
--- a/src/LambdaCube/Compiler/Pretty.hs
+++ b/src/LambdaCube/Compiler/Pretty.hs
@@ -21,9 +21,14 @@ import qualified Data.Set as Set
21import Data.Map (Map) 21import Data.Map (Map)
22import qualified Data.Map as Map 22import qualified Data.Map as Map
23import Control.Monad.Except 23import Control.Monad.Except
24import Control.Monad.Reader
25import Control.Monad.State
26import Control.Arrow hiding ((<+>))
24import Debug.Trace 27import Debug.Trace
25 28
26import Text.PrettyPrint.Leijen 29import Text.PrettyPrint.Leijen hiding ((<$>))
30
31import LambdaCube.Compiler.Utils
27 32
28-------------------------------------------------------------------------------- 33--------------------------------------------------------------------------------
29 34
@@ -150,3 +155,147 @@ error_ = error . correctEscs
150trace_ = trace . correctEscs 155trace_ = trace . correctEscs
151throwError_ = throwError . correctEscs 156throwError_ = throwError . correctEscs
152 157
158
159-------------------------------------------------------------------------------- fixity
160
161data FixityDir = Infix | InfixL | InfixR
162 deriving (Eq, Show)
163
164data Fixity = Fixity !FixityDir !Int
165 deriving (Eq, Show)
166
167-------------------------------------------------------------------------------- pretty print
168
169data NDoc
170 = DAtom String
171 | DOp Fixity NDoc String NDoc
172 | DPar String NDoc String
173 | DLam String [NDoc] String NDoc
174 | DVar Int
175 | DFreshName Bool{-False: dummy-} NDoc
176 | DUp Int NDoc
177 | DColor Color NDoc
178 -- add wl-pprint combinators as necessary here
179 deriving (Eq)
180
181pattern DParen x = DPar "(" x ")"
182pattern DBrace x = DPar "{" x "}"
183pattern DArr x y = DOp (Fixity InfixR (-1)) x "->" y
184pattern DAnn x y = DOp (Fixity InfixR (-3)) x ":" y
185
186data Color = Green | Blue | Underlined
187 deriving (Eq)
188
189inGreen' = DColor Green
190inBlue' = DColor Blue
191epar = DColor Underlined
192
193strip = \case
194 DColor _ x -> strip x
195 DUp _ x -> strip x
196 DFreshName _ x -> strip x
197 x -> x
198
199simple x = case strip x of
200 DAtom{} -> True
201 DVar{} -> True
202 DPar{} -> True
203 _ -> False
204
205renderDocX :: NDoc -> Doc
206renderDocX = render . addPar (-10) . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) . showVars
207 where
208 showVars x = case x of
209 DAtom s -> pure x
210 DColor c x -> DColor c <$> showVars x
211 DPar l x r -> DPar l <$> showVars x <*> pure r
212 DOp pr x s y -> DOp pr <$> showVars x <*> pure s <*> showVars y
213 DVar i -> asks $ DAtom . lookupVarName i
214 DFreshName True x -> gets head >>= \n -> modify tail >> local (n:) (showVars x)
215 DFreshName False x -> local ("_":) $ showVars x
216 DUp i x -> local (dropNth i) $ showVars x
217 DLam lam vs arr e -> DLam lam <$> (mapM showVars vs) <*> pure arr <*> showVars e
218 where
219 lookupVarName i xs | i < length xs = xs !! i
220 lookupVarName i _ = ((\s n -> n: '_': s) <$> iterate ('\'':) "" <*> ['a'..'z']) !! i
221
222 addPar :: Int -> NDoc -> NDoc
223 addPar pr x = case x of
224 DAtom{} -> x
225 DColor c x -> DColor c $ addPar pr x
226 DPar l x r -> DPar l (addPar (-20) x) r
227 DOp pr' x s y -> paren $ DOp pr' (addPar (precL pr') x) s (addPar (precR pr') y)
228 DLam lam vs arr e -> paren $ DLam lam (addPar 10 <$> vs) arr (addPar (-10) e)
229 where
230 paren d
231 | protect x = DParen d
232 | otherwise = d
233 where
234 protect x = case x of
235 DAtom{} -> False
236 DPar{} -> False
237 DOp (Fixity _ pr') _ _ _ -> pr' < pr
238 DLam{} -> -10 < pr
239
240 precL (Fixity Infix i) = i+1
241 precL (Fixity InfixL i) = i
242 precL (Fixity InfixR i) = i+1
243 precR (Fixity Infix i) = i+1
244 precR (Fixity InfixL i) = i+1
245 precR (Fixity InfixR i) = i
246
247 render x = case x of
248 DColor Green x -> text $ inGreen $ show $ render x -- TODO
249 DColor Blue x -> text $ inBlue $ show $ render x -- TODO
250 DColor Underlined x -> text $ underlined $ show $ render x -- TODO
251 DAtom s -> text s
252 DPar l x r -> text l <> render x <> text r
253 DOp _ x s y -> case s of
254 "" -> render x <+> render y
255 _ | simple x && simple y && s /= "," -> render x <> text s <> render y
256 | otherwise -> (render x <++> s) <+> render y
257 DLam lam vs arr e -> text lam <> hsep (render <$> vs) <+> text arr <+> render e
258 where
259 x <++> "," = x <> text ","
260 x <++> s = x <+> text s
261
262showDoc :: NDoc -> String
263showDoc = show . renderDocX
264
265showDoc_ :: NDoc -> Doc
266showDoc_ = renderDocX
267
268shVar = DVar
269
270shLet i a b = shLam' (cpar . shLet' (inBlue' $ shVar i) $ DUp i a) (DUp i b)
271shLet_ a b = DFreshName True $ shLam' (cpar . shLet' (shVar 0) $ DUp 0 a) b
272
273-----------------------------------------
274
275shAtom = DAtom
276
277shTuple [] = DAtom "()"
278shTuple [x] = DParen $ DParen x
279shTuple xs = DParen $ foldr1 (\x y -> DOp (Fixity InfixR (-20)) x "," y) xs
280
281shAnn _ True x y | strip y == DAtom "Type" = x
282shAnn s _ x y = DOp (Fixity InfixR (-3)) x s y
283
284shApp _ x y = DOp (Fixity InfixL 10) x "" y
285
286shArr = DArr
287
288shCstr x y = DOp (Fixity Infix (-2)) x "~" y
289
290shLet' x y = DOp (Fixity Infix (-4)) x ":=" y
291
292getFN (DFreshName True a) = first (+1) $ getFN a
293getFN a = (0, a)
294
295shLam' x (getFN -> (i, DLam "\\" xs "->" y)) = iterateN i (DFreshName True) $ DLam "\\" (iterateN i (DUp 0) x: xs) "->" y
296shLam' x y = DLam "\\" [x] "->" y
297
298cpar s | simple s = s
299cpar s = DParen s
300
301