@@ -1,15 +1,13 @@
11{-# LANGUAGE GADTs, TypeFamilies, TypeOperators, ScopedTypeVariables, CPP #-}
2−{-# LANGUAGE StandaloneDeriving, FlexibleInstances #-}
3−{-# LANGUAGE DefaultSignatures, FlexibleContexts, LambdaCase #-}
2+{-# LANGUAGE StandaloneDeriving, FlexibleInstances #-}
43{-# OPTIONS_GHC -Wall -fenable-rewrite-rules #-}
5−
64-- ScopedTypeVariables works around a 6.10 bug. The forall keyword is
75-- supposed to be recognized in a RULES pragma.
86
97----------------------------------------------------------------------
108-- |
119-- Module : Data.MemoTrie
12−-- Copyright : (c) Conal Elliott 2008-2016
10+-- Copyright : (c) Conal Elliott 2008-2012
1311-- License : BSD3
1412--
1513-- Maintainer : conal@conal.net
@@ -17,35 +15,9 @@
1715--
1816-- Trie-based memoizer
1917--
20−-- Adapted from sjanssen's paste: <http://hpaste.org/3839 \"a lazy trie\">,
18+-- Adapted from sjanssen's paste: \"a lazy trie\" <http://hpaste.org/3839>,
2119-- which I think is based on Ralf Hinze's paper "Memo Functions,
2220-- Polytypically!".
23−--
24−-- You can automatically derive generic instances. for example:
25−--
26−-- @
27−-- {-# LANGUAGE <https://ocharles.org.uk/blog/posts/2014-12-16-derive-generic.html DeriveGeneric>, TypeOperators, TypeFamilies #-}
28−-- import Data.MemoTrie
29−-- import GHC.Generics (Generic)
30−--
31−-- data Color = RGB Int Int Int
32−-- | NamedColor String
33−-- deriving ('Generic')
34−--
35−-- instance HasTrie Color where
36−-- newtype (Color :->: b) = ColorTrie { unColorTrie :: 'Reg' Color :->: b }
37−-- trie = 'trieGeneric' ColorTrie
38−-- untrie = 'untrieGeneric' unColorTrie
39−-- enumerate = 'enumerateGeneric' unColorTrie
40−-- @
41−--
42−-- see @examples/Generic.hs@, which can be run with:
43−--
44−-- @
45−-- cabal configure -fexamples && cabal run generic
46−-- @
47−--
48−--
4921----------------------------------------------------------------------
5022
5123module Data.MemoTrie
@@ -55,31 +27,21 @@ module Data.MemoTrie
5527 , memo, memo2, memo3, mup
5628 , inTrie, inTrie2, inTrie3
5729 -- , untrieBits
58− , trieGeneric, untrieGeneric, enumerateGeneric, Reg
59− , memoFix
6030 ) where
6131
6232-- Export the parts of HasTrie separately in order to get the associated data
6333-- type constructors, so I can define instances of other classes on them.
6434
65−import Data.Function (fix)
6635import Data.Bits
6736import Data.Word
6837import Data.Int
69−#if !MIN_VERSION_base(4,8,0)
7038import Control.Applicative
71−#endif
7239import Control.Arrow (first,(&&&))
73−#if !MIN_VERSION_base(4,8,0)
7440import Data.Monoid
75−#endif
7641import Data.Function (on)
77−import GHC.Generics
7842
79−import Control.Newtype.Generics
43+import Data.Void
8044
81−import Data.Void (Void)
82−
8345-- import Prelude hiding (id,(.))
8446-- import Control.Category
8547-- import Control.Arrow
@@ -136,13 +98,9 @@ untrie3 tt = untrie2 . untrie tt
13698-}
13799
138100
139−-- {-# RULES "trie/untrie" forall t. trie (untrie t) = t #-}
140−
141−-- warning: [-Winline-rule-shadowing] …
142−-- Rule "trie/untrie" may never fire
143−-- because rule "Class op untrie" for ‘untrie’ might fire first
144−-- Probable fix: add phase [n] or [~n] to the competing rule
145−
101+{-# RULES
102+"trie/untrie" forall t. trie (untrie t) = t
103+ #-}
146104
147105-- Don't include the dual rule:
148106-- "untrie/trie" forall f. untrie (trie f) = f
@@ -170,42 +128,6 @@ mup mem f = memo (mem . f)
170128memo2 = mup memo
171129memo3 = mup memo2
172130
173−-- | Memoizing recursion. Use like `fix`.
174−memoFix :: HasTrie a => ((a -> b) -> (a -> b)) -> (a -> b)
175−memoFix h = fix (memo . h)
176−
177−#if 0
178−-- Equivalently,
179−
180−memoFix h = fix (\ f' -> memo (h f'))
181−
182−memoFix h = f'
183− where f' = memo (h f')
184−
185−memoFix h = f'
186− where
187− f' = memo f
188− f = h f'
189−#endif
190−
191−#if 0
192−-- Example
193−
194−fibF :: (Integer -> Integer) -> (Integer -> Integer)
195−fibF _ 0 = 1
196−fibF _ 1 = 1
197−fibF f n = f (n-1) + f (n-2)
198−
199−fib :: Integer -> Integer
200−fib = fix fibF
201−
202−fib' :: Integer -> Integer
203−fib' = memoFix fibF
204−
205−-- Try fib 30 vs fib' 30
206−#endif
207−
208−
209131-- | Apply a unary function inside of a trie
210132inTrie :: (HasTrie a, HasTrie c) =>
211133 ((a -> b) -> (c -> d))
@@ -229,27 +151,16 @@ inTrie3 = untrie ~> inTrie2
229151
230152instance HasTrie Void where
231153 -- As suggested by Audun Skaugen
232− data Void :->: a = VoidTrie
154+ data Void :->: a = VoidTrie
233155 trie _ = VoidTrie
234− untrie VoidTrie = \ _ -> error "untrie VoidTrie"
235− -- \case -- needs EmptyCase
156+ untrie VoidTrie = absurd
236157 enumerate VoidTrie = []
237158
238−instance Newtype (Void :->: a) where
239− type O (Void :->: a) = ()
240− pack () = VoidTrie
241− unpack VoidTrie = ()
242−
243159instance HasTrie () where
244− newtype () :->: a = UnitTrie a
245− trie f = UnitTrie (f ())
246− untrie (UnitTrie a) = \ () -> a
247− enumerate (UnitTrie a) = [((),a)]
248−
249−instance Newtype (() :->: a) where
250− type O (() :->: a) = a
251− pack a = UnitTrie a
252− unpack (UnitTrie a) = a
160+ newtype () :->: a = UnitTrie a
161+ trie f = UnitTrie (f ())
162+ untrie (UnitTrie a) = \ () -> a
163+ enumerate (UnitTrie a) = [((),a)]
253164
254165-- Proofs of inverse properties:
255166
@@ -281,15 +192,10 @@ memoizing hyper-strict functions.
281192
282193
283194instance HasTrie Bool where
284− data Bool :->: x = BoolTrie x x
285− trie f = BoolTrie (f False) (f True)
286− untrie (BoolTrie f t) = if' f t
287− enumerate (BoolTrie f t) = [(False,f),(True,t)]
288−
289−instance Newtype (Bool :->: a) where
290− type O (Bool :->: a) = (a,a)
291− pack (a,a') = BoolTrie a a'
292− unpack (BoolTrie a a') = (a,a')
195+ data Bool :->: x = BoolTrie x x
196+ trie f = BoolTrie (f False) (f True)
197+ untrie (BoolTrie f t) = if' f t
198+ enumerate (BoolTrie f t) = [(False,f),(True,t)]
293199
294200-- | Conditional with boolean last.
295201-- Spec: @if' (f False) (f True) == f@
@@ -315,27 +221,12 @@ if' _ e True = e
315221 BoolTrie f t
316222-}
317223
318−instance HasTrie a => HasTrie (Maybe a) where
319− data (:->:) (Maybe a) b = MaybeTrie b (a :->: b)
320− trie f = MaybeTrie (f Nothing) (trie (f . Just))
321− untrie (MaybeTrie nothing_val a_trie) = maybe nothing_val (untrie a_trie)
322− enumerate (MaybeTrie nothing_val a_trie) = (Nothing, nothing_val) : enum' Just a_trie
323−
324−instance Newtype (Maybe a :->: x) where
325− type O (Maybe a :->: x) = (x, a :->: x)
326− pack (a,f) = MaybeTrie a f
327− unpack (MaybeTrie a f) = (a,f)
328224
329225instance (HasTrie a, HasTrie b) => HasTrie (Either a b) where
330− data (Either a b) :->: x = EitherTrie (a :->: x) (b :->: x)
331− trie f = EitherTrie (trie (f . Left)) (trie (f . Right))
332− untrie (EitherTrie s t) = either (untrie s) (untrie t)
333− enumerate (EitherTrie s t) = enum' Left s `weave` enum' Right t
334−
335−instance Newtype (Either a b :->: x) where
336− type O (Either a b :->: x) = (a :->: x, b :->: x)
337− pack (f,g) = EitherTrie f g
338− unpack (EitherTrie f g) = (f,g)
226+ data (Either a b) :->: x = EitherTrie (a :->: x) (b :->: x)
227+ trie f = EitherTrie (trie (f . Left)) (trie (f . Right))
228+ untrie (EitherTrie s t) = either (untrie s) (untrie t)
229+ enumerate (EitherTrie s t) = enum' Left s `weave` enum' Right t
339230
340231enum' :: (HasTrie a) => (a -> a') -> (a :->: b) -> [(a', b)]
341232enum' f = (fmap.first) f . enumerate
@@ -370,16 +261,11 @@ as `weave` [] = as
370261
371262
372263instance (HasTrie a, HasTrie b) => HasTrie (a,b) where
373− newtype (a,b) :->: x = PairTrie (a :->: (b :->: x))
374− trie f = PairTrie (trie (trie . curry f))
375− untrie (PairTrie t) = uncurry (untrie . untrie t)
376− enumerate (PairTrie tt) =
377− [ ((a,b),x) | (a,t) <- enumerate tt , (b,x) <- enumerate t ]
378−
379−instance Newtype ((a,b) :->: x) where
380− type O ((a,b) :->: x) = a :->: b :->: x
381− pack abx = PairTrie abx
382− unpack (PairTrie abx) = abx
264+ newtype (a,b) :->: x = PairTrie (a :->: (b :->: x))
265+ trie f = PairTrie (trie (trie . curry f))
266+ untrie (PairTrie t) = uncurry (untrie . untrie t)
267+ enumerate (PairTrie tt) =
268+ [ ((a,b),x) | (a,t) <- enumerate tt , (b,x) <- enumerate t ]
383269
384270{-
385271 untrie (trie f)
@@ -408,10 +294,10 @@ instance Newtype ((a,b) :->: x) where
408294-}
409295
410296instance (HasTrie a, HasTrie b, HasTrie c) => HasTrie (a,b,c) where
411− newtype (a,b,c) :->: x = TripleTrie (((a,b),c) :->: x)
412− trie f = TripleTrie (trie (f . trip))
413− untrie (TripleTrie t) = untrie t . detrip
414− enumerate (TripleTrie t) = enum' trip t
297+ newtype (a,b,c) :->: x = TripleTrie (((a,b),c) :->: x)
298+ trie f = TripleTrie (trie (f . trip))
299+ untrie (TripleTrie t) = untrie t . detrip
300+ enumerate (TripleTrie t) = enum' trip t
415301
416302trip :: ((a,b),c) -> (a,b,c)
417303trip ((a,b),c) = (a,b,c)
@@ -421,10 +307,10 @@ detrip (a,b,c) = ((a,b),c)
421307
422308
423309instance HasTrie x => HasTrie [x] where
424− newtype [x] :->: a = ListTrie (Either () (x,[x]) :->: a)
425− trie f = ListTrie (trie (f . list))
426− untrie (ListTrie t) = untrie t . delist
427− enumerate (ListTrie t) = enum' list t
310+ newtype [x] :->: a = ListTrie (Either () (x,[x]) :->: a)
311+ trie f = ListTrie (trie (f . list))
312+ untrie (ListTrie t) = untrie t . delist
313+ enumerate (ListTrie t) = enum' list t
428314
429315list :: Either () (x,[x]) -> [x]
430316list = either (const []) (uncurry (:))
@@ -435,10 +321,10 @@ delist (x:xs) = Right (x,xs)
435321
436322#define WordInstance(Type,TrieType)\
437323instance HasTrie Type where \
438− newtype Type :->: a = TrieType ([Bool] :->: a);\
439− trie f = TrieType (trie (f . unbits));\
440− untrie (TrieType t) = untrie t . bits;\
441− enumerate (TrieType t) = enum' unbits t
324+ newtype Type :->: a = TrieType ([Bool] :->: a);\
325+ trie f = TrieType (trie (f . unbits));\
326+ untrie (TrieType t) = untrie t . bits;\
327+ enumerate (TrieType t) = enum' unbits t
442328
443329WordInstance(Word,WordTrie)
444330WordInstance(Word8,Word8Trie)
@@ -447,10 +333,10 @@ WordInstance(Word32,Word32Trie)
447333WordInstance(Word64,Word64Trie)
448334
449335-- instance HasTrie Word where
450−-- newtype Word :->: a = WordTrie ([Bool] :->: a)
451−-- trie f = WordTrie (trie (f . unbits))
452−-- untrie (WordTrie t) = untrie t . bits
453−-- enumerate (WordTrie t) = enum' unbits t
336+-- newtype Word :->: a = WordTrie ([Bool] :->: a)
337+-- trie f = WordTrie (trie (f . unbits))
338+-- untrie (WordTrie t) = untrie t . bits
339+-- enumerate (WordTrie t) = enum' unbits t
454340
455341
456342-- | Extract bits in little-endian order
@@ -469,10 +355,10 @@ unbits [] = 0
469355unbits (x:xs) = unbit x .|. shiftL (unbits xs) 1
470356
471357instance HasTrie Char where
472− newtype Char :->: a = CharTrie (Int :->: a)
473− untrie (CharTrie t) n = untrie t (fromEnum n)
474− trie f = CharTrie (trie (f . toEnum))
475− enumerate (CharTrie t) = enum' toEnum t
358+ newtype Char :->: a = CharTrie (Int :->: a)
359+ untrie (CharTrie t) n = untrie t (fromEnum n)
360+ trie f = CharTrie (trie (f . toEnum))
361+ enumerate (CharTrie t) = enum' toEnum t
476362
477363-- Although Int is a Bits instance, we can't use bits directly for
478364-- memoizing, because the "bits" function gives an infinite result, since
@@ -481,10 +367,10 @@ instance HasTrie Char where
481367
482368#define IntInstance(IntType,WordType,TrieType) \
483369instance HasTrie IntType where \
484− newtype IntType :->: a = TrieType (WordType :->: a); \
485− untrie (TrieType t) n = untrie t (fromIntegral n); \
486− trie f = TrieType (trie (f . fromIntegral)); \
487− enumerate (TrieType t) = enum' fromIntegral t
370+ newtype IntType :->: a = TrieType (WordType :->: a); \
371+ untrie (TrieType t) n = untrie t (fromIntegral n); \
372+ trie f = TrieType (trie (f . fromIntegral)); \
373+ enumerate (TrieType t) = enum' fromIntegral t
488374
489375IntInstance(Int,Word,IntTrie)
490376IntInstance(Int8,Word8,Int8Trie)
@@ -496,10 +382,10 @@ IntInstance(Int64,Word64,Int64Trie)
496382-- extract the sign bit.
497383
498384instance HasTrie Integer where
499− newtype Integer :->: a = IntegerTrie ((Bool,[Bool]) :->: a)
500− trie f = IntegerTrie (trie (f . unbitsZ))
501− untrie (IntegerTrie t) = untrie t . bitsZ
502− enumerate (IntegerTrie t) = enum' unbitsZ t
385+ newtype Integer :->: a = IntegerTrie ((Bool,[Bool]) :->: a)
386+ trie f = IntegerTrie (trie (f . unbitsZ))
387+ untrie (IntegerTrie t) = untrie t . bitsZ
388+ enumerate (IntegerTrie t) = enum' unbitsZ t
503389
504390
505391unbitsZ :: (Num n, Bits n) => (Bool,[Bool]) -> n
@@ -587,12 +473,7 @@ instance HasTrie a => Monad ((:->:) a) where
587473
588474instance (HasTrie a, Monoid b) => Monoid (a :->: b) where
589475 mempty = trie mempty
590−#if !MIN_VERSION_base(4,11,0)
591476 mappend = inTrie2 mappend
592−#else
593−instance (HasTrie a, Semigroup b) => Semigroup (a :->: b) where
594− (<>) = inTrie2 (<>)
595−#endif
596477
597478instance HasTrie a => Functor ((:->:) a) where
598479 fmap f = inTrie (fmap f)
@@ -616,6 +497,7 @@ infixr 9 @.@
616497(@.@) = inTrie2 (.)
617498
618499
500+
619501-- instance Category (:->:) where
620502-- id = idTrie
621503-- (.) = (.:)
@@ -634,6 +516,7 @@ necessary but disallowed `HasTrie` constraints on the domain type.
634516
635517-}
636518
519+
637520---- To go elsewhere
638521
639522-- Matt Hellige's notation for @argument f . result g@.
@@ -649,92 +532,3 @@ f1 n = n + n
649532
650533f1' = memo f1
651534-}
652−
653−-- | just like @void@
654−instance HasTrie (V1 x) where
655− data (V1 x :->: b) = V1Trie
656− trie _ = V1Trie
657− untrie V1Trie = \ _ -> error "untrie V1Trie"
658− -- \case -- needs EmptyCase
659− enumerate V1Trie = []
660−
661−-- | just like @()@
662−instance HasTrie (U1 x) where
663− data (U1 x :->: b) = U1Trie b
664− trie f = U1Trie (f U1)
665− untrie (U1Trie b) = \U1 -> b
666− enumerate (U1Trie b) = [(U1, b)]
667−
668−-- | wraps @Either (f x) (g x)@
669−instance (HasTrie (f x), HasTrie (g x)) => HasTrie ((f :+: g) x) where
670− newtype ((f :+: g) x :->: b) = EitherTrie1 (Either (f x) (g x) :->: b)
671− trie f = EitherTrie1 (trie (f . liftSum))
672− untrie (EitherTrie1 t) = (untrie t) . dropSum
673− enumerate (EitherTrie1 t) = enum' liftSum t
674−
675−-- | wraps @(f x, g x)@
676−instance (HasTrie (f x), HasTrie (g x)) => HasTrie ((f :*: g) x) where
677− newtype ((f :*: g) x :->: b) = PairTrie1 ((f x, g x) :->: b)
678− trie f = PairTrie1 (trie (f . liftProduct))
679− untrie (PairTrie1 t) = (untrie t) . dropProduct
680− enumerate (PairTrie1 t) = enum' liftProduct t
681−
682−-- | wraps @a@
683−instance (HasTrie a) => HasTrie (K1 i a x) where
684− data (K1 i a x :->: b) = K1Trie (a :->: b)
685− trie f = K1Trie (trie (f . K1))
686− untrie (K1Trie t) = \(K1 a) -> (untrie t) a
687− enumerate (K1Trie t) = enum' K1 t
688−
689−-- | wraps @f x@
690−instance (HasTrie (f x)) => HasTrie (M1 i t f x) where
691− data (M1 i t f x :->: b) = M1Trie (f x :->: b)
692− trie f = M1Trie (trie (f . M1))
693− untrie (M1Trie t) = \(M1 a) -> (untrie t) a
694− enumerate (M1Trie t) = enum' M1 t
695−
696−-- | the data type in a __reg__ular form.
697−-- "unlifted" generic representation. (i.e. is a unary type constructor).
698−type Reg a = Rep a ()
699−
700−-- | 'Generic'-friendly default for 'trie'
701−trieGeneric :: (Generic a, HasTrie (Reg a))
702− => ((Reg a :->: b) -> (a :->: b))
703− -> (a -> b)
704− -> (a :->: b)
705−trieGeneric theConstructor f = theConstructor (trie (f . to))
706−{-# INLINEABLE trieGeneric #-}
707−
708−-- | 'Generic'-friendly default for 'untrie'
709−untrieGeneric :: (Generic a, HasTrie (Reg a))
710− => ((a :->: b) -> (Reg a :->: b))
711− -> (a :->: b)
712− -> (a -> b)
713−untrieGeneric theDestructor t = \a -> (untrie (theDestructor t)) (from a)
714−{-# INLINEABLE untrieGeneric #-}
715−
716−-- | 'Generic'-friendly default for 'enumerate'
717−enumerateGeneric :: (Generic a, HasTrie (Reg a))
718− => ((a :->: b) -> (Reg a :->: b))
719− -> (a :->: b)
720− -> [(a, b)]
721−enumerateGeneric theDestructor t = enum' to (theDestructor t)
722−{-# INLINEABLE enumerateGeneric #-}
723−
724−dropProduct :: (f :*: g) a -> (f a, g a)
725−dropProduct (a :*: b) = (a, b)
726−{-# INLINEABLE dropProduct #-}
727−
728−liftProduct :: (f a, g a) -> (f :*: g) a
729−liftProduct (a, b) = (a :*: b)
730−{-# INLINEABLE liftProduct #-}
731−
732−dropSum :: (f :+: g) a -> Either (f a) (g a)
733−dropSum s = case s of
734− L1 x -> Left x
735− R1 x -> Right x
736−{-# INLINEABLE dropSum #-}
737−
738−liftSum :: Either (f a) (g a) -> (f :+: g) a
739−liftSum = either L1 R1
740−{-# INLINEABLE liftSum #-}