@@ -149,43 +149,44 @@ import GHC.TypeLits
149149import Data.Singletons
150150import Data.String
151151import Prelude.Singletons ()
152+ import Data.Convertible
152153
153154{- |
154155`Opt`ional type with
155156either a `Def`ault promoted value @def@,
156157or `Some` specific `Demote`d value.
157158-}
158- data Opt (def :: k ) where
159- Def :: forall {k } def . SingDef def => Opt (def :: k )
160- Some :: forall {k } def . Demote k -> Opt (def :: k )
159+ data Opt (def :: k ) a where
160+ Def :: forall {k } def a . SingDef def a => Opt (def :: k ) a
161+ Some :: forall {k } def a . a -> Opt (def :: k ) a
161162
162163{- | Constraint required to `demote` @@def@. -}
163- type SingDef (def :: k ) = (SingI def , SingKind k )
164+ type SingDef (def :: k ) a = (SingI def , SingKind k , Convertible ( Demote k ) a )
164165
165- instance Semigroup (Opt (def :: k )) where
166+ instance Semigroup (Opt (def :: k ) a ) where
166167 Def <> opt = opt
167168 Some x <> _ = Some x
168169
169- instance SingDef def => Monoid (Opt def ) where
170+ instance SingDef def a => Monoid (Opt def a ) where
170171 mempty = Def
171172
172- deriving instance (SingDef def , Show ( Demote k ) )
173- => Show (Opt (def :: k ))
173+ deriving instance (SingDef def a , Show a )
174+ => Show (Opt (def :: k ) a )
174175
175- deriving instance (SingDef def , Read ( Demote k ) )
176- => Read (Opt (def :: k ))
176+ deriving instance (SingDef def a , Read a )
177+ => Read (Opt (def :: k ) a )
177178
178- deriving instance (SingDef def , Eq ( Demote k ) )
179- => Eq (Opt (def :: k ))
179+ deriving instance (SingDef def a , Eq a )
180+ => Eq (Opt (def :: k ) a )
180181
181- deriving instance (SingDef def , Ord ( Demote k ) )
182- => Ord (Opt (def :: k ))
182+ deriving instance (SingDef def a , Ord a )
183+ => Ord (Opt (def :: k ) a )
183184
184- instance SingDef def
185- => Default (Opt (def :: k )) where def = Def
185+ instance SingDef def a
186+ => Default (Opt (def :: k ) a ) where def = Def
186187
187- instance Num ( Demote k )
188- => Num (Opt (def :: k )) where
188+ instance Num a
189+ => Num (Opt (def :: k ) a ) where
189190 x + y = Some $ definite x + definite y
190191 x * y = Some $ definite x * definite y
191192 abs x = Some $ abs (definite x)
@@ -194,19 +195,19 @@ instance Num (Demote k)
194195 negate x = Some $ negate (definite x)
195196 x - y = Some $ definite x - definite y
196197
197- instance Fractional ( Demote k )
198- => Fractional (Opt (def :: k )) where
198+ instance Fractional a
199+ => Fractional (Opt (def :: k ) a ) where
199200 recip x = Some $ recip (definite x)
200201 x / y = Some $ definite x / definite y
201202 fromRational x = Some $ fromRational x
202203
203- instance IsString ( Demote k )
204- => IsString (Opt (def :: k )) where
204+ instance IsString a
205+ => IsString (Opt (def :: k ) a ) where
205206 fromString x = Some $ fromString x
206207
207- instance IsList ( Demote k )
208- => IsList (Opt (def :: k )) where
209- type Item (Opt (def :: k )) = Item ( Demote k )
208+ instance IsList a
209+ => IsList (Opt (def :: k ) a ) where
210+ type Item (Opt (def :: k ) a ) = Item a
210211 fromList xs = Some $ fromList xs
211212 fromListN n xs = Some $ fromListN n xs
212213 toList x = toList $ definite x
@@ -222,19 +223,19 @@ and `Just` maps to `Some`.
222223"bar"
223224-}
224225optionally
225- :: forall {k } def . SingDef def
226- => Maybe ( Demote k )
227- -> Opt (def :: k )
226+ :: forall {k } def a . SingDef def a
227+ => Maybe a
228+ -> Opt (def :: k ) a
228229optionally = maybe Def Some
229230
230231{- |
231232Deconstructs an `Opt` to a `Demote`d value.
232233`Def` maps to `demote` @@def@,
233234and `Some` maps to its argument.
234235-}
235- definite :: forall {k } def . Opt (def :: k ) -> Demote k
236+ definite :: forall {k } def a . Opt (def :: k ) a -> a
236237definite = \ case
237- Def -> demote @ def
238+ Def -> convert $ demote @ def
238239 Some a -> a
239240
240241{- |
@@ -244,8 +245,8 @@ and `Some` maps to `pure`,
244245inverting `optionally`.
245246-}
246247perhaps
247- :: forall {k } def m . Alternative m
248- => Opt (def :: k ) -> m ( Demote k )
248+ :: forall {k } def a m . Alternative m
249+ => Opt (def :: k ) a -> m a
249250perhaps = \ case
250251 Def -> empty
251252 Some a -> pure a
@@ -435,4 +436,4 @@ instance SingKind Q where
435436 toSing q = withSomeSing q SomeSing
436437
437438instance (SingI num , SingI denom ) => SingI (num :% denom ) where
438- sing = SRational sing sing
439+ sing = SRational sing sing
0 commit comments