fundeps
ocaml-nagoyaでid:syd_sydさんにHaskellの型推論のマニアックな機能について教えてもらった。型クラスに複数のパラメーターがあった時に、そのパラメーター同士の関係を明示する事で、より直感的な推論結果を導けるらしい。具体的にはfunctional dependenciesという機能。似た方向性としてtype familyというのもあるらしい。
OCamlだと、ins2はこうかな?
# let ins2 xs x y = (xs#insert x)#insert y;; val ins2 : < insert : 'a -> < insert : 'b -> 'c; .. >; .. > -> 'a -> 'b -> 'c = <fun>
やっぱりxとyは同じ型にならないなー。insertメソッドが何型を返してもいいんだから、そりゃそーかー。なんか型クラスとは違うなー。
これならどうだ?
# class type ['a, 'b] t_coll = object method empty : 'a method insert : 'b -> 'a end;; class type ['a, 'b] t_coll = object method empty : 'a method insert : 'b -> 'a end # let ins2 (xs : ('a, 'b) t_coll) x y = (xs#insert x)#insert y;; val ins2 : (< insert : 'a -> 'b; .. >, 'c) t_coll -> 'c -> 'a -> 'b = <fun>
うーん、だめぽ。また考えよう。
追記:
あっ、こうかな?
# class type ['a] t_coll = object ('me) method empty : 'me method insert : 'a -> 'me end;; class type ['a] t_coll = object ('b) method empty : 'b method insert : 'a -> 'b end # let ins2 (xs : 'a t_coll) x y = (xs#insert x)#insert y;; val ins2 : 'a t_coll -> 'a -> 'a -> 'a t_coll = <fun>
あれ?型パラメーターがひとつ?混乱中。