Skip to content

Instantly share code, notes, and snippets.

@boj
Last active March 27, 2020 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boj/7540bb0edc458ed6c8742f375e0558b1 to your computer and use it in GitHub Desktop.
Save boj/7540bb0edc458ed6c8742f375e0558b1 to your computer and use it in GitHub Desktop.
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE TypeFamilies #-}
module Main where
import Data.Coerce
import Data.Tagged
data Validated
data Unvalidated
data MyError = MyError String deriving (Show)
newtype Name a = Name { unName :: String } deriving (Show)
-- data NameTag
-- type Name a = Tagged NameTag String
-- mkName :: String -> Name Unvalidated
-- mkName = Tagged
class Validatable a where
type ValidationError a :: *
validate :: a Unvalidated -> Either (ValidationError a) (a Validated)
instance Validatable Name where
type ValidationError Name = MyError
validate n
| length (unName n) >= 3 = Right (coerce n)
| otherwise = Left (MyError "too short")
main :: IO ()
main = do
print $ validate (Name "abcd")
print $ validate (Name "a")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment