church-maybe (empty) → 0.1.0.0
raw patch · 5 files changed
+225/−0 lines, 5 filesdep +basedep +deepseqdep +semigroupoidssetup-changed
Dependencies added: base, deepseq, semigroupoids, semigroups
Files
- ChangeLog.md +3/−0
- LICENCE +31/−0
- Setup.hs +2/−0
- church-maybe.cabal +35/−0
- src/Data/Church/Maybe.hs +154/−0
+ ChangeLog.md view
@@ -0,0 +1,3 @@+## 0.1.0.0++Intial release
+ LICENCE view
@@ -0,0 +1,31 @@+Copyright (c) 2017, Commonwealth Scientific and Industrial Research Organisation+(CSIRO) ABN 41 687 119 230.++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Data61 nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ church-maybe.cabal view
@@ -0,0 +1,35 @@+-- Initial church-maybe.cabal generated by cabal init. For further documentation,+-- see http://haskell.org/cabal/users-guide/++name: church-maybe+version: 0.1.0.0+synopsis: Church encoded Maybe+description: Church encoded Maybe type, exposing the same API as Data.Maybe+license: BSD3+-- Must be spelled with a 'C' for nix+license-file: LICENCE+author: Isaac Elliott+maintainer: Queensland Functional Programming Lab <oᴉ˙ldɟb@llǝʞsɐɥ>+homepage: https://github.com/qfpl/church-maybe+bug-reports: https://github.com/qfpl/church-maybe/issues+category: Data+build-type: Simple+extra-source-files: ChangeLog.md+cabal-version: >=1.10+tested-with: GHC == 7.10.3, GHC == 8.0.2, GHC == 8.2.2, GHC == 8.4.3+source-repository head+ type: git+ location: git@github.com:qfpl/church-maybe.git++library+ exposed-modules: Data.Church.Maybe+ build-depends: base >=4.8 && <5+ , deepseq+ , semigroups >=0.9 && <0.19+ , semigroupoids >=4 && <6+ if impl(ghc < 8.2)+ build-depends: deepseq >= 1.4+ if impl(ghc >= 8.2)+ build-depends: deepseq >= 1.4.3+ hs-source-dirs: src+ default-language: Haskell2010
+ src/Data/Church/Maybe.hs view
@@ -0,0 +1,154 @@+{-# language NoImplicitPrelude #-}+{-# language RankNTypes #-}+{-# language CPP #-}++module Data.Church.Maybe+ ( just, nothing, Maybe(..)+ , maybe, isNothing, isJust, fromMaybe+ , listToMaybe, maybeToList+ , catMaybes, mapMaybe+ )+where++import Control.Applicative (Alternative(..), Applicative(..))+#if __GLASGOW_HASKELL__ < 802+import Control.DeepSeq (NFData(..))+#else+import Control.DeepSeq (NFData(..), NFData1(..))+#endif+import Control.Monad (Monad(..), MonadPlus(..), liftM2)+import Control.Monad.Fix (MonadFix(..))+import Control.Monad.Zip (MonadZip(..))+import Data.Bool (Bool(..))+import Data.Foldable (Foldable(..))+import Data.Function ((.), const, id)+import Data.Functor (Functor(..))+import Data.Functor.Alt (Alt(..))+import Data.Functor.Apply (Apply(..))+import Data.Functor.Bind (Bind(..))+import Data.Monoid (Monoid(..))+import Data.Semigroup (Semigroup(..))+import Data.Traversable (Traversable(..))+import GHC.Err (error)++newtype Maybe a = Maybe { unMaybe :: forall r. r -> (a -> r) -> r }++{-# inline just #-}+just :: a -> Maybe a+just a = Maybe (\_ f -> f a)++{-# inline nothing #-}+nothing :: Maybe a+nothing = Maybe (\a _ -> a)++{-# inline maybe #-}+maybe :: b -> (a -> b) -> Maybe a -> b+maybe b f m = unMaybe m b f++{-# inline isNothing #-}+isNothing :: Maybe a -> Bool+isNothing m = unMaybe m True (const False)++{-# inline isJust #-}+isJust :: Maybe a -> Bool+isJust m = unMaybe m False (const True)++{-# inline fromMaybe #-}+fromMaybe :: a -> Maybe a -> a+fromMaybe a m = unMaybe m a id++{-# inline listToMaybe #-}+listToMaybe :: [a] -> Maybe a+listToMaybe [] = nothing+listToMaybe (a:_) = just a++{-# inline maybeToList #-}+maybeToList :: Maybe a -> [a]+maybeToList m = unMaybe m [] (: [])++{-# inline catMaybes #-}+catMaybes :: [Maybe a] -> [a]+catMaybes = go+ where+ go [] = []+ go (a : as) = unMaybe a (go as) (: go as)++{-# inline mapMaybe #-}+mapMaybe :: (a -> Maybe b) -> [a] -> [b]+mapMaybe f = go+ where+ go [] = []+ go (a : as) = unMaybe (f a) (go as) (: go as)++instance Functor Maybe where+ {-# inline fmap #-}+ fmap f (Maybe m) = Maybe (\n j -> m n (j . f))++instance Apply Maybe where+ {-# inline (<.>) #-}+ Maybe mf <.> Maybe ma = Maybe (\n j -> mf n (\f -> ma n (j . f)))++instance Applicative Maybe where+ {-# inline pure #-}+ pure = just+ {-# inline (<*>) #-}+ (<*>) = (<.>)++instance Alt Maybe where+ {-# inline (<!> )#-}+ Maybe ma <!> Maybe mb = Maybe (\n j -> ma (mb n j) j)++instance Alternative Maybe where+ {-# inline empty #-}+ empty = nothing+ {-# inline (<|>) #-}+ (<|>) = (<!>)++instance Bind Maybe where+ {-# inline (>>-) #-}+ Maybe ma >>- f = Maybe (\n j -> ma n (\a -> unMaybe (f a) n j))++instance Monad Maybe where+ {-# inline (>>=) #-}+ (>>=) = (>>-)++instance MonadPlus Maybe where++instance MonadFix Maybe where+ {-# inline mfix #-}+ mfix f =+ let+ x = f (unMaybe x (error "mfix Maybe: Nothing") id)+ in+ x++instance MonadZip Maybe where+ {-# inline mzipWith #-}+ mzipWith = liftM2++instance Semigroup a => Semigroup (Maybe a) where+ {-# inline (<>) #-}+ Maybe ma <> Maybe mb = Maybe (\n j -> ma n (\a -> mb n (j . (a <>))))++instance Semigroup a => Monoid (Maybe a) where+ {-# inline mempty #-}+ mempty = nothing+ {-# inline mappend #-}+ mappend = (<>)++instance Foldable Maybe where+ {-# inline foldMap #-}+ foldMap f m = unMaybe m mempty f++instance Traversable Maybe where+ {-# inline traverse #-}+ traverse f m = unMaybe m (pure nothing) (fmap just . f)++-- | And 'NFData1' for GHC >=8.2+instance NFData a => NFData (Maybe a) where+ rnf (Maybe m) = m () rnf++#if __GLASGOW_HASKELL__ >= 802+instance NFData1 Maybe where+ liftRnf f (Maybe m) = m () f+#endif