on master help sign in
3 files changed+78−15
since splitsnapshots
unifiedsplit
MemoTrie.cabal+5−8 src/Data/MemoTrie.hs+66−7 stack.yaml+7
MemoTrie.cabal+5−8
@@ -1,5 +1,5 @@
11Name: MemoTrie
2−Version: 0.6.4
2+Version: 0.6.10
33Cabal-Version: >= 1.10
44Synopsis: Trie-based memo functions
55Category: Data
@@ -10,11 +10,11 @@ Description:
1010 .
1111 Project wiki page: <http://haskell.org/haskellwiki/MemoTrie>
1212 .
13− &#169; 2008-2015 by Conal Elliott; BSD3 license.
13+ &#199; 2008-2019 by Conal Elliott; BSD3 license.
1414Homepage: https://github.com/conal/MemoTrie
1515Author: Conal Elliott
1616Maintainer: conal@conal.net
17−Copyright: (c) 2008-2015 by Conal Elliott
17+Copyright: (c) 2008-2019 by Conal Elliott
1818License: BSD3
1919License-File: COPYING
2020Stability: experimental
@@ -33,16 +33,14 @@ Library
3333 hs-Source-Dirs: src
3434
3535 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
3737 else
38− Build-Depends: base <4.8.0.0, void
38+ Build-Depends: base <4.8.0.0, void, newtype-generics >= 0.5.3
3939
4040 Exposed-Modules:
4141 Data.MemoTrie
4242 Other-Modules:
4343
44− ghc-prof-options: -prof -auto-all
45−
4644 default-language: Haskell2010
4745
4846executable generic
@@ -50,7 +48,6 @@ executable generic
5048 buildable: False
5149 main-is: Generic.hs
5250 ghc-options: -Wall
53− default-language: Haskell2010
5451 hs-source-dirs: examples
5552 default-language: Haskell2010
5653 build-depends: base, MemoTrie
src/Data/MemoTrie.hs+66−7
@@ -9,7 +9,7 @@
99----------------------------------------------------------------------
1010-- |
1111-- Module : Data.MemoTrie
12−-- Copyright : (c) Conal Elliott 2008-2012
12+-- Copyright : (c) Conal Elliott 2008-2016
1313-- License : BSD3
1414--
1515-- Maintainer : conal@conal.net
@@ -42,7 +42,7 @@
4242-- see @examples/Generic.hs@, which can be run with:
4343--
4444-- @
45−-- $ cabal configure -fexamples && cabal run generic
45+-- cabal configure -fexamples && cabal run generic
4646-- @
4747--
4848--
@@ -55,22 +55,28 @@ module Data.MemoTrie
5555 , memo, memo2, memo3, mup
5656 , inTrie, inTrie2, inTrie3
5757 -- , untrieBits
58− , trieGeneric, untrieGeneric, enumerateGeneric, Reg
58+ , trieGeneric, untrieGeneric, enumerateGeneric, Reg
59+ , memoFix
5960 ) where
6061
6162-- Export the parts of HasTrie separately in order to get the associated data
6263-- type constructors, so I can define instances of other classes on them.
6364
65+import Data.Function (fix)
6466import Data.Bits
6567import Data.Word
6668import Data.Int
67−-- import Control.Applicative
69+#if !MIN_VERSION_base(4,8,0)
70+import Control.Applicative
71+#endif
6872import Control.Arrow (first,(&&&))
69−-- import Data.Monoid
73+#if !MIN_VERSION_base(4,8,0)
74+import Data.Monoid
75+#endif
7076import Data.Function (on)
7177import GHC.Generics
7278
73−import Control.Newtype
79+import Control.Newtype.Generics
7480
7581import Data.Void (Void)
7682
@@ -164,6 +170,42 @@ mup mem f = memo (mem . f)
164170memo2 = mup memo
165171memo3 = mup memo2
166172
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+
167209-- | Apply a unary function inside of a trie
168210inTrie :: (HasTrie a, HasTrie c) =>
169211 ((a -> b) -> (c -> d))
@@ -273,6 +315,16 @@ if' _ e True = e
273315 BoolTrie f t
274316-}
275317
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)
276328
277329instance (HasTrie a, HasTrie b) => HasTrie (Either a b) where
278330 data (Either a b) :->: x = EitherTrie (a :->: x) (b :->: x)
@@ -535,7 +587,12 @@ instance HasTrie a => Monad ((:->:) a) where
535587
536588instance (HasTrie a, Monoid b) => Monoid (a :->: b) where
537589 mempty = trie mempty
590+#if !MIN_VERSION_base(4,11,0)
538591 mappend = inTrie2 mappend
592+#else
593+instance (HasTrie a, Semigroup b) => Semigroup (a :->: b) where
594+ (<>) = inTrie2 (<>)
595+#endif
539596
540597instance HasTrie a => Functor ((:->:) a) where
541598 fmap f = inTrie (fmap f)
@@ -640,6 +697,7 @@ instance (HasTrie (f x)) => HasTrie (M1 i t f x) where
640697-- "unlifted" generic representation. (i.e. is a unary type constructor).
641698type Reg a = Rep a ()
642699
700+-- | 'Generic'-friendly default for 'trie'
643701trieGeneric :: (Generic a, HasTrie (Reg a))
644702 => ((Reg a :->: b) -> (a :->: b))
645703 -> (a -> b)
@@ -647,6 +705,7 @@ trieGeneric :: (Generic a, HasTrie (Reg a))
647705trieGeneric theConstructor f = theConstructor (trie (f . to))
648706{-# INLINEABLE trieGeneric #-}
649707
708+-- | 'Generic'-friendly default for 'untrie'
650709untrieGeneric :: (Generic a, HasTrie (Reg a))
651710 => ((a :->: b) -> (Reg a :->: b))
652711 -> (a :->: b)
@@ -654,6 +713,7 @@ untrieGeneric :: (Generic a, HasTrie (Reg a))
654713untrieGeneric theDestructor t = \a -> (untrie (theDestructor t)) (from a)
655714{-# INLINEABLE untrieGeneric #-}
656715
716+-- | 'Generic'-friendly default for 'enumerate'
657717enumerateGeneric :: (Generic a, HasTrie (Reg a))
658718 => ((a :->: b) -> (Reg a :->: b))
659719 -> (a :->: b)
@@ -678,4 +738,3 @@ dropSum s = case s of
678738liftSum :: Either (f a) (g a) -> (f :+: g) a
679739liftSum = either L1 R1
680740{-# 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