@coord_e
こういう感じの関数型言語です(省略)
f x
f x = e
x :: t
ここでは式を分類します(省略)
class Eq a where eq :: a -> a -> Bool -- 同値比較ができる型のリストの上でのみ行える操作 member :: Eq a => a -> [a] -> Bool member x [] = False member x (h : t) | eq x h = True | otherwise = member x t
Eq a
a
eq
Eq
※OOPはね、関係ない
class Eq a where eq :: a -> a -> Bool instance Eq Char where eq = ... -- プリミティヴな操作 instance Eq Int where eq = ... -- プリミティヴな操作 instance Eq Bool where eq True True = True eq False False = True eq _ _ = False
member 'a' ['b', 'c'] :: Eq Char => Bool
instance Eq Char
Eq Char
Bool
instance (Eq a, Eq b) => Eq (a, b) where eq (x1, y1) (x2, y2) = eq x1 x2 && eq y1 y2
(Eq a, Eq b)
Eq (a, b)
b
C t
eq (True, 'a') (False, 'b') :: Eq (Bool, Char) => Bool
instance (Eq a, Eq b) => Eq (a, b)
Eq (Bool, Char)
Eq Bool, Eq Char
instance Eq Bool
class Eq a => Ord a where lt :: a -> a -> Bool gt :: a -> a -> Bool ...
Ord a
Eq a, Ord a
Ord t
Eq t
x@d
x
d
x@d = ...
member x [] = False member x (h : t) | eq x h = True | otherwise = member x t
member :: Eq a => a -> a -> Bool
member@dict x [] = False member@dict x (h : t) | eq@dict x h = True | otherwise = member x t
member :: forall a. Eq a => a -> a -> Bool
member (True, 'a') [] :: Eq (Bool, Char) => Bool
-- `eqPair` は `Eq (a, b)` の実装が入った辞書 member@eqPair (True, 'a') [] :: (Eq Bool, Eq Char) => Bool
member
eqPair
Eq Bool
-- `eqBool` は `Eq Bool` の実装が入った辞書 member@(eqPair@eqBool) (True, 'a') [] :: Eq Char => Bool
-- `eqChar` は `Eq Char` の実装が入った辞書 member@(eqPair@eqBool@eqChar) (True, 'a') [] :: Bool
class Functor f where fmap :: (a -> b) -> f a -> f b instance Functor [] where fmap = map instance Functor Maybe where fmap f (Just x) = Just (f x) fmap _ Nothing = Nothing
class Functor f where fmap :: (a -> b) -> f a -> f b void :: Functor f => f a -> f () void = fmap (const ())
↓
data FunctorDict f = FunctorDict { fmap :: forall a b. (a -> b) -> f a -> f b } void :: FunctorDict f -> f a -> f () void dict = fmap dict (const ())
スライド
coord-e.github.io/slide-type-class-lt
textlint-disable terminology
textlint-enable terminology