lude

Haskell prelude alternative
git clone git://git.rr3.xyz/lude | git clone gits://git.rr3.xyz/lude
Log | Files | Refs | README | LICENSE

Maybe.hs (2273B)


      1 module Lude.Maybe (
      2     Maybe (..),
      3     isJust,
      4     isNothing,
      5     maybe,
      6     (??),
      7     unsafe_fromJust,
      8     --
      9     Maybe#,
     10     nothing#,
     11     just#,
     12     maybe#,
     13     isNothing#,
     14     isJust#,
     15     (??#),
     16 ) where
     17 
     18 import Data.Maybe (Maybe (..), isJust, isNothing)
     19 import Data.Maybe qualified as Base
     20 
     21 import Lude.Bool
     22 import Lude.Function
     23 import Lude.Haskell
     24 
     25 -- TODO: Versions for other reprs? Look at Todo/Lude.hs.
     26 
     27 -- TODO: Defaulting operators
     28 
     29 -- TODO: Using a `unBox# -> x` view pattern in `maybe#` triggers a GHC 9.14.1 bug. Report it.
     30 {-
     31 <no location info>: error:
     32     panic! (the 'impossible' happened)
     33   GHC version 9.14.1:
     34 	typeLevity
     35   a_a1Ri
     36   Call stack:
     37       CallStack (from HasCallStack):
     38         pprPanic, called at compiler/GHC/Core/Type.hs:2353:32 in ghc-9.14.1-ade7:GHC.Core.Type
     39 
     40 Please report this as a GHC bug:  https://www.haskell.org/ghc/reportabug
     41 -}
     42 
     43 --------------------------------------------------------------------------------
     44 
     45 maybe :: forall (a :: Type) {s :: Repr} (b :: Type# s). Lev b -> (a -> b) -> Maybe a -> b
     46 maybe nothing just = \case
     47     Nothing -> nothing
     48     Just x -> just x
     49 
     50 (??) :: forall (a :: Type). Maybe a -> a -> a
     51 Just x ?? _ = x
     52 Nothing ?? x = x
     53 infix 1 ??
     54 
     55 unsafe_fromJust :: forall (a :: Type). Maybe a -> a
     56 unsafe_fromJust = Base.fromJust
     57 
     58 --------------------------------------------------------------------------------
     59 
     60 type Maybe# :: forall (r :: Repr). Type# r -> Type
     61 type Maybe# a = Maybe (Box a)
     62 
     63 nothing# :: forall {r :: Repr} (a :: Type# r). (Functional# r) => Maybe# a
     64 nothing# = Nothing
     65 
     66 just# :: forall {r :: Repr} (a :: Type# r). (Functional# r) => a -> Maybe# a
     67 just# = Just .# mkBox#
     68 
     69 maybe# :: forall {r :: Repr} (a :: Type# r) {s :: Repr} (b :: Type# s). (Functional# r) => Lev b -> (a -> b) -> Maybe# a -> b
     70 maybe# nothing just = \case
     71     Nothing -> nothing
     72     Just b -> just $# unBox# b
     73 
     74 isNothing# :: forall {r :: Repr} (a :: Type# r). (Functional# r) => Maybe# a -> Bool
     75 isNothing# = maybe# True (const False)
     76 
     77 isJust# :: forall {r :: Repr} (a :: Type# r). (Functional# r) => Maybe# a -> Bool
     78 isJust# = maybe# False (const True)
     79 
     80 (??#) :: forall {r :: Repr} (a :: Type# r). (Functional# r) => Maybe# a -> a -> a
     81 (??#) m = lam# \d -> case m of
     82     Nothing -> d
     83     Just b -> unBox# b
     84 infix 1 ??#