flat-maybe (empty) → 0.1.0.0
raw patch · 4 files changed
+226/−0 lines, 4 filesdep +basedep +ghc-primsetup-changed
Dependencies added: base, ghc-prim
Files
- Data/Maybe/Flat.hs +156/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- flat-maybe.cabal +38/−0
+ Data/Maybe/Flat.hs view
@@ -0,0 +1,156 @@+{-# LANGUAGE MagicHash, PatternSynonyms, ViewPatterns,+ RoleAnnotations, UnboxedTuples, BangPatterns, KindSignatures,+ DeriveDataTypeable, ScopedTypeVariables #-}++module Data.Maybe.Flat (+ Maybe+ , pattern Nothing+ , pattern Just+ , maybe+ ) where+ +import Prelude hiding (Maybe(..), maybe, null)++import Control.Applicative+import Control.Monad+import Control.Monad.Fix+import GHC.Prim+import GHC.Read+import GHC.Show+import Text.ParserCombinators.ReadPrec+import qualified Text.Read.Lex as L+import Data.Data++type role Maybe nominal+newtype Maybe (a :: *) = Maybe Any deriving (Typeable)++data Null = Null++null :: Null+null = Null+{-# NOINLINE null #-}++isNothing# :: Maybe a -> Int#+isNothing# (Maybe !any) = reallyUnsafePtrEquality# (unsafeCoerce# any) null+{-# INLINE isNothing# #-}++isJust# :: Maybe a -> (# Int#, a #)+isJust# (Maybe !any) =+ (# reallyUnsafePtrEquality# (unsafeCoerce# any) null, unsafeCoerce# any #)+{-# INLINE isJust# #-} ++pattern Nothing <- (isNothing# -> 1#) where+ Nothing = (unsafeCoerce# null :: Maybe a)++pattern Just a <- (isJust# -> (# 0#, a #)) where+ Just (a :: a) = (unsafeCoerce# a :: Maybe a)++maybe :: b -> (a -> b) -> Maybe a -> b+maybe b f (Just a) = f a+maybe b f _ = b++instance Functor Maybe where+ fmap f (Just a) = Just (f a)+ fmap f x = unsafeCoerce# x++instance Foldable Maybe where+ foldr f z (Just a) = f a z+ foldr f z _ = z+ + foldl f = foldr (flip f)+ + foldMap f (Just a) = f a+ foldMap f _ = mempty++instance Traversable Maybe where+ traverse f (Just a) = Just <$> f a+ traverse f x = pure (unsafeCoerce# x)++instance Eq a => Eq (Maybe a) where+ Just a == Just b = a == b+ _ == _ = False++instance Monoid a => Monoid (Maybe a) where+ mempty = Nothing+ Nothing `mappend` m = m+ m `mappend` Nothing = m+ Just m1 `mappend` Just m2 = Just (m1 `mappend` m2) ++instance Ord a => Ord (Maybe a) where+ compare (Just a) (Just b) = compare a b+ compare Nothing (Just _) = LT+ compare (Just _) Nothing = GT+ compare _ _ = EQ++instance Show a => Show (Maybe a) where+ showsPrec p (Just a) s =+ (showParen (p > appPrec) $+ showString "Just " .+ showsPrec appPrec1 a) s + showsPrec _ _ s = showString "Nothing" s+++instance Read a => Read (Maybe a) where+ readPrec =+ parens+ (do expectP (L.Ident "Nothing")+ return Nothing+ ++++ prec appPrec (+ do expectP (L.Ident "Just")+ x <- step readPrec+ return (Just x))+ ) ++instance Applicative Maybe where+ pure = Just++ Just f <*> m = fmap f m+ x <*> _m = unsafeCoerce# x++ Just _m1 *> m2 = m2+ x *> _m2 = unsafeCoerce# x++instance Monad Maybe where+ (Just x) >>= k = k x+ x >>= _ = unsafeCoerce# x++ (>>) = (*>)++ return = Just+ fail _ = Nothing++instance Alternative Maybe where+ empty = Nothing+ Nothing <|> r = r+ l <|> _ = l++instance MonadPlus Maybe++instance MonadFix Maybe where+ mfix f = let a = f (unJust a) in a+ where unJust (Just x) = x+ unJust _ = error "mfix Maybe: Nothing"++nothingConstr :: Constr+nothingConstr = mkConstr maybeDataType "Nothing" [] Prefix+justConstr :: Constr+justConstr = mkConstr maybeDataType "Just" [] Prefix++maybeDataType :: DataType+maybeDataType = mkDataType "Prelude.Maybe" [nothingConstr,justConstr]++instance Data a => Data (Maybe a) where+ gfoldl f z (Just x) = z Just `f` x+ gfoldl _ z x = z x++ toConstr (Just _) = justConstr + toConstr _ = nothingConstr++ gunfold k z c = case constrIndex c of+ 1 -> z Nothing+ 2 -> k (z Just)+ _ -> error "Data.Data.gunfold(Maybe)"+ dataTypeOf _ = maybeDataType+ dataCast1 f = gcast1 f+
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, András Kovács++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 András Kovács 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
+ flat-maybe.cabal view
@@ -0,0 +1,38 @@+-- Initial flat-maybe.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/ + +name: flat-maybe +version: 0.1.0.0 +synopsis: Strict Maybe without space and indirection overhead +description: + Strict Maybe without space and indirection overhead. + Inspired by Rust's Option, which is represented by + either a pointer to the object or a specific "null" pointer. + The implementation is a giant unsafe trick. + +homepage: https://github.com/AndrasKovacs/flat-maybe +bug-reports: https://github.com/AndrasKovacs/flat-maybe/issues +maintainer: puttamalac@gmail.com + +license: BSD3 +license-file: LICENSE +author: András Kovács +maintainer: puttamalac@gmail.com +copyright: 2015 András Kovács +category: Data +build-type: Simple +cabal-version: >=1.10 +tested-with: GHC == 7.10.2 + +source-repository head + type: git + location: https://github.com/AndrasKovacs/flat-maybe.git + +library + exposed-modules: Data.Maybe.Flat + -- other-modules: + -- other-extensions: + build-depends: base >=4.8 && <4.9, ghc-prim == 0.4.0.0 + -- hs-source-dirs: + default-language: Haskell2010 + ghc-options: -O2