Skip to content

Instantly share code, notes, and snippets.

@KeenS
Created June 3, 2018 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KeenS/883eb276d45ac8553a60b664d5209313 to your computer and use it in GitHub Desktop.
Save KeenS/883eb276d45ac8553a60b664d5209313 to your computer and use it in GitHub Desktop.
GHCで {リスト,スカラ} x {リスト,スカラ}演算のオーバーロード
{-#LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleInstances #-}
class Overload a b where
type Out a b :: *
(+):: a -> b -> Out a b
instance Overload Int Int where
type Out Int Int = Int
(+) = (Prelude.+)
instance Overload [Int] Int where
type Out [Int] Int = [Int]
(+) a b = map (Prelude.+ b) a
instance Overload Int [Int] where
type Out Int [Int] = [Int]
(+) a b = map (a Prelude.+) b
instance Overload [Int] [Int] where
type Out [Int] [Int] = [Int]
(+) a b = map (uncurry (Prelude.+)) $ zip a b
main :: IO ()
main = return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment