{-# 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 $bJustVoid# :: forall (a :: TYPE ('TupleRep '[])). a -> MaybeVoid# a
$mJustVoid# :: forall {r} {a :: TYPE ('TupleRep '[])}.
MaybeVoid# a -> (a -> r) -> ((# #) -> r) -> r
JustVoid# a <- (helper -> (# 1##, a #)) where
  JustVoid# a
_ = forall (a :: TYPE ('TupleRep '[])). Word# -> MaybeVoid# a
MaybeVoid# Word#
1##

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

pattern NothingVoid# :: MaybeVoid# a
pattern $bNothingVoid# :: (# #) -> forall (a :: TYPE ('TupleRep '[])). MaybeVoid# a
$mNothingVoid# :: forall {r} {a :: TYPE ('TupleRep '[])}.
MaybeVoid# a -> ((# #) -> r) -> ((# #) -> r) -> r
NothingVoid# = MaybeVoid# 0##