Skip to content

Instantly share code, notes, and snippets.

@barrucadu
Created May 10, 2020 11:59
Show Gist options
  • Save barrucadu/9033540712e953c874b5e565aed46e13 to your computer and use it in GitHub Desktop.
Save barrucadu/9033540712e953c874b5e565aed46e13 to your computer and use it in GitHub Desktop.
{-# 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