OCamlのバグ?
以下のようなヴァリアント型を定義する.
type term_ = | Lit of int | Inc of term_ | IsZ of term_ | If of term_ * term_ * term_ | Pair of term_ * term_ | Fst of term_ | Snd of term_ type 'a term = term_
そして,これらのコンストラクタ用の関数を以下のように定義する.
let lit n:int term = Lit n let inc (t:int term): int term = Inc t let isz (t:int term): bool term = IsZ t let ift (b:bool term) (t1:'a term) (t2:'a term) : 'a term = If(b,t1,t2) let pair (t1:'a term) (t2:'b term) : ('a * 'b) term = Pair(t1,t2) let fst (t:('a * 'b) term): 'a term = Fst t let snd (t:('a * 'b) term): 'b term = Snd t
上記のコンストラクタ関数を使ってみると...
# inc (lit 3) - : int term = Inc (Lit 3) # isz (lit 0) - : bool term = IsZ (Lit 0) ここまではOK # isz (isz (lit 0)) - : bool term = IsZ (IsZ (Lit 3)) !!! isz : int term -> bool termなので型エラーになってほしいのに...
type 'a term = term_
と定義しているので,その部分で型情報がうまく伝播してくれてないような気がする.仕様なのかバグなのか微妙なところだ.