on master help sign in
MemoTrie.cabal+5−8
since splitsnapshots
unifiedsplit
@@ -1,5 +1,5 @@
1Name: MemoTrie
1Name: MemoTrie
2−Version: 0.6.4
2+Version: 0.6.10
3Cabal-Version: >= 1.10
3Cabal-Version: >= 1.10
4Synopsis: Trie-based memo functions
4Synopsis: Trie-based memo functions
5Category: Data
5Category: Data
@@ -10,11 +10,11 @@ Description:
10 .
10 .
11 Project wiki page: <http://haskell.org/haskellwiki/MemoTrie>
11 Project wiki page: <http://haskell.org/haskellwiki/MemoTrie>
12 .
12 .
13− &#169; 2008-2015 by Conal Elliott; BSD3 license.
13+ &#199; 2008-2019 by Conal Elliott; BSD3 license.
14Homepage: https://github.com/conal/MemoTrie
14Homepage: https://github.com/conal/MemoTrie
15Author: Conal Elliott
15Author: Conal Elliott
16Maintainer: conal@conal.net
16Maintainer: conal@conal.net
17−Copyright: (c) 2008-2015 by Conal Elliott
17+Copyright: (c) 2008-2019 by Conal Elliott
18License: BSD3
18License: BSD3
19License-File: COPYING
19License-File: COPYING
20Stability: experimental
20Stability: experimental
@@ -33,16 +33,14 @@ Library
33 hs-Source-Dirs: src
33 hs-Source-Dirs: src
34
34
35 if impl(ghc >= 7.10.0)
35 if impl(ghc >= 7.10.0)
36− Build-Depends: base >=4.8.0.0 && <5
36+ Build-Depends: base >=4.8.0.0 && <5, newtype-generics >= 0.5.3
37 else
37 else
38− Build-Depends: base <4.8.0.0, void
38+ Build-Depends: base <4.8.0.0, void, newtype-generics >= 0.5.3
39
39
40 Exposed-Modules:
40 Exposed-Modules:
41 Data.MemoTrie
41 Data.MemoTrie
42 Other-Modules:
42 Other-Modules:
43
43
44− ghc-prof-options: -prof -auto-all
45−
46 default-language: Haskell2010
44 default-language: Haskell2010
47
45
48executable generic
46executable generic
@@ -50,7 +48,6 @@ executable generic
50 buildable: False
48 buildable: False
51 main-is: Generic.hs
49 main-is: Generic.hs
52 ghc-options: -Wall
50 ghc-options: -Wall
53− default-language: Haskell2010
54 hs-source-dirs: examples
51 hs-source-dirs: examples
55 default-language: Haskell2010
52 default-language: Haskell2010
56 build-depends: base, MemoTrie
53 build-depends: base, MemoTrie
src/Data/MemoTrie.hs+66−7
@@ -9,7 +9,7 @@
9----------------------------------------------------------------------
9----------------------------------------------------------------------
10-- |
10-- |
11-- Module : Data.MemoTrie
11-- Module : Data.MemoTrie
12−-- Copyright : (c) Conal Elliott 2008-2012
12+-- Copyright : (c) Conal Elliott 2008-2016
13-- License : BSD3
13-- License : BSD3
14--
14--
15-- Maintainer : conal@conal.net
15-- Maintainer : conal@conal.net
@@ -42,7 +42,7 @@
42-- see @examples/Generic.hs@, which can be run with:
42-- see @examples/Generic.hs@, which can be run with:
43--
43--
44-- @
44-- @
45−-- $ cabal configure -fexamples && cabal run generic
45+-- cabal configure -fexamples && cabal run generic
46-- @
46-- @
47--
47--
48--
48--
@@ -55,22 +55,28 @@ module Data.MemoTrie
55 , memo, memo2, memo3, mup
55 , memo, memo2, memo3, mup
56 , inTrie, inTrie2, inTrie3
56 , inTrie, inTrie2, inTrie3
57 -- , untrieBits
57 -- , untrieBits
58− , trieGeneric, untrieGeneric, enumerateGeneric, Reg
58+ , trieGeneric, untrieGeneric, enumerateGeneric, Reg
59+ , memoFix
59 ) where
60 ) where
60
61
61-- Export the parts of HasTrie separately in order to get the associated data
62-- Export the parts of HasTrie separately in order to get the associated data
62-- type constructors, so I can define instances of other classes on them.
63-- type constructors, so I can define instances of other classes on them.
63
64
65+import Data.Function (fix)
64import Data.Bits
66import Data.Bits
65import Data.Word
67import Data.Word
66import Data.Int
68import Data.Int
69+#if !MIN_VERSION_base(4,8,0)
67−-- import Control.Applicative
70+import Control.Applicative
71+#endif
68import Control.Arrow (first,(&&&))
72import Control.Arrow (first,(&&&))
73+#if !MIN_VERSION_base(4,8,0)
69−-- import Data.Monoid
74+import Data.Monoid
75+#endif
70import Data.Function (on)
76import Data.Function (on)
71import GHC.Generics
77import GHC.Generics
72
78
73−import Control.Newtype
79+import Control.Newtype.Generics
74
80
75import Data.Void (Void)
81import Data.Void (Void)
76
82
@@ -164,6 +170,42 @@ mup mem f = memo (mem . f)
164memo2 = mup memo
170memo2 = mup memo
165memo3 = mup memo2
171memo3 = mup memo2
166
172
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+
167-- | Apply a unary function inside of a trie
209-- | Apply a unary function inside of a trie
168inTrie :: (HasTrie a, HasTrie c) =>
210inTrie :: (HasTrie a, HasTrie c) =>
169 ((a -> b) -> (c -> d))
211 ((a -> b) -> (c -> d))
@@ -273,6 +315,16 @@ if' _ e True = e
273 BoolTrie f t
315 BoolTrie f t
274-}
316-}
275
317
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)
276
328
277instance (HasTrie a, HasTrie b) => HasTrie (Either a b) where
329instance (HasTrie a, HasTrie b) => HasTrie (Either a b) where
278 data (Either a b) :->: x = EitherTrie (a :->: x) (b :->: x)
330 data (Either a b) :->: x = EitherTrie (a :->: x) (b :->: x)
@@ -535,7 +587,12 @@ instance HasTrie a => Monad ((:->:) a) where
535
587
536instance (HasTrie a, Monoid b) => Monoid (a :->: b) where
588instance (HasTrie a, Monoid b) => Monoid (a :->: b) where
537 mempty = trie mempty
589 mempty = trie mempty
590+#if !MIN_VERSION_base(4,11,0)
538 mappend = inTrie2 mappend
591 mappend = inTrie2 mappend
592+#else
593+instance (HasTrie a, Semigroup b) => Semigroup (a :->: b) where
594+ (<>) = inTrie2 (<>)
595+#endif
539
596
540instance HasTrie a => Functor ((:->:) a) where
597instance HasTrie a => Functor ((:->:) a) where
541 fmap f = inTrie (fmap f)
598 fmap f = inTrie (fmap f)
@@ -640,6 +697,7 @@ instance (HasTrie (f x)) => HasTrie (M1 i t f x) where
640-- "unlifted" generic representation. (i.e. is a unary type constructor).
697-- "unlifted" generic representation. (i.e. is a unary type constructor).
641type Reg a = Rep a ()
698type Reg a = Rep a ()
642
699
700+-- | 'Generic'-friendly default for 'trie'
643trieGeneric :: (Generic a, HasTrie (Reg a))
701trieGeneric :: (Generic a, HasTrie (Reg a))
644 => ((Reg a :->: b) -> (a :->: b))
702 => ((Reg a :->: b) -> (a :->: b))
645 -> (a -> b)
703 -> (a -> b)
@@ -647,6 +705,7 @@ trieGeneric :: (Generic a, HasTrie (Reg a))
647trieGeneric theConstructor f = theConstructor (trie (f . to))
705trieGeneric theConstructor f = theConstructor (trie (f . to))
648{-# INLINEABLE trieGeneric #-}
706{-# INLINEABLE trieGeneric #-}
649
707
708+-- | 'Generic'-friendly default for 'untrie'
650untrieGeneric :: (Generic a, HasTrie (Reg a))
709untrieGeneric :: (Generic a, HasTrie (Reg a))
651 => ((a :->: b) -> (Reg a :->: b))
710 => ((a :->: b) -> (Reg a :->: b))
652 -> (a :->: b)
711 -> (a :->: b)
@@ -654,6 +713,7 @@ untrieGeneric :: (Generic a, HasTrie (Reg a))
654untrieGeneric theDestructor t = \a -> (untrie (theDestructor t)) (from a)
713untrieGeneric theDestructor t = \a -> (untrie (theDestructor t)) (from a)
655{-# INLINEABLE untrieGeneric #-}
714{-# INLINEABLE untrieGeneric #-}
656
715
716+-- | 'Generic'-friendly default for 'enumerate'
657enumerateGeneric :: (Generic a, HasTrie (Reg a))
717enumerateGeneric :: (Generic a, HasTrie (Reg a))
658 => ((a :->: b) -> (Reg a :->: b))
718 => ((a :->: b) -> (Reg a :->: b))
659 -> (a :->: b)
719 -> (a :->: b)
@@ -678,4 +738,3 @@ dropSum s = case s of
678liftSum :: Either (f a) (g a) -> (f :+: g) a
738liftSum :: Either (f a) (g a) -> (f :+: g) a
679liftSum = either L1 R1
739liftSum = either L1 R1
680{-# INLINEABLE liftSum #-}
740{-# INLINEABLE liftSum #-}
681−
stack.yamlnew file+7
@@ -0,0 +1,7 @@
1+# resolver: lts-9.21 # ghc-8.0.2
2+# resolver: lts-11.22 # ghc-8.2.2
3+# resolver: lts-12.14 # ghc-8.4.3
4+# resolver: lts-12.19 # ghc-8.4.4
5+# resolver: lts-13.0 # ghc-8.6.3
6+# resolver: lts-13.13 # ghc-8.6.4
7+resolver: lts-14.6 # ghc-8.6.5