packages feed

unlifted-0.2.1.0: src/Data/Maybe/Void.hs

{-# language DataKinds #-}
{-# language ScopedTypeVariables #-}
{-# language ViewPatterns #-}
{-# language RankNTypes #-}
{-# language KindSignatures #-}
{-# language PatternSynonyms #-}
{-# language MagicHash #-}
{-# language GADTSyntax #-}
{-# language UnliftedNewtypes #-}
{-# language UnboxedTuples #-}

module Data.Maybe.Void
  ( MaybeVoid#(..)
  , pattern JustVoid#
  , pattern NothingVoid#
  ) where

import GHC.Exts

-- | Unboxed variant of @Maybe@. The thing possibly contained by @Just@
-- has a void runtime representation. Rather than using a sum, like the
-- more general @Maybe#@ does, this represents @Nothing@ with 0 and
-- @Just@ with 1.
--
-- It is recommended that the data constructor not be used directly.
-- Prefer the two pattern synonyms.
newtype MaybeVoid# :: TYPE ('TupleRep '[]) -> TYPE 'WordRep where
  MaybeVoid# :: forall (a :: TYPE ('TupleRep '[])). Word# -> MaybeVoid# a

pattern JustVoid# :: a -> MaybeVoid# a
pattern JustVoid# a <- (helper -> (# 1##, a #)) where
  JustVoid# _ = MaybeVoid# 1##

helper :: forall (a :: TYPE ('TupleRep '[])).
  MaybeVoid# a -> (# Word#, a #)
{-# inline helper #-}
helper (MaybeVoid# x) =
  (# x, (unsafeCoerce# :: (# #) -> a) (# #) #)

pattern NothingVoid# :: MaybeVoid# a
pattern NothingVoid# = MaybeVoid# 0##