on master help sign in
example executable, simpler conditional dependency
commitb5aa3aeb903b7bfe132bb4dcde33f4b7f71c9367 authorSam Boosalis <samboosalis@gmail.com> dateThu 19 Nov 2015, 14:21 (11 years ago) parent28c6471
4 files changed+61−35
MemoTrie.cabal+22−4 examples/Generic.hs+26 src/Data/MemoTrie.hs+13−20 src/Data/MemoTrie/Extra.hs−11
MemoTrie.cabal+22−4
@@ -19,22 +19,40 @@ License: BSD3
1919License-File: COPYING
2020Stability: experimental
2121build-type: Simple
22+cabal-version: >= 1.10
2223
2324source-repository head
2425 type: git
2526 location: git://github.com/conal/MemoTrie.git
2627
28+Flag examples
29+ Description: "examples"
30+ Default: False
31+ Manual: True
32+
2733Library
2834 hs-Source-Dirs: src
2935
3036 if impl(ghc >= 7.10.0)
31− Build-Depends: base < 5
37+ Build-Depends: base >=4.8.0.0 && <5
3238 else
33− Build-Depends: base < 5, void
39+ Build-Depends: base <4.8.0.0, void
3440
3541 Exposed-Modules:
3642 Data.MemoTrie
3743 Other-Modules:
38− Data.MemoTrie.Extra
3944
40− ghc-prof-options: -prof -auto-all
45+ ghc-prof-options: -prof -auto-all
46+
47+ default-language: Haskell2010
48+
49+executable generic
50+ if !flag(examples)
51+ buildable: False
52+ main-is: Generic.hs
53+ ghc-options: -Wall
54+ default-language: Haskell2010
55+ hs-source-dirs: examples
56+ default-language: Haskell2010
57+ build-depends: base, MemoTrie
58+
examples/Generic.hsnew file+26
@@ -0,0 +1,26 @@
1+{-# LANGUAGE DeriveGeneric, TypeOperators, TypeFamilies #-}
2+import Data.MemoTrie
3+import GHC.Generics (Generic)
4+
5+data Color
6+ = RGB Int Int Int
7+ | Color String
8+ deriving (Generic)
9+
10+instance HasTrie Color where
11+ newtype (Color :->: b) = ColorTrie { unColorTrie :: Reg Color :->: b }
12+ trie = trieGeneric ColorTrie
13+ untrie = untrieGeneric unColorTrie
14+ enumerate = enumerateGeneric unColorTrie
15+
16+runColor (RGB r g b) = r + g + b
17+runColor (Color s) = length [1..10e7]
18+
19+runColorMemoized = memo runColor
20+
21+main = do
22+ putStrLn "first call (should take a few seconds): "
23+ print$ runColorMemoized (Color "")
24+ putStrLn "cached call (should be instantaneous): "
25+ print$ runColorMemoized (Color "")
26+
src/Data/MemoTrie.hs+13−20
@@ -23,12 +23,14 @@
2323-- You can automatically derive generic instances. for example:
2424--
2525-- @
26−-- -- LANGUAGE DeriveGeneric, TypeOperators
26+-- -- LANGUAGE <https://ocharles.org.uk/blog/posts/2014-12-16-derive-generic.html DeriveGeneric>, TypeOperators, TypeFamilies #-}
27+-- import Data.MemoTrie
28+-- import GHC.Generics (Generic)
2729--
2830-- data Color
2931-- = RGB Int Int Int
3032-- | Color String
31−-- deriving (Generic)
33+-- deriving ('Generic')
3234--
3335-- instance HasTrie Color where
3436-- newtype (Color :->: b) = ColorTrie { unColorTrie :: 'Reg' Color :->: b }
@@ -37,6 +39,12 @@
3739-- enumerate = 'enumerateGeneric' unColorTrie
3840-- @
3941--
42+-- see @examples/Generic.hs@, which can be run with:
43+--
44+-- @
45+-- $ cabal configure -fexamples && cabal run generic
46+-- @
47+--
4048--
4149----------------------------------------------------------------------
4250
@@ -62,7 +70,7 @@ import Data.Monoid
6270import Data.Function (on)
6371import GHC.Generics
6472
65−import Data.MemoTrie.Extra (Void)
73+import Data.Void (Void)
6674
6775-- import Prelude hiding (id,(.))
6876-- import Control.Category
@@ -595,7 +603,8 @@ instance (HasTrie (f x)) => HasTrie (M1 i t f x) where
595603 untrie (M1Trie t) = \(M1 a) -> (untrie t) a
596604 enumerate (M1Trie t) = enum' M1 t
597605
598−-- | "unlifted" generic representation. (i.e. is a unary type constructor).
606+-- | the data type in a __reg__ular form.
607+-- "unlifted" generic representation. (i.e. is a unary type constructor).
599608type Reg a = Rep a ()
600609
601610trieGeneric
@@ -640,19 +649,3 @@ liftSum :: Either (f a) (g a) -> (f :+: g) a
640649liftSum = either L1 R1
641650{-# INLINEABLE liftSum #-}
642651
643−{- example:
644−
645−{-# LANGUAGE DeriveGeneric, TypeOperators #-}
646−
647−data Color
648− = RGB Int Int Int
649− | Color String
650− deriving (Generic)
651−
652−instance HasTrie Color where
653− newtype (Color :->: b) = ColorTrie { unColorTrie :: Reg Color :->: b }
654− trie = trieGeneric ColorTrie
655− untrie = untrieGeneric unColorTrie
656− enumerate = enumerateGeneric unColorTrie
657−
658−-}
src/Data/MemoTrie/Extra.hsdeleted−11
@@ -1,11 +0,0 @@
1−{-# LANGUAGE CPP, PackageImports #-}
2−module Data.MemoTrie.Extra
3− ( Void
4− ) where
5−
6−#if MIN_VERSION_base(4,8,0)
7−import "base" Data.Void
8−#else
9−import "void" Data.Void
10−#endif
11−