Last active
June 21, 2019 10:06
-
-
Save pwm/23c140293af0f506766b19f7cb9b88b8 to your computer and use it in GitHub Desktop.
Type level tagging
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
{-# 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