summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-05-01 07:30:17 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-05-01 07:30:17 +0200
commit991725b2c054359388feab08373ebfd0683b5e46 (patch)
tree5672a9d7931e5d420b75c0449033af1c847733d1 /src
parent1fd867e5beea5b4197f300e2e964c0f6b0035830 (diff)
refactoring
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/Compiler.hs4
-rw-r--r--src/LambdaCube/Compiler/DesugaredSource.hs23
-rw-r--r--src/LambdaCube/Compiler/Infer.hs4
-rw-r--r--src/LambdaCube/Compiler/Lexer.hs2
-rw-r--r--src/LambdaCube/Compiler/Pretty.hs44
-rw-r--r--src/LambdaCube/Compiler/Utils.hs2
6 files changed, 49 insertions, 30 deletions
diff --git a/src/LambdaCube/Compiler.hs b/src/LambdaCube/Compiler.hs
index ecb33167..bc1f615b 100644
--- a/src/LambdaCube/Compiler.hs
+++ b/src/LambdaCube/Compiler.hs
@@ -188,8 +188,8 @@ loadModule ex imp mname_ = do
188 filterImports (ImportJust ns) = (`elem` map sName ns) 188 filterImports (ImportJust ns) = (`elem` map sName ns)
189 189
190-- used in runTests 190-- used in runTests
191getDef :: MonadMask m => FilePath -> SName -> Maybe Exp -> MMT m Infos (Infos, Either Doc (FilePath, Either Doc (Exp, Exp))) 191getDef :: MonadMask m => FilePath -> SName -> Maybe Exp -> MMT m (Infos, [Stmt]) ((Infos, [Stmt]), Either Doc (FilePath, Either Doc (Exp, Exp)))
192getDef = getDef_ fst 192getDef = getDef_ id
193 193
194getDef_ ex m d ty = loadModule ex Nothing (Left m) <&> \case 194getDef_ ex m d ty = loadModule ex Nothing (Left m) <&> \case
195 Left err -> (mempty, Left err) 195 Left err -> (mempty, Left err)
diff --git a/src/LambdaCube/Compiler/DesugaredSource.hs b/src/LambdaCube/Compiler/DesugaredSource.hs
index 6c4ff0e0..8f92f18a 100644
--- a/src/LambdaCube/Compiler/DesugaredSource.hs
+++ b/src/LambdaCube/Compiler/DesugaredSource.hs
@@ -79,7 +79,6 @@ instance Eq FileInfo where (==) = (==) `on` fileId
79instance Ord FileInfo where compare = compare `on` fileId 79instance Ord FileInfo where compare = compare `on` fileId
80 80
81instance PShow FileInfo where pShow = text . filePath 81instance PShow FileInfo where pShow = text . filePath
82instance Show FileInfo where show = ppShow
83 82
84showPos :: FileInfo -> SPos -> Doc 83showPos :: FileInfo -> SPos -> Doc
85showPos n p = pShow n <> ":" <> pShow p 84showPos n p = pShow n <> ":" <> pShow p
@@ -117,7 +116,7 @@ instance NFData SI where
117 NoSI x -> rnf x 116 NoSI x -> rnf x
118 RangeSI r -> rnf r 117 RangeSI r -> rnf r
119 118
120instance Show SI where show _ = "SI" 119--instance Show SI where show _ = "SI"
121instance Eq SI where _ == _ = True 120instance Eq SI where _ == _ = True
122instance Ord SI where _ `compare` _ = EQ 121instance Ord SI where _ `compare` _ = EQ
123 122
@@ -149,7 +148,7 @@ pattern SIName si n <- SIName_ si _ n
149 148
150instance Eq SIName where (==) = (==) `on` sName 149instance Eq SIName where (==) = (==) `on` sName
151instance Ord SIName where compare = compare `on` sName 150instance Ord SIName where compare = compare `on` sName
152instance Show SIName where show = sName 151--instance Show SIName where show = sName
153instance PShow SIName 152instance PShow SIName
154 where 153 where
155 pShow (SIName si n) = expand (text n) $ case si of 154 pShow (SIName si n) = expand (text n) $ case si of
@@ -210,10 +209,10 @@ data Binder
210 = BPi Visibility 209 = BPi Visibility
211 | BLam Visibility 210 | BLam Visibility
212 | BMeta -- a metavariable is like a floating hidden lambda 211 | BMeta -- a metavariable is like a floating hidden lambda
213 deriving (Eq, Show) 212 deriving (Eq)
214 213
215data Visibility = Hidden | Visible 214data Visibility = Hidden | Visible
216 deriving (Eq, Show) 215 deriving (Eq)
217 216
218dummyName s = SIName (debugSI s) ("v_" ++ s) 217dummyName s = SIName (debugSI s) ("v_" ++ s)
219dummyName' = SData . dummyName 218dummyName' = SData . dummyName
@@ -380,10 +379,11 @@ trSExp' = trSExp elimVoid
380 379
381instance (Up a, PShow a) => PShow (SExp' a) where 380instance (Up a, PShow a) => PShow (SExp' a) where
382 pShow = \case 381 pShow = \case
382 SGlobal op | Just p <- getFixity op -> DOp0 (sName op) p
383 SGlobal ns -> pShow ns 383 SGlobal ns -> pShow ns
384 SAnn a b -> shAnn False (pShow a) (pShow b) 384 SAnn a b -> shAnn False (pShow a) (pShow b)
385 TyType a -> text "tyType" `DApp` pShow a 385 TyType a -> text "tyType" `dApp` pShow a
386 SGlobal op `SAppV` a `SAppV` b | Just p <- getFixity op -> DOp (sName op) p (pShow a) (pShow b) 386 SAppV a b -> pShow a `dApp` pShow b
387 SApp h a b -> shApp h (pShow a) (pShow b) 387 SApp h a b -> shApp h (pShow a) (pShow b)
388 Wildcard t -> shAnn True (text "_") (pShow t) 388 Wildcard t -> shAnn True (text "_") (pShow t)
389 SBind_ _ h _ a b -> shLam (usedVar 0 b) h (pShow a) (pShow b) 389 SBind_ _ h _ a b -> shLam (usedVar 0 b) h (pShow a) (pShow b)
@@ -427,8 +427,8 @@ showLam x (DFreshName True d) = DFreshName True $ showLam (DUp 0 x) d
427showLam x (DLam xs y) = DLam (DSep (InfixR 11) x xs) y 427showLam x (DLam xs y) = DLam (DSep (InfixR 11) x xs) y
428showLam x y = DLam x y 428showLam x y = DLam x y
429 429
430shLet i a b = showLam (DLet (blue $ shVar i) $ DUp i a) (DUp i b) 430shLet i a b = showLam (DLet ":=" (blue $ shVar i) $ DUp i a) (DUp i b)
431shLet_ a b = DFreshName True $ showLam (DLet (shVar 0) $ DUp 0 a) b 431shLet_ a b = DFreshName True $ showLam (DLet ":=" (shVar 0) $ DUp 0 a) b
432 432
433-------------------------------------------------------------------------------- statement 433-------------------------------------------------------------------------------- statement
434 434
@@ -441,9 +441,10 @@ pattern Primitive n t = Let n (Just t) (SBuiltin "undefined")
441 441
442instance PShow Stmt where 442instance PShow Stmt where
443 pShow = \case 443 pShow = \case
444 Let n ty e -> text (sName n) </> "=" <+> maybe (pShow e) (\ty -> pShow e </> "::" <+> pShow ty) ty 444 Primitive n t -> shAnn False (pShow n) (pShow t)
445 Let n ty e -> DLet "=" (pShow n) $ maybe (pShow e) (\ty -> shAnn False (pShow e) (pShow ty)) ty
445 Data n ps ty cs -> "data" <+> text (sName n) 446 Data n ps ty cs -> "data" <+> text (sName n)
446 PrecDef n i -> "precedence" <+> text (sName n) <+> text (show i) 447 PrecDef n i -> pShow i <+> DOp0 (sName n) i
447 448
448instance DeBruijnify SIName Stmt where 449instance DeBruijnify SIName Stmt where
449 deBruijnify_ k v = \case 450 deBruijnify_ k v = \case
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs
index ad7eeae6..88860d62 100644
--- a/src/LambdaCube/Compiler/Infer.hs
+++ b/src/LambdaCube/Compiler/Infer.hs
@@ -63,7 +63,7 @@ data Exp
63 | Neut Neutral 63 | Neut Neutral
64 64
65data Freq = CompileTime | RunTime 65data Freq = CompileTime | RunTime
66 deriving (Eq, Show) 66 deriving (Eq)
67 67
68pattern TType = TType_ CompileTime 68pattern TType = TType_ CompileTime
69 69
@@ -756,7 +756,7 @@ instance Monoid MaxDB where
756 max 0 x = x 756 max 0 x = x
757 max _ _ = 1 -- 757 max _ _ = 1 --
758 758
759instance Show MaxDB where show _ = "MaxDB" 759--instance Show MaxDB where show _ = "MaxDB"
760 760
761varDB i = MaxDB 1 -- 761varDB i = MaxDB 1 --
762 762
diff --git a/src/LambdaCube/Compiler/Lexer.hs b/src/LambdaCube/Compiler/Lexer.hs
index 46c44086..e10ac817 100644
--- a/src/LambdaCube/Compiler/Lexer.hs
+++ b/src/LambdaCube/Compiler/Lexer.hs
@@ -181,7 +181,7 @@ commaSep1 p = sepBy1 p $ symbol ","
181-------------------------------------------------------------------------------- namespace handling 181-------------------------------------------------------------------------------- namespace handling
182 182
183data Namespace = TypeNS | ExpNS 183data Namespace = TypeNS | ExpNS
184 deriving (Eq, Show) 184 deriving (Eq)
185 185
186tick c = f <$> asks namespace 186tick c = f <$> asks namespace
187 where 187 where
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs
index 2059e530..1d0e07dd 100644
--- a/src/LambdaCube/Compiler/Pretty.hs
+++ b/src/LambdaCube/Compiler/Pretty.hs
@@ -35,7 +35,7 @@ data Fixity
35 = Infix !Int 35 = Infix !Int
36 | InfixL !Int 36 | InfixL !Int
37 | InfixR !Int 37 | InfixR !Int
38 deriving (Eq, Show) 38 deriving (Eq)
39 39
40instance PShow Fixity where 40instance PShow Fixity where
41 pShow = \case 41 pShow = \case
@@ -64,6 +64,7 @@ data Doc
64 64
65 | DAtom DocAtom 65 | DAtom DocAtom
66 | DInfix Fixity Doc DocAtom Doc 66 | DInfix Fixity Doc DocAtom Doc
67 | DPreOp Int DocAtom Doc
67 68
68 | DFreshName Bool{-used-} Doc 69 | DFreshName Bool{-used-} Doc
69 | DVar Int 70 | DVar Int
@@ -120,6 +121,7 @@ renderDoc
120 DDocOp x d -> DDocOp x $ expand full <$> d 121 DDocOp x d -> DDocOp x $ expand full <$> d
121 DAtom s -> DAtom $ mapDocAtom (\_ _ -> noexpand) s 122 DAtom s -> DAtom $ mapDocAtom (\_ _ -> noexpand) s
122 DInfix pr x op y -> DInfix pr (noexpand x) (mapDocAtom (\_ _ -> noexpand) op) (noexpand y) 123 DInfix pr x op y -> DInfix pr (noexpand x) (mapDocAtom (\_ _ -> noexpand) op) (noexpand y)
124 DPreOp pr op y -> DPreOp pr (mapDocAtom (\_ _ -> noexpand) op) (noexpand y)
123 DVar i -> DVar i 125 DVar i -> DVar i
124 DFreshName b x -> DFreshName b $ noexpand x 126 DFreshName b x -> DFreshName b $ noexpand x
125 DUp i x -> DUp i $ noexpand x 127 DUp i x -> DUp i $ noexpand x
@@ -129,6 +131,7 @@ renderDoc
129 DFormat c x -> DFormat c <$> showVars x 131 DFormat c x -> DFormat c <$> showVars x
130 DDocOp x d -> DDocOp x <$> traverse showVars d 132 DDocOp x d -> DDocOp x <$> traverse showVars d
131 DInfix pr x op y -> DInfix pr <$> showVars x <*> showVarA op <*> showVars y 133 DInfix pr x op y -> DInfix pr <$> showVars x <*> showVarA op <*> showVars y
134 DPreOp pr op y -> DPreOp pr <$> showVarA op <*> showVars y
132 DVar i -> asks $ text . (!! i) 135 DVar i -> asks $ text . (!! i)
133 DFreshName True x -> gets head >>= \n -> modify tail >> local (n:) (showVars x) 136 DFreshName True x -> gets head >>= \n -> modify tail >> local (n:) (showVars x)
134 DFreshName False x -> local ("_":) $ showVars x 137 DFreshName False x -> local ("_":) $ showVars x
@@ -140,8 +143,13 @@ renderDoc
140 addPar :: Int -> Doc -> Doc 143 addPar :: Int -> Doc -> Doc
141 addPar pr x = case x of 144 addPar pr x = case x of
142 DAtom x -> DAtom $ addParA x 145 DAtom x -> DAtom $ addParA x
146 DOp0 s f -> DParen $ DOp0 s f
147 DOpL s f x -> DParen $ DOpL s f $ addPar (leftPrecedence f) x
148 DOpR s f x -> DParen $ DOpR s f $ addPar (rightPrecedence f) x
143 DInfix pr' x op y -> (if protect then DParen else id) 149 DInfix pr' x op y -> (if protect then DParen else id)
144 $ DInfix pr' (addPar (leftPrecedence pr') x) (addParA op) (addPar (rightPrecedence pr') y) 150 $ DInfix pr' (addPar (leftPrecedence pr') x) (addParA op) (addPar (rightPrecedence pr') y)
151 DPreOp pr' op y -> (if protect then DParen else id)
152 $ DPreOp pr' (addParA op) (addPar pr' y)
145 DFormat c x -> DFormat c $ addPar pr x 153 DFormat c x -> DFormat c $ addPar pr x
146 DDocOp x d -> DDocOp x $ addPar (-10) <$> d 154 DDocOp x d -> DDocOp x $ addPar (-10) <$> d
147 where 155 where
@@ -149,6 +157,7 @@ renderDoc
149 157
150 protect = case x of 158 protect = case x of
151 DInfix f _ _ _ -> precedence f < pr 159 DInfix f _ _ _ -> precedence f < pr
160 DPreOp f _ _ -> f < pr
152 _ -> False 161 _ -> False
153 162
154 render :: Doc -> P.Doc 163 render :: Doc -> P.Doc
@@ -159,6 +168,7 @@ renderDoc
159 DDocOp f d -> (('\0', '\0'), f $ render <$> d) 168 DDocOp f d -> (('\0', '\0'), f $ render <$> d)
160 DAtom x -> renderA x 169 DAtom x -> renderA x
161 DInfix _ x op y -> render' x <++> renderA op <++> render' y 170 DInfix _ x op y -> render' x <++> renderA op <++> render' y
171 DPreOp _ op y -> renderA op <++> render' y
162 172
163 renderA (SimpleAtom s) = rtext s 173 renderA (SimpleAtom s) = rtext s
164 renderA (ComplexAtom s _ d a) = rtext s <++> render' d <++> renderA a 174 renderA (ComplexAtom s _ d a) = rtext s <++> render' d <++> renderA a
@@ -220,40 +230,50 @@ shVar = DVar
220shortForm d = DPar "" d "" 230shortForm d = DPar "" d ""
221expand = DExpand 231expand = DExpand
222 232
223pattern DPar l d r = DAtom (ComplexAtom l (-20) d (SimpleAtom r)) 233pattern DAt x = DGlue (InfixR 20) (DText "@") x
234pattern DApp x y = DSep (InfixL 10) x y
235pattern DArr_ s x y = DOp s (InfixR (-1)) x y -- -> => .
236pattern DCstr x y = DOp "~" (Infix (-2)) x y
237pattern DAnn x y = DOp "::" (Infix (-3)) x y
238pattern DLet s x y = DOp s (Infix (-4)) x y -- := =
239pattern DComma a b = DOp "," (InfixR (-20)) a b
240pattern DPar l d r = DAtom (ComplexAtom l (-20) d (SimpleAtom r))
241
224pattern DParen x = DPar "(" x ")" 242pattern DParen x = DPar "(" x ")"
225pattern DBrace x = DPar "{" x "}" 243pattern DBrace x = DPar "{" x "}"
226pattern DOp s f l r = DInfix f l (SimpleAtom s) r 244pattern DOp s f l r = DInfix f l (SimpleAtom s) r
245pattern DOp0 s f = DOp s f (DText "") (DText "")
246pattern DOpL s f x = DOp s f x (DText "")
247pattern DOpR s f x = DOp s f (DText "") x
227pattern DSep p a b = DOp " " p a b 248pattern DSep p a b = DOp " " p a b
228pattern DGlue p a b = DOp "" p a b 249pattern DGlue p a b = DOp "" p a b
229pattern DAt x = DGlue (InfixR 20) (DText "@") x
230 250
231pattern DArr_ s x y = DOp s (InfixR (-1)) x y
232pattern DArr x y = DArr_ "->" x y 251pattern DArr x y = DArr_ "->" x y
233pattern DAnn x y = DOp "::" (InfixR (-3)) x y
234pattern DApp x y = DSep (InfixL 10) x y
235pattern DComma a b = DOp "," (InfixR (-20)) a b
236 252
237braces = DBrace 253braces = DBrace
238parens = DParen 254parens = DParen
239 255
256dApp (DOp0 s f) x = DOpL s f x
257dApp (DOpL s f x) y = DOp s f x y
258dApp x y = DApp x y
259
260shCstr = DCstr
261
240shTuple [] = "()" 262shTuple [] = "()"
241shTuple [x] = DParen $ DParen x 263shTuple [x] = DParen $ DParen x
242shTuple xs = DParen $ foldr1 DComma xs 264shTuple xs = DParen $ foldr1 DComma xs
243 265
244pattern DLet x y = DOp ":=" (Infix (-4)) x y
245
246shAnn True x (strip -> DText "Type") = x 266shAnn True x (strip -> DText "Type") = x
247shAnn _ x y = DOp "::" (InfixR (-3)) x y 267shAnn True x (strip -> DText "'Type") = x
268shAnn _ x y = DAnn x y
248 269
249shArr = DArr 270shArr = DArr
250 271
251shCstr = DOp "~" (Infix (-2))
252 272
253pattern DForall s vs e = DArr_ s (DSep (Infix 10) (DText "forall") vs) e 273pattern DForall s vs e = DArr_ s (DSep (Infix 10) (DText "forall") vs) e
254pattern DContext vs e = DArr_ "=>" vs e 274pattern DContext vs e = DArr_ "=>" vs e
255pattern DParContext vs e = DContext (DParen vs) e 275pattern DParContext vs e = DContext (DParen vs) e
256pattern DLam vs e = DSep (InfixR (-10)) (DAtom (ComplexAtom "\\" 11 vs (SimpleAtom "->"))) e 276pattern DLam vs e = DPreOp (-10) (ComplexAtom "\\" 11 vs (SimpleAtom "->")) e
257 277
258-------------------------------------------------------------------------------- 278--------------------------------------------------------------------------------
259 279
diff --git a/src/LambdaCube/Compiler/Utils.hs b/src/LambdaCube/Compiler/Utils.hs
index 8e5dbee5..78872b04 100644
--- a/src/LambdaCube/Compiler/Utils.hs
+++ b/src/LambdaCube/Compiler/Utils.hs
@@ -36,7 +36,6 @@ unfoldNat z s n | n > 0 = s $ unfoldNat z s (n-1)
36 36
37data Void 37data Void
38 38
39instance Show Void where show = elimVoid
40instance Eq Void where x == y = elimVoid x 39instance Eq Void where x == y = elimVoid x
41 40
42elimVoid :: Void -> a 41elimVoid :: Void -> a
@@ -47,7 +46,6 @@ elimVoid v = case v of
47-- supplementary data: data with no semantic relevance 46-- supplementary data: data with no semantic relevance
48newtype SData a = SData a 47newtype SData a = SData a
49 48
50instance Show (SData a) where show _ = "SData"
51instance Eq (SData a) where _ == _ = True 49instance Eq (SData a) where _ == _ = True
52instance Ord (SData a) where _ `compare` _ = EQ 50instance Ord (SData a) where _ `compare` _ = EQ
53 51