Associated Type Synonyms
著者
2005/1
大まかな流れ
Abstract
1 Introduction
2 Applications of Associated Type Synonyms
3 The Programmer's-eye view
4 The Type System
5 Type Inference
6 Functional Dependencies
7 Other related work
8 Conclusions and further work
References
fun depsとassocieated type synonymの関連、みたいなやつか
コレ読みたいやつだ

fun depsとの差異を知りたいだけなので全部を読む気はない

ムズイ。理解できない

これ前提として、
hsにfun depsが実装済み、で、type familyがまだない?もしくはtype familyはあるがAssociated typesがまだない?状況で書かれたものっぽい
Abstract
これは、fun depsに比べて明示的であり、fun depsの代替手段になる
1 Introduction
fun deps周りの議論をするときに
Collects
型クラスを扱うのってあるあるなのか?

hsclass Collects e where
type Elem e
empty :: e
insert :: Elem e -> e -> e
toList :: e -> [Elem e]
元のmulti paramのやつ.hsclass Collects e ce where
empty :: ce -- 空のcollectionを作成
insert :: e -> ce -> ce -- 追加
member :: e -> ce -> Bool -- 存在確認
fun depsで書き換えたやつ.hs class Collects e c | c -> e where
empty :: c
insert :: e -> c -> c
member :: e -> c -> Bool
Elem
とは何か?
Elem c
は、collection type c
の要素となる型
Elem
は、collection typeをelement typeに変える
型レベル関数とみなせる
Elem
はnon-parametoricである
> However, just as insert
is non-parametric (its implementation varies depending on c
), so is Elem
.
>> For example, Elem [e]
is e
, but Elem BitSet
is Char
.
c
はassociated type Elem c
を持つ
具体定期にどういう型なのかはclass宣言時には指定しない
isntance宣言時に与えられる
hsinstance Eq e => Collects [e] where
type Elem [e] = e
..
instance Collects BitSet where
type Elem BitSet = Char
..
instance (Collects c, Hasable (Elem c)) => Collects (Array Int c) where
type Elem (Array Int c) = Elem c
..
fun depsと比較してどちらが良いのかを言いたいわけではない
our goal here is only to describe and characterise a new pointin the design space of type classes.
2 Applications of Associated Type Synonyms
hs{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
data I f = I f
data C f = C f
data S f = S String f
formatSpec :: S (I (S (C String)))
formatSpec = S "Int: " $ I $ S ", Char: " $ C $ "."
class Format fmt where
type Sprintf fmt
sprintf' :: String -> fmt -> Sprintf fmt
instance Format String where
type Sprintf String = String
sprintf' prefix str = prefix ++ str
instance Format a => Format (I a) where
type Sprintf (I a) = Int -> Sprintf a
sprintf' prefix (I a) = \i -> sprintf' (prefix ++ show i) a
instance Format a => Format (C a) where
type Sprintf (C a) = Char -> Sprintf a
sprintf' prefix (C a) = \c -> sprintf' (prefix ++ [c]) a
-- errorになる
instance Format a => Sprintf (S a) where
type Sprintf (S a) = Sprintf a
sprintf' prefix (S str a) = sprintf' (prefix ++ str) a
sprintf :: Format fmt => fmt -> Sprintf fmt
sprintf = sprintf' ""
3 The Programmer's-eye view
4 The Type System
5 Type Inference
6 Functional Dependencies
fun depsとの比較
fun depsには3つのvariantが存在するので比較は複雑になる
決定可能である
2 GHCで実装されているもの
決定可能でない
よくわからんが、一部を妥協することで1,2より一般的になっている
決定可能である
7 Other related work
Hindley-Milner, ML moduleについての関連
8 Conclusions and further work
References