@@ -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
@@ -76,7 +76,7 @@ import Data.Monoid
7676import Data.Function (on)
7777import GHC.Generics
7878
79−import Control.Newtype
79+import Control.Newtype.Generics
8080
8181import Data.Void (Void)
8282
@@ -315,6 +315,16 @@ if' _ e True = e
315315 BoolTrie f t
316316-}
317317
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)
318328
319329instance (HasTrie a, HasTrie b) => HasTrie (Either a b) where
320330 data (Either a b) :->: x = EitherTrie (a :->: x) (b :->: x)
@@ -577,7 +587,12 @@ instance HasTrie a => Monad ((:->:) a) where
577587
578588instance (HasTrie a, Monoid b) => Monoid (a :->: b) where
579589 mempty = trie mempty
590+#if !MIN_VERSION_base(4,11,0)
580591 mappend = inTrie2 mappend
592+#else
593+instance (HasTrie a, Semigroup b) => Semigroup (a :->: b) where
594+ (<>) = inTrie2 (<>)
595+#endif
581596
582597instance HasTrie a => Functor ((:->:) a) where
583598 fmap f = inTrie (fmap f)