diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-05-01 07:30:17 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-05-01 07:30:17 +0200 |
commit | 991725b2c054359388feab08373ebfd0683b5e46 (patch) | |
tree | 5672a9d7931e5d420b75c0449033af1c847733d1 /src | |
parent | 1fd867e5beea5b4197f300e2e964c0f6b0035830 (diff) |
refactoring
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/Compiler.hs | 4 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/DesugaredSource.hs | 23 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Infer.hs | 4 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Lexer.hs | 2 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Pretty.hs | 44 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Utils.hs | 2 |
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 |
191 | getDef :: MonadMask m => FilePath -> SName -> Maybe Exp -> MMT m Infos (Infos, Either Doc (FilePath, Either Doc (Exp, Exp))) | 191 | getDef :: MonadMask m => FilePath -> SName -> Maybe Exp -> MMT m (Infos, [Stmt]) ((Infos, [Stmt]), Either Doc (FilePath, Either Doc (Exp, Exp))) |
192 | getDef = getDef_ fst | 192 | getDef = getDef_ id |
193 | 193 | ||
194 | getDef_ ex m d ty = loadModule ex Nothing (Left m) <&> \case | 194 | getDef_ 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 | |||
79 | instance Ord FileInfo where compare = compare `on` fileId | 79 | instance Ord FileInfo where compare = compare `on` fileId |
80 | 80 | ||
81 | instance PShow FileInfo where pShow = text . filePath | 81 | instance PShow FileInfo where pShow = text . filePath |
82 | instance Show FileInfo where show = ppShow | ||
83 | 82 | ||
84 | showPos :: FileInfo -> SPos -> Doc | 83 | showPos :: FileInfo -> SPos -> Doc |
85 | showPos n p = pShow n <> ":" <> pShow p | 84 | showPos 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 | ||
120 | instance Show SI where show _ = "SI" | 119 | --instance Show SI where show _ = "SI" |
121 | instance Eq SI where _ == _ = True | 120 | instance Eq SI where _ == _ = True |
122 | instance Ord SI where _ `compare` _ = EQ | 121 | instance Ord SI where _ `compare` _ = EQ |
123 | 122 | ||
@@ -149,7 +148,7 @@ pattern SIName si n <- SIName_ si _ n | |||
149 | 148 | ||
150 | instance Eq SIName where (==) = (==) `on` sName | 149 | instance Eq SIName where (==) = (==) `on` sName |
151 | instance Ord SIName where compare = compare `on` sName | 150 | instance Ord SIName where compare = compare `on` sName |
152 | instance Show SIName where show = sName | 151 | --instance Show SIName where show = sName |
153 | instance PShow SIName | 152 | instance 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 | ||
215 | data Visibility = Hidden | Visible | 214 | data Visibility = Hidden | Visible |
216 | deriving (Eq, Show) | 215 | deriving (Eq) |
217 | 216 | ||
218 | dummyName s = SIName (debugSI s) ("v_" ++ s) | 217 | dummyName s = SIName (debugSI s) ("v_" ++ s) |
219 | dummyName' = SData . dummyName | 218 | dummyName' = SData . dummyName |
@@ -380,10 +379,11 @@ trSExp' = trSExp elimVoid | |||
380 | 379 | ||
381 | instance (Up a, PShow a) => PShow (SExp' a) where | 380 | instance (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 | |||
427 | showLam x (DLam xs y) = DLam (DSep (InfixR 11) x xs) y | 427 | showLam x (DLam xs y) = DLam (DSep (InfixR 11) x xs) y |
428 | showLam x y = DLam x y | 428 | showLam x y = DLam x y |
429 | 429 | ||
430 | shLet i a b = showLam (DLet (blue $ shVar i) $ DUp i a) (DUp i b) | 430 | shLet i a b = showLam (DLet ":=" (blue $ shVar i) $ DUp i a) (DUp i b) |
431 | shLet_ a b = DFreshName True $ showLam (DLet (shVar 0) $ DUp 0 a) b | 431 | shLet_ 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 | ||
442 | instance PShow Stmt where | 442 | instance 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 | ||
448 | instance DeBruijnify SIName Stmt where | 449 | instance 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 | ||
65 | data Freq = CompileTime | RunTime | 65 | data Freq = CompileTime | RunTime |
66 | deriving (Eq, Show) | 66 | deriving (Eq) |
67 | 67 | ||
68 | pattern TType = TType_ CompileTime | 68 | pattern 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 | ||
759 | instance Show MaxDB where show _ = "MaxDB" | 759 | --instance Show MaxDB where show _ = "MaxDB" |
760 | 760 | ||
761 | varDB i = MaxDB 1 -- | 761 | varDB 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 | ||
183 | data Namespace = TypeNS | ExpNS | 183 | data Namespace = TypeNS | ExpNS |
184 | deriving (Eq, Show) | 184 | deriving (Eq) |
185 | 185 | ||
186 | tick c = f <$> asks namespace | 186 | tick 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 | ||
40 | instance PShow Fixity where | 40 | instance 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 | |||
220 | shortForm d = DPar "" d "" | 230 | shortForm d = DPar "" d "" |
221 | expand = DExpand | 231 | expand = DExpand |
222 | 232 | ||
223 | pattern DPar l d r = DAtom (ComplexAtom l (-20) d (SimpleAtom r)) | 233 | pattern DAt x = DGlue (InfixR 20) (DText "@") x |
234 | pattern DApp x y = DSep (InfixL 10) x y | ||
235 | pattern DArr_ s x y = DOp s (InfixR (-1)) x y -- -> => . | ||
236 | pattern DCstr x y = DOp "~" (Infix (-2)) x y | ||
237 | pattern DAnn x y = DOp "::" (Infix (-3)) x y | ||
238 | pattern DLet s x y = DOp s (Infix (-4)) x y -- := = | ||
239 | pattern DComma a b = DOp "," (InfixR (-20)) a b | ||
240 | pattern DPar l d r = DAtom (ComplexAtom l (-20) d (SimpleAtom r)) | ||
241 | |||
224 | pattern DParen x = DPar "(" x ")" | 242 | pattern DParen x = DPar "(" x ")" |
225 | pattern DBrace x = DPar "{" x "}" | 243 | pattern DBrace x = DPar "{" x "}" |
226 | pattern DOp s f l r = DInfix f l (SimpleAtom s) r | 244 | pattern DOp s f l r = DInfix f l (SimpleAtom s) r |
245 | pattern DOp0 s f = DOp s f (DText "") (DText "") | ||
246 | pattern DOpL s f x = DOp s f x (DText "") | ||
247 | pattern DOpR s f x = DOp s f (DText "") x | ||
227 | pattern DSep p a b = DOp " " p a b | 248 | pattern DSep p a b = DOp " " p a b |
228 | pattern DGlue p a b = DOp "" p a b | 249 | pattern DGlue p a b = DOp "" p a b |
229 | pattern DAt x = DGlue (InfixR 20) (DText "@") x | ||
230 | 250 | ||
231 | pattern DArr_ s x y = DOp s (InfixR (-1)) x y | ||
232 | pattern DArr x y = DArr_ "->" x y | 251 | pattern DArr x y = DArr_ "->" x y |
233 | pattern DAnn x y = DOp "::" (InfixR (-3)) x y | ||
234 | pattern DApp x y = DSep (InfixL 10) x y | ||
235 | pattern DComma a b = DOp "," (InfixR (-20)) a b | ||
236 | 252 | ||
237 | braces = DBrace | 253 | braces = DBrace |
238 | parens = DParen | 254 | parens = DParen |
239 | 255 | ||
256 | dApp (DOp0 s f) x = DOpL s f x | ||
257 | dApp (DOpL s f x) y = DOp s f x y | ||
258 | dApp x y = DApp x y | ||
259 | |||
260 | shCstr = DCstr | ||
261 | |||
240 | shTuple [] = "()" | 262 | shTuple [] = "()" |
241 | shTuple [x] = DParen $ DParen x | 263 | shTuple [x] = DParen $ DParen x |
242 | shTuple xs = DParen $ foldr1 DComma xs | 264 | shTuple xs = DParen $ foldr1 DComma xs |
243 | 265 | ||
244 | pattern DLet x y = DOp ":=" (Infix (-4)) x y | ||
245 | |||
246 | shAnn True x (strip -> DText "Type") = x | 266 | shAnn True x (strip -> DText "Type") = x |
247 | shAnn _ x y = DOp "::" (InfixR (-3)) x y | 267 | shAnn True x (strip -> DText "'Type") = x |
268 | shAnn _ x y = DAnn x y | ||
248 | 269 | ||
249 | shArr = DArr | 270 | shArr = DArr |
250 | 271 | ||
251 | shCstr = DOp "~" (Infix (-2)) | ||
252 | 272 | ||
253 | pattern DForall s vs e = DArr_ s (DSep (Infix 10) (DText "forall") vs) e | 273 | pattern DForall s vs e = DArr_ s (DSep (Infix 10) (DText "forall") vs) e |
254 | pattern DContext vs e = DArr_ "=>" vs e | 274 | pattern DContext vs e = DArr_ "=>" vs e |
255 | pattern DParContext vs e = DContext (DParen vs) e | 275 | pattern DParContext vs e = DContext (DParen vs) e |
256 | pattern DLam vs e = DSep (InfixR (-10)) (DAtom (ComplexAtom "\\" 11 vs (SimpleAtom "->"))) e | 276 | pattern 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 | ||
37 | data Void | 37 | data Void |
38 | 38 | ||
39 | instance Show Void where show = elimVoid | ||
40 | instance Eq Void where x == y = elimVoid x | 39 | instance Eq Void where x == y = elimVoid x |
41 | 40 | ||
42 | elimVoid :: Void -> a | 41 | elimVoid :: 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 |
48 | newtype SData a = SData a | 47 | newtype SData a = SData a |
49 | 48 | ||
50 | instance Show (SData a) where show _ = "SData" | ||
51 | instance Eq (SData a) where _ == _ = True | 49 | instance Eq (SData a) where _ == _ = True |
52 | instance Ord (SData a) where _ `compare` _ = EQ | 50 | instance Ord (SData a) where _ `compare` _ = EQ |
53 | 51 | ||