summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCsaba Hruska <csaba.hruska@gmail.com>2017-09-22 20:43:20 +0100
committerCsaba Hruska <csaba.hruska@gmail.com>2017-09-22 20:43:20 +0100
commite205a839ce8cf1e0f71b88a7a9c7a793848b8f8e (patch)
treefe7bb921684f8804e93c3b9ff0de9085f3048ecf /src
parente8bf0b7bcfb28bddfef1b8110cec848a8c4a4ff4 (diff)
Exp binary instance
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/Compiler.hs6
-rw-r--r--src/LambdaCube/Compiler/Core.hs83
-rw-r--r--src/LambdaCube/Compiler/DeBruijn.hs8
-rw-r--r--src/LambdaCube/Compiler/DesugaredSource.hs58
-rw-r--r--src/LambdaCube/Compiler/InferMonad.hs2
-rw-r--r--src/LambdaCube/Compiler/Pretty.hs8
-rw-r--r--src/LambdaCube/Compiler/Utils.hs11
7 files changed, 161 insertions, 15 deletions
diff --git a/src/LambdaCube/Compiler.hs b/src/LambdaCube/Compiler.hs
index a61686ff..4d5213c3 100644
--- a/src/LambdaCube/Compiler.hs
+++ b/src/LambdaCube/Compiler.hs
@@ -89,7 +89,7 @@ type ModuleFetcher m = Maybe FilePath -> Either FilePath MName -> m (Either Doc
89ioFetch :: MonadIO m => [FilePath] -> ModuleFetcher (MMT m x) 89ioFetch :: MonadIO m => [FilePath] -> ModuleFetcher (MMT m x)
90ioFetch paths' imp n = do 90ioFetch paths' imp n = do
91 preludePath <- (</> "lc") <$> liftIO getDataDir 91 preludePath <- (</> "lc") <$> liftIO getDataDir
92 let paths = map (id &&& id) paths' ++ [(preludePath, "<<installed-prelude-path>>")] 92 let paths = map (id &&& id) paths' ++ [(preludePath, preludePath)]
93 find ((x, (x', mn)): xs) = liftIO (readFileIfExists x) >>= maybe (find xs) (\src -> return $ Right (x', mn, liftIO src)) 93 find ((x, (x', mn)): xs) = liftIO (readFileIfExists x) >>= maybe (find xs) (\src -> return $ Right (x', mn, liftIO src))
94 find [] = return $ Left $ "can't find" <+> either (("lc file" <+>) . text) (("module" <+>) . text) n 94 find [] = return $ Left $ "can't find" <+> either (("lc file" <+>) . text) (("module" <+>) . text) n
95 <+> "in path" <+> hsep (text . snd <$> paths) 95 <+> "in path" <+> hsep (text . snd <$> paths)
@@ -141,7 +141,7 @@ loadModule ex imp mname_ = do
141 src <- srcm 141 src <- srcm
142 fid <- gets nextMId 142 fid <- gets nextMId
143 modify $ \(Modules nm im ni) -> Modules (Map.insert fname fid nm) im $ ni+1 143 modify $ \(Modules nm im ni) -> Modules (Map.insert fname fid nm) im $ ni+1
144 let fi = FileInfo fid fname mname src 144 let fi = FileInfo fid fname mname
145 res <- case parseLC fi of 145 res <- case parseLC fi of
146 Left e -> return $ Left $ text $ show e 146 Left e -> return $ Left $ text $ show e
147 Right e -> do 147 Right e -> do
@@ -224,7 +224,7 @@ preCompile paths paths' backend mod = do
224 where 224 where
225 compile src = runMM fetch $ do 225 compile src = runMM fetch $ do
226 let pname = "." </> "Prelude.lc" 226 let pname = "." </> "Prelude.lc"
227 modify $ \(Modules nm im ni) -> Modules (Map.insert pname ni nm) (IM.insert ni (FileInfo ni pname "Prelude" $ fileContent fi, prelude) im) (ni+1) 227 modify $ \(Modules nm im ni) -> Modules (Map.insert pname ni nm) (IM.insert ni (FileInfo ni pname "Prelude", prelude) im) (ni+1)
228 (snd &&& fst) <$> compilePipeline' ex backend "Main" 228 (snd &&& fst) <$> compilePipeline' ex backend "Main"
229 where 229 where
230 fetch imp = \case 230 fetch imp = \case
diff --git a/src/LambdaCube/Compiler/Core.hs b/src/LambdaCube/Compiler/Core.hs
index 91056dd9..8e97c6b2 100644
--- a/src/LambdaCube/Compiler/Core.hs
+++ b/src/LambdaCube/Compiler/Core.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE DeriveGeneric #-}
1{-# LANGUAGE OverloadedStrings #-} 2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE LambdaCase #-} 3{-# LANGUAGE LambdaCase #-}
3{-# LANGUAGE ViewPatterns #-} 4{-# LANGUAGE ViewPatterns #-}
@@ -12,6 +13,9 @@
12--{-# OPTIONS_GHC -fno-warn-unused-binds #-} -- TODO: remove 13--{-# OPTIONS_GHC -fno-warn-unused-binds #-} -- TODO: remove
13module LambdaCube.Compiler.Core where 14module LambdaCube.Compiler.Core where
14 15
16import Data.Binary
17import GHC.Generics (Generic)
18
15import Data.Monoid 19import Data.Monoid
16import Data.Function 20import Data.Function
17import Data.List 21import Data.List
@@ -25,14 +29,18 @@ import LambdaCube.Compiler.DesugaredSource
25-------------------------------------------------------------------------------- names with infos 29-------------------------------------------------------------------------------- names with infos
26 30
27data ConName = ConName FName Int{-ordinal number, e.g. Zero:0, Succ:1-} Type 31data ConName = ConName FName Int{-ordinal number, e.g. Zero:0, Succ:1-} Type
32 deriving Generic
28 33
29data TyConName = TyConName FName Int{-num of indices-} Type [(ConName, Type)]{-constructors-} CaseFunName 34data TyConName = TyConName FName Int{-num of indices-} Type [(ConName, Type)]{-constructors-} CaseFunName
35 deriving Generic
30 36
31data FunName = FunName FName Int{-num of global vars-} FunDef Type 37data FunName = FunName FName Int{-num of global vars-} FunDef Type
32 38
33data CaseFunName = CaseFunName FName Type Int{-num of parameters-} 39data CaseFunName = CaseFunName FName Type Int{-num of parameters-}
40 deriving Generic
34 41
35data TyCaseFunName = TyCaseFunName FName Type 42data TyCaseFunName = TyCaseFunName FName Type
43 deriving Generic
36 44
37data FunDef 45data FunDef
38 = DeltaDef !Int{-arity-} (FreeVars -> [Exp]{-args in reversed order-} -> Exp) 46 = DeltaDef !Int{-arity-} (FreeVars -> [Exp]{-args in reversed order-} -> Exp)
@@ -67,7 +75,7 @@ instance PShow TyCaseFunName where pShow (TyCaseFunName n _) = text $ MatchName
67-------------------------------------------------------------------------------- core expression representation 75-------------------------------------------------------------------------------- core expression representation
68 76
69data Freq = CompileTime | RunTime -- TODO 77data Freq = CompileTime | RunTime -- TODO
70 deriving (Eq) 78 deriving (Eq, Generic)
71 79
72data Exp 80data Exp
73 = ELit Lit 81 = ELit Lit
@@ -80,6 +88,8 @@ data Exp
80 | RHS Exp{-always in hnf-} 88 | RHS Exp{-always in hnf-}
81 | Let_ FreeVars ExpType Exp 89 | Let_ FreeVars ExpType Exp
82 | Up_ FreeVars [Int] Exp 90 | Up_ FreeVars [Int] Exp
91 | STOP
92 deriving Generic
83 93
84data Neutral 94data Neutral
85 = Var_ !Int{-De Bruijn index-} 95 = Var_ !Int{-De Bruijn index-}
@@ -88,13 +98,14 @@ data Neutral
88 | TyCaseFun__ FreeVars TyCaseFunName [Exp] Neutral 98 | TyCaseFun__ FreeVars TyCaseFunName [Exp] Neutral
89 | Fun_ FreeVars FunName [Exp]{-given parameters, reversed-} Exp{-unfolded expression, in hnf-} 99 | Fun_ FreeVars FunName [Exp]{-given parameters, reversed-} Exp{-unfolded expression, in hnf-}
90 | UpN_ FreeVars [Int] Neutral 100 | UpN_ FreeVars [Int] Neutral
101 deriving Generic
91 102
92-------------------------------------------------------------------------------- auxiliary functions and patterns 103-------------------------------------------------------------------------------- auxiliary functions and patterns
93 104
94type Type = Exp 105type Type = Exp
95 106
96data ExpType = ET {expr :: Exp, ty :: Type} 107data ExpType = ET {expr :: Exp, ty :: Type}
97 deriving (Eq) 108 deriving (Eq, Generic)
98{- 109{-
99pattern ET a b <- ET_ a b 110pattern ET a b <- ET_ a b
100 where ET a b = ET_ a (hnf b) 111 where ET a b = ET_ a (hnf b)
@@ -469,6 +480,7 @@ instance MkDoc Neutral where
469 MT "finElim" [m, z, s, n, ConN "FZero" [i]] -> z `app_` i 480 MT "finElim" [m, z, s, n, ConN "FZero" [i]] -> z `app_` i
470-} 481-}
471 482
483mkFunDef :: FName -> Type -> FunName
472mkFunDef a@(FTag FprimFix) t = fn 484mkFunDef a@(FTag FprimFix) t = fn
473 where 485 where
474 fn = FunName a 0 (DeltaDef 2 fx) t 486 fn = FunName a 0 (DeltaDef 2 fx) t
@@ -681,3 +693,70 @@ instance NType Lit where
681 LChar _ -> TChar 693 LChar _ -> TChar
682 694
683 695
696closeTyConName (TyConName fname ints type_ cons caseFunName)
697 -- = TyConName fname ints (closeExp type_) [(closeConName n, closeExp t) | (n,t) <- cons] (closeCaseFunName caseFunName)
698 = TyConName fname ints STOP [(closeConName n, STOP) | (n,t) <- cons] (closeCaseFunName caseFunName)
699
700closeTyCaseFunName (TyCaseFunName fname type_)
701 -- = TyCaseFunName fname (closeExp type_)
702 = TyCaseFunName fname STOP
703
704closeConName (ConName fname ordinal type_)
705 -- = ConName fname ordinal (closeExp type_)
706 = ConName fname ordinal STOP
707
708closeCaseFunName (CaseFunName fname type_ int)
709 -- = CaseFunName fname (closeExp type_) int
710 = CaseFunName fname STOP int
711
712closeFunName (FunName fname int funDef type_)
713 -- = FunName fname int (closeFunDef funDef) (closeExp type_)
714 = FunName fname int (closeFunDef funDef) STOP
715
716closeFunDef = \case
717 ExpDef exp -> ExpDef STOP
718 --ExpDef exp -> ExpDef (closeExp exp)
719 x -> x
720
721closeExp :: Exp -> Exp
722closeExp = \case
723 Lam_ freeVars exp -> Lam_ freeVars $ closeExp exp
724 Con_ freeVars conName noErasedApplied argsReversed -> Con_ freeVars (closeConName conName) noErasedApplied (map closeExp argsReversed)
725 TyCon_ freeVars tyConName argsReversed -> TyCon_ freeVars (closeTyConName tyConName) (map closeExp argsReversed)
726 Pi_ freeVars visibility exp1 exp2 -> Pi_ freeVars visibility (closeExp exp1) (closeExp exp2)
727 Neut neutral -> Neut $ closeNeutral neutral
728 RHS whnf -> STOP
729 Let_ freeVars expType exp -> Let_ freeVars (closeExpType expType) (closeExp exp)
730 Up_ freeVars ints exp -> Up_ freeVars ints (closeExp exp)
731 e -> e
732
733closeNeutral :: Neutral -> Neutral
734closeNeutral = \case
735 App__ freeVars neutral exp -> App__ freeVars (closeNeutral neutral) (closeExp exp)
736 CaseFun__ freeVars caseFunName exps neutral -> CaseFun__ freeVars (closeCaseFunName caseFunName) (map closeExp exps) (closeNeutral neutral)
737 TyCaseFun__ freeVars tyCaseFunName exps neutral -> TyCaseFun__ freeVars (closeTyCaseFunName tyCaseFunName) (map closeExp exps) (closeNeutral neutral)
738 Fun_ freeVars funName exps exp -> Fun_ freeVars (closeFunName funName) (map closeExp exps) (closeExp exp)
739 UpN_ freeVars ints neutral -> UpN_ freeVars ints (closeNeutral neutral)
740 n -> n
741
742closeExpType :: ExpType -> ExpType
743closeExpType (ET e t) = ET (closeExp e) (closeExp t)
744
745instance Binary ExpType
746instance Binary Exp
747instance Binary Neutral
748instance Binary Freq
749instance Binary ConName
750instance Binary CaseFunName
751instance Binary TyConName
752instance Binary TyCaseFunName
753
754instance Binary FunName where -- do FunName/FunDef instance together
755 put (FunName fName int funDef type_) = do
756 put fName
757 put type_
758
759 get = do
760 fName <- get
761 type_ <- get
762 pure $ mkFunDef fName type_
diff --git a/src/LambdaCube/Compiler/DeBruijn.hs b/src/LambdaCube/Compiler/DeBruijn.hs
index a1af40db..a0fd8326 100644
--- a/src/LambdaCube/Compiler/DeBruijn.hs
+++ b/src/LambdaCube/Compiler/DeBruijn.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE DeriveGeneric #-}
1{-# LANGUAGE LambdaCase #-} 2{-# LANGUAGE LambdaCase #-}
2{-# LANGUAGE ViewPatterns #-} 3{-# LANGUAGE ViewPatterns #-}
3{-# LANGUAGE PatternSynonyms #-} 4{-# LANGUAGE PatternSynonyms #-}
@@ -10,6 +11,9 @@
10{-# LANGUAGE MultiParamTypeClasses #-} 11{-# LANGUAGE MultiParamTypeClasses #-}
11module LambdaCube.Compiler.DeBruijn where 12module LambdaCube.Compiler.DeBruijn where
12 13
14import Data.Binary
15import GHC.Generics (Generic)
16
13import Data.Bits 17import Data.Bits
14import Control.Arrow hiding ((<+>)) 18import Control.Arrow hiding ((<+>))
15 19
@@ -63,7 +67,9 @@ instance Rearrange Void where
63------------------------------------------------------- set of free variables (implemented with bit vectors) 67------------------------------------------------------- set of free variables (implemented with bit vectors)
64 68
65newtype FreeVars = FreeVars Integer 69newtype FreeVars = FreeVars Integer
66 deriving Eq 70 deriving (Eq, Generic)
71
72instance Binary FreeVars
67 73
68instance PShow FreeVars where 74instance PShow FreeVars where
69 pShow (FreeVars s) = "FreeVars" `DApp` pShow s 75 pShow (FreeVars s) = "FreeVars" `DApp` pShow s
diff --git a/src/LambdaCube/Compiler/DesugaredSource.hs b/src/LambdaCube/Compiler/DesugaredSource.hs
index d355b4b9..8d9a38be 100644
--- a/src/LambdaCube/Compiler/DesugaredSource.hs
+++ b/src/LambdaCube/Compiler/DesugaredSource.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE DeriveGeneric #-}
1{-# LANGUAGE LambdaCase #-} 2{-# LANGUAGE LambdaCase #-}
2{-# LANGUAGE ViewPatterns #-} 3{-# LANGUAGE ViewPatterns #-}
3{-# LANGUAGE PatternSynonyms #-} 4{-# LANGUAGE PatternSynonyms #-}
@@ -13,6 +14,13 @@ module LambdaCube.Compiler.DesugaredSource
13 , Fixity(..) 14 , Fixity(..)
14 )where 15 )where
15 16
17import System.IO.Unsafe
18import Text.Printf
19import qualified Data.ByteString.Char8 as BS
20
21import Data.Binary
22import GHC.Generics (Generic)
23
16import Data.Monoid 24import Data.Monoid
17import Data.Maybe 25import Data.Maybe
18import Data.List 26import Data.List
@@ -51,7 +59,9 @@ pattern MatchName cs <- 'm':'a':'t':'c':'h':cs where MatchName cs = "match" ++ c
51 59
52-- source position without file name 60-- source position without file name
53newtype SPos = SPos_ Int 61newtype SPos = SPos_ Int
54 deriving (Eq, Ord) 62 deriving (Eq, Ord, Generic)
63
64instance Binary SPos
55 65
56row :: SPos -> Int 66row :: SPos -> Int
57row (SPos_ i) = i `shiftR` 16 67row (SPos_ i) = i `shiftR` 16
@@ -72,8 +82,16 @@ data FileInfo = FileInfo
72 { fileId :: !Int 82 { fileId :: !Int
73 , filePath :: FilePath 83 , filePath :: FilePath
74 , fileModule :: String -- module name 84 , fileModule :: String -- module name
75 , fileContent :: String
76 } 85 }
86 deriving Generic
87
88instance Binary FileInfo
89
90fileContent :: FileInfo -> String
91fileContent fi = unsafePerformIO $ do
92 let fname = filePath fi
93 BS.putStrLn $ BS.pack $ printf "load source %s" fname
94 readFile fname
77 95
78instance Eq FileInfo where (==) = (==) `on` fileId 96instance Eq FileInfo where (==) = (==) `on` fileId
79instance Ord FileInfo where compare = compare `on` fileId 97instance Ord FileInfo where compare = compare `on` fileId
@@ -90,7 +108,9 @@ data Range = Range
90 , rangeStart :: !SPos 108 , rangeStart :: !SPos
91 , rangeStop :: !SPos 109 , rangeStop :: !SPos
92 } 110 }
93 deriving (Eq, Ord) 111 deriving (Eq, Ord, Generic)
112
113instance Binary Range
94 114
95instance Show Range where show = ppShow 115instance Show Range where show = ppShow
96instance PShow Range 116instance PShow Range
@@ -112,6 +132,9 @@ joinRange (Range n b e) (Range n' b' e') = Range n (min b b') (max e e')
112data SI 132data SI
113 = NoSI (Set.Set String) -- no source info, attached debug info 133 = NoSI (Set.Set String) -- no source info, attached debug info
114 | RangeSI Range 134 | RangeSI Range
135 deriving Generic
136
137instance Binary SI
115 138
116getRange :: SI -> Maybe Range 139getRange :: SI -> Maybe Range
117getRange (RangeSI r) = Just r 140getRange (RangeSI r) = Just r
@@ -159,6 +182,9 @@ class SetSourceInfo a where
159-------------------------------------------------------------------------------- name with source info 182-------------------------------------------------------------------------------- name with source info
160 183
161data SIName = SIName__ { nameHash :: Int, nameSI :: SI, nameFixity :: Maybe Fixity, sName :: SName } 184data SIName = SIName__ { nameHash :: Int, nameSI :: SI, nameFixity :: Maybe Fixity, sName :: SName }
185 deriving Generic
186
187instance Binary SIName
162 188
163pattern SIName_ :: SI -> Maybe Fixity -> SName -> SIName 189pattern SIName_ :: SI -> Maybe Fixity -> SName -> SIName
164pattern SIName_ si f n <- SIName__ _ si f n 190pattern SIName_ si f n <- SIName__ _ si f n
@@ -186,6 +212,9 @@ instance SetSourceInfo SIName where
186-------------------------------------------------------------------------------- hashed names 212-------------------------------------------------------------------------------- hashed names
187 213
188newtype FName = FName { fName :: SIName } 214newtype FName = FName { fName :: SIName }
215 deriving Generic
216
217instance Binary FName
189 218
190instance Eq FName where (==) = (==) `on` nameHash . fName 219instance Eq FName where (==) = (==) `on` nameHash . fName
191instance Ord FName where compare = compare `on` nameHash . fName 220instance Ord FName where compare = compare `on` nameHash . fName
@@ -228,7 +257,9 @@ data FNameTag
228 257
229 -- other 258 -- other
230 | F_rhs | F_section 259 | F_rhs | F_section
231 deriving (Eq, Ord, Show, Enum, Bounded) 260 deriving (Eq, Ord, Show, Enum, Bounded, Generic)
261
262instance Binary FNameTag
232 263
233tagName :: FNameTag -> String 264tagName :: FNameTag -> String
234tagName FCons = ":" 265tagName FCons = ":"
@@ -267,7 +298,9 @@ data Lit
267 | LChar Char 298 | LChar Char
268 | LFloat Double 299 | LFloat Double
269 | LString String 300 | LString String
270 deriving (Eq) 301 deriving (Eq, Generic)
302
303instance Binary Lit
271 304
272instance PShow Lit where 305instance PShow Lit where
273 pShow = \case 306 pShow = \case
@@ -287,7 +320,9 @@ data SExp' a
287 | SLet_ SI (SData SIName) (SExp' a) (SExp' a) -- let x = e in f --> SLet e f{-x is Var 0-} 320 | SLet_ SI (SData SIName) (SExp' a) (SExp' a) -- let x = e in f --> SLet e f{-x is Var 0-}
288 | SLHS SIName (SExp' a) 321 | SLHS SIName (SExp' a)
289 | STyped a 322 | STyped a
290 deriving (Eq) 323 deriving (Eq, Generic)
324
325instance Binary SExp
291 326
292sLHS :: SIName -> SExp' a -> SExp' a 327sLHS :: SIName -> SExp' a -> SExp' a
293sLHS _ (SRHS x) = x 328sLHS _ (SRHS x) = x
@@ -299,7 +334,9 @@ data Binder
299 = BPi Visibility 334 = BPi Visibility
300 | BLam Visibility 335 | BLam Visibility
301 | BMeta -- a metavariable is like a floating hidden lambda 336 | BMeta -- a metavariable is like a floating hidden lambda
302 deriving (Eq) 337 deriving (Eq, Generic)
338
339instance Binary Binder
303 340
304instance PShow Binder where 341instance PShow Binder where
305 pShow = \case 342 pShow = \case
@@ -308,7 +345,9 @@ instance PShow Binder where
308 BMeta -> "BMeta" 345 BMeta -> "BMeta"
309 346
310data Visibility = Hidden | Visible 347data Visibility = Hidden | Visible
311 deriving (Eq) 348 deriving (Eq, Generic)
349
350instance Binary Visibility
312 351
313instance PShow Visibility where 352instance PShow Visibility where
314 pShow = \case 353 pShow = \case
@@ -617,6 +656,9 @@ data Stmt
617 = StmtLet SIName SExp 656 = StmtLet SIName SExp
618 | Data SIName [(Visibility, SExp)]{-parameters-} SExp{-type-} [(SIName, SExp)]{-constructor names and types-} 657 | Data SIName [(Visibility, SExp)]{-parameters-} SExp{-type-} [(SIName, SExp)]{-constructor names and types-}
619 | PrecDef SIName Fixity 658 | PrecDef SIName Fixity
659 deriving Generic
660
661instance Binary Stmt
620 662
621pattern StLet :: SIName -> Maybe (SExp' Void) -> SExp' Void -> Stmt 663pattern StLet :: SIName -> Maybe (SExp' Void) -> SExp' Void -> Stmt
622pattern StLet n mt x <- StmtLet n (getSAnn -> (x, mt)) 664pattern StLet n mt x <- StmtLet n (getSAnn -> (x, mt))
diff --git a/src/LambdaCube/Compiler/InferMonad.hs b/src/LambdaCube/Compiler/InferMonad.hs
index 1905c839..2499fa5c 100644
--- a/src/LambdaCube/Compiler/InferMonad.hs
+++ b/src/LambdaCube/Compiler/InferMonad.hs
@@ -214,3 +214,5 @@ dependentVars ie = cycle mempty
214 a <-> b = (a --> b) <> (b --> a) 214 a <-> b = (a --> b) <> (b --> a)
215 215
216 216
217--closeGlobalEnv :: GlobalEnv -> GlobalEnv
218closeGlobalEnv = fmap (\(exp, type_, si) -> (closeExp exp {-, closeExp type_, si)-})) -- HINT: type should be finite
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs
index 317c4080..24e540dd 100644
--- a/src/LambdaCube/Compiler/Pretty.hs
+++ b/src/LambdaCube/Compiler/Pretty.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE DeriveGeneric #-}
1{-# LANGUAGE OverloadedStrings #-} 2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE NoMonomorphismRestriction #-} 3{-# LANGUAGE NoMonomorphismRestriction #-}
3{-# LANGUAGE LambdaCase #-} 4{-# LANGUAGE LambdaCase #-}
@@ -13,6 +14,9 @@ module LambdaCube.Compiler.Pretty
13 ( module LambdaCube.Compiler.Pretty 14 ( module LambdaCube.Compiler.Pretty
14 ) where 15 ) where
15 16
17import Data.Binary (Binary)
18import GHC.Generics (Generic)
19
16import Data.Maybe 20import Data.Maybe
17import Data.String 21import Data.String
18import Data.Char 22import Data.Char
@@ -36,7 +40,9 @@ data Fixity
36 = Infix !Int 40 = Infix !Int
37 | InfixL !Int 41 | InfixL !Int
38 | InfixR !Int 42 | InfixR !Int
39 deriving (Eq) 43 deriving (Eq, Generic)
44
45instance Binary Fixity
40 46
41instance PShow Fixity where 47instance PShow Fixity where
42 pShow = \case 48 pShow = \case
diff --git a/src/LambdaCube/Compiler/Utils.hs b/src/LambdaCube/Compiler/Utils.hs
index 2a86f80b..3d1dc1ec 100644
--- a/src/LambdaCube/Compiler/Utils.hs
+++ b/src/LambdaCube/Compiler/Utils.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE DeriveGeneric #-}
1{-# LANGUAGE NoMonomorphismRestriction #-} 2{-# LANGUAGE NoMonomorphismRestriction #-}
2{-# LANGUAGE ScopedTypeVariables #-} 3{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE EmptyCase #-} 4{-# LANGUAGE EmptyCase #-}
@@ -7,6 +8,9 @@
7{-# OPTIONS_GHC -fno-warn-orphans #-} 8{-# OPTIONS_GHC -fno-warn-orphans #-}
8module LambdaCube.Compiler.Utils where 9module LambdaCube.Compiler.Utils where
9 10
11import Data.Binary (Binary(..))
12import GHC.Generics (Generic)
13
10import qualified Data.IntSet as IS 14import qualified Data.IntSet as IS
11import qualified Data.Text as T 15import qualified Data.Text as T
12import qualified Text.Show.Pretty as PP 16import qualified Text.Show.Pretty as PP
@@ -36,6 +40,10 @@ foldlrev f = foldr (flip f)
36 40
37data Void 41data Void
38 42
43instance Binary Void where
44 get = error "Binary get"
45 put = error "Binary put"
46
39instance Eq Void where x == y = elimVoid x 47instance Eq Void where x == y = elimVoid x
40 48
41elimVoid :: Void -> a 49elimVoid :: Void -> a
@@ -45,6 +53,9 @@ elimVoid v = case v of
45 53
46-- supplementary data: data with no semantic relevance 54-- supplementary data: data with no semantic relevance
47newtype SData a = SData a 55newtype SData a = SData a
56 deriving Generic
57
58instance Binary a => Binary (SData a)
48 59
49instance Eq (SData a) where _ == _ = True 60instance Eq (SData a) where _ == _ = True
50instance Ord (SData a) where _ `compare` _ = EQ 61instance Ord (SData a) where _ `compare` _ = EQ