Created
May 10, 2020 11:59
-
-
Save barrucadu/9033540712e953c874b5e565aed46e13 to your computer and use it in GitHub Desktop.
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 FlexibleContexts #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
class IsSubtypeOf a b where | |
to :: a -> b | |
from :: b -> Maybe a | |
instance IsSubtypeOf Int Int where | |
to = id | |
from = Just | |
instance IsSubtypeOf Bool Int where | |
to True = 1 | |
to False = 0 | |
from 0 = Just False | |
from 1 = Just True | |
from _ = Nothing | |
add :: (IsSubtypeOf a Int, IsSubtypeOf b Int) => a -> b -> Int | |
add a b = to a + to b | |
main :: IO () | |
main = do | |
print $ add True (3::Int) | |
print $ add True False | |
print $ add (3::Int) (7::Int) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment