Quick actions

cmd+k|ctrl+k

Navigation

Languages

TypeFamilies - Intro

Snippet info

Language

Haskell

Visibility

public

Author

homam

Created

2016-12-25T18:49:49Z

Updated

2018-03-13T18:42:14Z

{-# LANGUAGE TypeFamilies, MultiParamTypeClasses #-}

-- Generalised Num class
class GNum a b where
  type SumTy a b :: *
  (+>) :: a -> b -> SumTy a b

instance GNum Int Int where
  type SumTy Int Int = Int
  x +> y = x + y
  
instance GNum Int Float where
  type SumTy Int Float = Float
  x +> y = fromIntegral x + y
  
instance GNum Float Int where
  type SumTy Float Int = Float
  x +> y = x + fromIntegral y
  
  
main = do
    print $ (1 :: Int) +> (5 :: Int)
    print $ (1 :: Int) +> (4.2 :: Float)
    print $ (1.24 :: Float) +> (2 :: Int)

-- http://research.microsoft.com/en-us/um/people/simonpj/papers/assoc-types/fun-with-type-funs/FunWithTypeFuns-Apr09.pdf
INFO