Skip to content

Instantly share code, notes, and snippets.

@relrod
Created June 4, 2015 07:01
Show Gist options
  • Save relrod/232cbc526f676ac25780 to your computer and use it in GitHub Desktop.
Save relrod/232cbc526f676ac25780 to your computer and use it in GitHub Desktop.
This is that flexibility thing that Scala folks always talk about, right? ;) (aka I finally decided to learn about TypeFamilies, then had some fun)
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE RebindableSyntax #-}
module Foo where
import Prelude hiding ((+), Num(..))
import qualified Prelude as P
default (Integer)
class Add a b where
type AddThings a b
(+) :: a -> b -> AddThings a b
instance Add String Integer where
type AddThings String Integer = String
a + b = a ++ show b
instance Add Integer String where
type AddThings Integer String = String
a + b = show a ++ b
instance P.Num a => Add a a where
type AddThings a a = a
a + b = a P.+ b
fromInteger :: Integer -> Integer
fromInteger = id
-- λ> :set -XRebindableSyntax
-- λ> :l /tmp/foo.hs
--
-- λ> 3 + 4
-- 7
--
-- λ> 3 + "four"
-- "3four"
--
-- λ> "three" + 4
-- "three4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment