Skip to content

Instantly share code, notes, and snippets.

@boj
Last active March 27, 2020 21:42
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/0e9faa3b43e9e336d723c977a98a7978 to your computer and use it in GitHub Desktop.
Save boj/0e9faa3b43e9e336d723c977a98a7978 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE TypeFamilies #-}
module Main where
import Data.Coerce
untag :: Coercible a b => (b -> a) -> a -> b
untag _ = coerce
data Validation = Validated | Unvalidated
data MyError = MyError String deriving (Show)
newtype Name (a :: Validation) = Name String deriving (Show)
mkName :: String -> Name Unvalidated
mkName = Name
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 (untag Name n) >= 3 = Right (coerce n)
| otherwise = Left (MyError "too short")
main :: IO ()
main = do
print $ validate (mkName "abcd")
print $ validate (mkName "a")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment