Last active
March 27, 2020 21:42
-
-
Save boj/0e9faa3b43e9e336d723c977a98a7978 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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