on master help sign in
Merge pull request #8 from sboosali/master

Data.Void

commitd51b8634ca8434ef4ec398685ec68df7bbde3c34 authorConal Elliott <conal@conal.net> dateSat 21 Nov 2015, 10:44 (11 years ago) parent6938796b5aa3ae
3 files changed+68−25
MemoTrie.cabal+27−3 examples/Generic.hs+26 src/Data/MemoTrie.hs+15−22
MemoTrie.cabal+27−3
@@ -19,16 +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
29− Extensions:
30− Build-Depends: base < 5, void
35+
36+ if impl(ghc >= 7.10.0)
37+ Build-Depends: base >=4.8.0.0 && <5
38+ else
39+ Build-Depends: base <4.8.0.0, void
40+
3141 Exposed-Modules:
3242 Data.MemoTrie
43+ Other-Modules:
44+
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
3358
34− ghc-prof-options: -prof -auto-all
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+15−22
@@ -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,8 +70,8 @@ import Data.Monoid
6270import Data.Function (on)
6371import GHC.Generics
6472
65−import Data.Void
66−
73+import Data.Void (Void)
74+
6775-- import Prelude hiding (id,(.))
6876-- import Control.Category
6977-- import Control.Arrow
@@ -173,7 +181,7 @@ instance HasTrie Void where
173181 -- As suggested by Audun Skaugen
174182 data Void :->: a = VoidTrie
175183 trie _ = VoidTrie
176− untrie VoidTrie = absurd
184+ untrie VoidTrie = \case
177185 enumerate VoidTrie = []
178186
179187instance HasTrie () where
@@ -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−-}