summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKosyrev Serge <serge.kosyrev@iohk.io>2018-01-16 03:12:01 +0300
committerKosyrev Serge <serge.kosyrev@iohk.io>2018-01-16 03:12:01 +0300
commitf844c6f6179eb0a9a0921e26d59cd7fca40520d7 (patch)
tree1e99cf9caf7b0d8b8ff58ea63a80829611f6f8c2
parentcbac76692978c316223eda2407deda4978a09241 (diff)
Semigroup-Monoid Proposal fixes: GHC 8.4 compat
-rw-r--r--prototypes/FreeVars.hs24
-rw-r--r--prototypes/LamMachineV2.hs6
-rw-r--r--prototypes/SplayList.hs8
-rw-r--r--prototypes/Stream.hs5
-rw-r--r--src/LambdaCube/Compiler/DeBruijn.hs6
-rw-r--r--src/LambdaCube/Compiler/DesugaredSource.hs9
-rw-r--r--src/LambdaCube/Compiler/Parser.hs6
-rw-r--r--src/LambdaCube/Compiler/Patterns.hs10
-rw-r--r--src/LambdaCube/Compiler/Pretty.hs6
9 files changed, 80 insertions, 0 deletions
diff --git a/prototypes/FreeVars.hs b/prototypes/FreeVars.hs
index f0fb9a4b..32ad98a7 100644
--- a/prototypes/FreeVars.hs
+++ b/prototypes/FreeVars.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE OverloadedStrings #-} 2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE PatternSynonyms #-} 3{-# LANGUAGE PatternSynonyms #-}
3{-# LANGUAGE PatternGuards #-} 4{-# LANGUAGE PatternGuards #-}
@@ -74,7 +75,12 @@ instance Num Nat where
74 75
75instance Monoid Nat where 76instance Monoid Nat where
76 mempty = 0 77 mempty = 0
78#if !MIN_VERSION_base(4,11,0)
77 Nat a `mappend` Nat b = Nat (a + b) 79 Nat a `mappend` Nat b = Nat (a + b)
80#else
81instance Semigroup Nat where
82 Nat a <> Nat b = Nat (a + b)
83#endif
78 84
79instance PShow Nat where pShow (Nat i) = pShow i 85instance PShow Nat where pShow (Nat i) = pShow i
80instance Show Nat where show = ppShow 86instance Show Nat where show = ppShow
@@ -157,6 +163,7 @@ fromStr = fromBools . map (=='1')
157instance Monoid FV where 163instance Monoid FV where
158 mempty = FE 164 mempty = FE
159 165
166#if !MIN_VERSION_base(4,11,0)
160 mappend x FE = x 167 mappend x FE = x
161 mappend FE x = x 168 mappend FE x = x
162 mappend (FV a b us) (FV a' b' us') 169 mappend (FV a b us) (FV a' b' us')
@@ -166,6 +173,18 @@ instance Monoid FV where
166 | otherwise = fv c (a' + b' - c) $ mappend (FV 0 ((a + b) - (a' + b')) us) us' 173 | otherwise = fv c (a' + b' - c) $ mappend (FV 0 ((a + b) - (a' + b')) us) us'
167 where 174 where
168 c = min a a' 175 c = min a a'
176#else
177instance Semigroup FV where
178 (<>) x FE = x
179 (<>) FE x = x
180 (<>) (FV a b us) (FV a' b' us')
181 | a + b <= a' = fv a b $ us <> (FV (a' - (a + b)) b' us')
182 | a + b - a' <= b' = fv c (a + b - c) $ us <> (FV 0 (b' - (a + b - a')) us')
183 | a' + b' <= a = fv a' b' $ (FV (a - (a' + b')) b us) <> us'
184 | otherwise = fv c (a' + b' - c) $ (FV 0 ((a + b) - (a' + b')) us) <> us'
185 where
186 c = min a a'
187#endif
169 188
170prop_monoid_FV = prop_Monoid (T :: T FV) 189prop_monoid_FV = prop_Monoid (T :: T FV)
171prop_mappend_normal_FV (a :: FV) b = testNormalFV (a <> b) 190prop_mappend_normal_FV (a :: FV) b = testNormalFV (a <> b)
@@ -335,7 +354,12 @@ instance Arbitrary SFV where
335instance Monoid SFV where 354instance Monoid SFV where
336 mempty = SFV 0 mempty 355 mempty = SFV 0 mempty
337 356
357#if !MIN_VERSION_base(4,11,0)
338 SFV m b `mappend` SFV n a = SFV (n + m) $ sDrop n b <> a 358 SFV m b `mappend` SFV n a = SFV (n + m) $ sDrop n b <> a
359#else
360instance Semigroup SFV where
361 SFV m b <> SFV n a = SFV (n + m) $ sDrop n b <> a
362#endif
339 363
340prop_monoid_SFV = prop_Monoid (T :: T SFV) 364prop_monoid_SFV = prop_Monoid (T :: T SFV)
341{- 365{-
diff --git a/prototypes/LamMachineV2.hs b/prototypes/LamMachineV2.hs
index 0d51d8a8..acacdf4b 100644
--- a/prototypes/LamMachineV2.hs
+++ b/prototypes/LamMachineV2.hs
@@ -3,6 +3,7 @@
3-- LamMachine is a variant of the machine described in 3-- LamMachine is a variant of the machine described in
4-- "Deriving a Lazy Abstract Machine" (1997) by Peter Sestoft 4-- "Deriving a Lazy Abstract Machine" (1997) by Peter Sestoft
5 5
6{-# LANGUAGE CPP #-}
6{-# LANGUAGE OverloadedStrings #-} 7{-# LANGUAGE OverloadedStrings #-}
7{-# LANGUAGE PatternSynonyms #-} 8{-# LANGUAGE PatternSynonyms #-}
8{-# LANGUAGE PatternGuards #-} 9{-# LANGUAGE PatternGuards #-}
@@ -192,7 +193,12 @@ newtype MDB = MDB {getMDB :: Int}
192 193
193instance Monoid MDB where 194instance Monoid MDB where
194 mempty = MDB 0 195 mempty = MDB 0
196#if !MIN_VERSION_base(4,11,0)
195 MDB n `mappend` MDB m = MDB $ n `max` m 197 MDB n `mappend` MDB m = MDB $ n `max` m
198#else
199instance Semigroup MDB where
200 MDB n <> MDB m = MDB $ n `max` m
201#endif
196 202
197------------------------------------- rearrange De Bruijn indices 203------------------------------------- rearrange De Bruijn indices
198 204
diff --git a/prototypes/SplayList.hs b/prototypes/SplayList.hs
index 2cee0699..b2a8c743 100644
--- a/prototypes/SplayList.hs
+++ b/prototypes/SplayList.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE FlexibleContexts #-} 2{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE FlexibleInstances #-} 3{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE UndecidableInstances #-} 4{-# LANGUAGE UndecidableInstances #-}
@@ -58,9 +59,16 @@ class (HasFV a, Measure a ~ Nat) => Measured a where
58 59
59instance Measured a => Monoid (SplayList a) where 60instance Measured a => Monoid (SplayList a) where
60 mempty = Nil 61 mempty = Nil
62#if !MIN_VERSION_base(4,11,0)
61 Nil `mappend` ys = ys 63 Nil `mappend` ys = ys
62 xs `mappend` Nil = xs 64 xs `mappend` Nil = xs
63 xs `mappend` ys = Append xs ys 65 xs `mappend` ys = Append xs ys
66#else
67instance Semigroup (SplayList a) where
68 Nil <> ys = ys
69 xs <> Nil = xs
70 xs <> ys = Append xs ys
71#endif
64 72
65instance (Measured a, HasFV a) => HasFV (SplayList a) where 73instance (Measured a, HasFV a) => HasFV (SplayList a) where
66 fvLens = \case 74 fvLens = \case
diff --git a/prototypes/Stream.hs b/prototypes/Stream.hs
index e848fe36..b887f1ad 100644
--- a/prototypes/Stream.hs
+++ b/prototypes/Stream.hs
@@ -139,7 +139,12 @@ dbAnd a b = not <$> sOr (not <$> a) (not <$> b)
139 139
140instance Monoid (Stream Bool) where 140instance Monoid (Stream Bool) where
141 mempty = Repeat False 141 mempty = Repeat False
142#if !MIN_VERSION_base(4,11,0)
142 mappend = sOr 143 mappend = sOr
144#else
145instance Semigroup (Stream Bool) where
146 (<>) = sOr
147#endif
143 148
144prop_StreamBool_monoid_left (a :: Stream Bool) = mempty <> a == a 149prop_StreamBool_monoid_left (a :: Stream Bool) = mempty <> a == a
145prop_StreamBool_monoid_right (a :: Stream Bool) = a <> mempty == a 150prop_StreamBool_monoid_right (a :: Stream Bool) = a <> mempty == a
diff --git a/src/LambdaCube/Compiler/DeBruijn.hs b/src/LambdaCube/Compiler/DeBruijn.hs
index a0fd8326..a3d1d0fd 100644
--- a/src/LambdaCube/Compiler/DeBruijn.hs
+++ b/src/LambdaCube/Compiler/DeBruijn.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE DeriveGeneric #-} 2{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE LambdaCase #-} 3{-# LANGUAGE LambdaCase #-}
3{-# LANGUAGE ViewPatterns #-} 4{-# LANGUAGE ViewPatterns #-}
@@ -76,7 +77,12 @@ instance PShow FreeVars where
76 77
77instance Monoid FreeVars where 78instance Monoid FreeVars where
78 mempty = FreeVars 0 79 mempty = FreeVars 0
80#if !MIN_VERSION_base(4,11,0)
79 FreeVars a `mappend` FreeVars b = FreeVars $ a .|. b 81 FreeVars a `mappend` FreeVars b = FreeVars $ a .|. b
82#else
83instance Semigroup FreeVars where
84 FreeVars a <> FreeVars b = FreeVars $ a .|. b
85#endif
80 86
81freeVar :: Int -> FreeVars 87freeVar :: Int -> FreeVars
82freeVar i = FreeVars $ 1 `shiftL` i 88freeVar i = FreeVars $ 1 `shiftL` i
diff --git a/src/LambdaCube/Compiler/DesugaredSource.hs b/src/LambdaCube/Compiler/DesugaredSource.hs
index 4a556c32..2e967eff 100644
--- a/src/LambdaCube/Compiler/DesugaredSource.hs
+++ b/src/LambdaCube/Compiler/DesugaredSource.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE DeriveGeneric #-} 2{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE LambdaCase #-} 3{-# LANGUAGE LambdaCase #-}
3{-# LANGUAGE ViewPatterns #-} 4{-# LANGUAGE ViewPatterns #-}
@@ -148,10 +149,18 @@ instance Ord SI where _ `compare` _ = EQ
148 149
149instance Monoid SI where 150instance Monoid SI where
150 mempty = NoSI Set.empty 151 mempty = NoSI Set.empty
152#if !MIN_VERSION_base(4,11,0)
151 mappend (RangeSI r1) (RangeSI r2) = RangeSI (joinRange r1 r2) 153 mappend (RangeSI r1) (RangeSI r2) = RangeSI (joinRange r1 r2)
152 mappend (NoSI ds1) (NoSI ds2) = NoSI (ds1 `Set.union` ds2) 154 mappend (NoSI ds1) (NoSI ds2) = NoSI (ds1 `Set.union` ds2)
153 mappend r@RangeSI{} _ = r 155 mappend r@RangeSI{} _ = r
154 mappend _ r@RangeSI{} = r 156 mappend _ r@RangeSI{} = r
157#else
158instance Semigroup SI where
159 (<>) (RangeSI r1) (RangeSI r2) = RangeSI (joinRange r1 r2)
160 (<>) (NoSI ds1) (NoSI ds2) = NoSI (ds1 `Set.union` ds2)
161 (<>) r@RangeSI{} _ = r
162 (<>) _ r@RangeSI{} = r
163#endif
155 164
156instance PShow SI where 165instance PShow SI where
157 pShow (NoSI ds) = hsep $ map text $ Set.toList ds 166 pShow (NoSI ds) = hsep $ map text $ Set.toList ds
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs
index 1ed8ac8c..70e7b98c 100644
--- a/src/LambdaCube/Compiler/Parser.hs
+++ b/src/LambdaCube/Compiler/Parser.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE LambdaCase #-} 2{-# LANGUAGE LambdaCase #-}
2{-# LANGUAGE ViewPatterns #-} 3{-# LANGUAGE ViewPatterns #-}
3{-# LANGUAGE PatternSynonyms #-} 4{-# LANGUAGE PatternSynonyms #-}
@@ -124,7 +125,12 @@ data DesugarInfo = DesugarInfo
124 125
125instance Monoid DesugarInfo where 126instance Monoid DesugarInfo where
126 mempty = DesugarInfo mempty mempty mempty 127 mempty = DesugarInfo mempty mempty mempty
128#if !MIN_VERSION_base(4,11,0)
127 DesugarInfo a b c `mappend` DesugarInfo a' b' c' = DesugarInfo (a <> a') (b <> b') (c <> c') 129 DesugarInfo a b c `mappend` DesugarInfo a' b' c' = DesugarInfo (a <> a') (b <> b') (c <> c')
130#else
131instance Semigroup DesugarInfo where
132 DesugarInfo a b c <> DesugarInfo a' b' c' = DesugarInfo (a <> a') (b <> b') (c <> c')
133#endif
128 134
129mkDesugarInfo :: [Stmt] -> DesugarInfo 135mkDesugarInfo :: [Stmt] -> DesugarInfo
130mkDesugarInfo ss = DesugarInfo 136mkDesugarInfo ss = DesugarInfo
diff --git a/src/LambdaCube/Compiler/Patterns.hs b/src/LambdaCube/Compiler/Patterns.hs
index a354ccf3..e2b18223 100644
--- a/src/LambdaCube/Compiler/Patterns.hs
+++ b/src/LambdaCube/Compiler/Patterns.hs
@@ -3,6 +3,7 @@
3-- overview: 3-- overview:
4-- https://rawgit.com/BP-HUG/presentations/master/2016_april/pattern-match-compilation/patternMatchComp.html 4-- https://rawgit.com/BP-HUG/presentations/master/2016_april/pattern-match-compilation/patternMatchComp.html
5 5
6{-# LANGUAGE CPP #-}
6{-# LANGUAGE LambdaCase #-} 7{-# LANGUAGE LambdaCase #-}
7{-# LANGUAGE ViewPatterns #-} 8{-# LANGUAGE ViewPatterns #-}
8{-# LANGUAGE PatternSynonyms #-} 9{-# LANGUAGE PatternSynonyms #-}
@@ -292,11 +293,20 @@ type GuardTrees = Lets GuardTree
292 293
293instance Monoid GuardTrees where 294instance Monoid GuardTrees where
294 mempty = In GTFailure 295 mempty = In GTFailure
296#if !MIN_VERSION_base(4,11,0)
295 LLet sn e x `mappend` y = LLet sn e $ x `mappend` rUp 1 0 y 297 LLet sn e x `mappend` y = LLet sn e $ x `mappend` rUp 1 0 y
296 LTypeAnn t x `mappend` y = LTypeAnn t $ x `mappend` y 298 LTypeAnn t x `mappend` y = LTypeAnn t $ x `mappend` y
297 In (GuardNode e n ps t ts) `mappend` y = In $ GuardNode e n ps t (ts `mappend` y) 299 In (GuardNode e n ps t ts) `mappend` y = In $ GuardNode e n ps t (ts `mappend` y)
298 In GTFailure `mappend` y = y 300 In GTFailure `mappend` y = y
299 x@(In GTSuccess{}) `mappend` _ = x 301 x@(In GTSuccess{}) `mappend` _ = x
302#else
303instance Semigroup GuardTrees where
304 LLet sn e x <> y = LLet sn e $ x <> rUp 1 0 y
305 LTypeAnn t x <> y = LTypeAnn t $ x <> y
306 In (GuardNode e n ps t ts) <> y = In $ GuardNode e n ps t (ts <> y)
307 In GTFailure <> y = y
308 x@(In GTSuccess{}) <> _ = x
309#endif
300 310
301noGuards :: SExp -> GuardTrees 311noGuards :: SExp -> GuardTrees
302noGuards = In . GTSuccess 312noGuards = In . GTSuccess
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs
index 24e540dd..2f333e85 100644
--- a/src/LambdaCube/Compiler/Pretty.hs
+++ b/src/LambdaCube/Compiler/Pretty.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE DeriveGeneric #-} 2{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE OverloadedStrings #-} 3{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE NoMonomorphismRestriction #-} 4{-# LANGUAGE NoMonomorphismRestriction #-}
@@ -101,7 +102,12 @@ pattern DText s = DAtom (SimpleAtom s)
101 102
102instance Monoid Doc where 103instance Monoid Doc where
103 mempty = text "" 104 mempty = text ""
105#if !MIN_VERSION_base(4,11,0)
104 mappend = dTwo mappend 106 mappend = dTwo mappend
107#else
108instance Semigroup Doc where
109 (<>) = dTwo (<>)
110#endif
105 111
106instance Show Doc where 112instance Show Doc where
107 show = ($ "") . P.displayS . P.renderPretty 0.4 200 . renderDoc 113 show = ($ "") . P.displayS . P.renderPretty 0.4 200 . renderDoc