Skip to content

Instantly share code, notes, and snippets.

@pwm
Last active June 21, 2019 10:06
Show Gist options
  • Save pwm/23c140293af0f506766b19f7cb9b88b8 to your computer and use it in GitHub Desktop.
Save pwm/23c140293af0f506766b19f7cb9b88b8 to your computer and use it in GitHub Desktop.
Type level tagging
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
module Tag where
newtype Tag t a = MkTag { unTag :: a } deriving (Eq, Ord, Show) via a
----
data Dirty
data Clean
data Foo = MkFoo
{ a :: Int
, b :: String
} deriving (Eq, Ord, Show)
validate :: Tag Dirty Foo -> Maybe (Tag Clean Foo)
validate (MkTag MkFoo{..})
| a > 10 && b == "yes" = Just $ MkTag @Clean MkFoo{..}
| otherwise = Nothing
----
badFoo :: Tag Dirty Foo
badFoo = MkTag @Dirty MkFoo {a = 5, b = "hello"}
goodFoo :: Tag Dirty Foo
goodFoo = MkTag @Dirty MkFoo {a = 50, b = "yes"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment