-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE StandaloneDeriving #-} | ||
|
||
module Data.MaybeK | ||
( MaybeK (..) | ||
, Presence (..) | ||
, fmapMaybeK | ||
, maybeOfMaybeK | ||
) | ||
where | ||
|
||
import Prelude | ||
|
||
-- | A type level tag to indicate whether a value is present or not. | ||
data Presence = Present | Absent | ||
|
||
-- | A Maybe carring the type level tag. | ||
data MaybeK x a where | ||
NothingK :: MaybeK x Absent | ||
JustK :: x -> MaybeK x Present | ||
|
||
deriving instance Show x => Show (MaybeK x a) | ||
deriving instance Eq x => Eq (MaybeK x a) | ||
|
||
-- | Map a function over a MaybeK. | ||
fmapMaybeK :: (x -> y) -> MaybeK x a -> MaybeK y a | ||
fmapMaybeK _ NothingK = NothingK | ||
fmapMaybeK f (JustK x) = JustK (f x) | ||
|
||
-- | Convert a MaybeK to a Maybe, dropping the type level tag. | ||
maybeOfMaybeK :: MaybeK x a -> Maybe x | ||
maybeOfMaybeK NothingK = Nothing | ||
maybeOfMaybeK (JustK x) = Just x |