練習問題 9.4 シグネチャの実用性
次の抽象データ型を表す、ふたつのシグネチャをもつモジュールは、どちらもあまり実用上意味がありません。なぜでしょうか?(ヒント:empty や add のない TABLE モジュールを考えてみなさい)
module type BOGUS1 =
sig
type t
val f: t -> int -> t
end
module type BOGUS2 =
sig
type t
val e: t
end
このシグネチャをみたすモジュールを考えてみた
シグネチャがこれ
module type BOGUS1 =
sig
type t
val f: t -> int -> t
end;;
module type BOGUS2 =
sig
type t
val e: t
end;;
モジュールがこれ
module Bogus1 : BOGUS1 =
struct
type t = int
let f x y = x + y
end;;
module Bo =
struct
type t = int
let f x y = x + y
end;;
コンパイラの応答
module type BOGUS1 = sig type t val f : t -> int -> t end
module type BOGUS2 = sig type t val e : t end
module Bogus1 : BOGUS1
module Bo : sig type t = int val f : t -> t -> t end
ここでたとえば以下のようにすると・・・。
# Bogus1.f 2 3;;
~
Error: This expression has type int but an expression was expected of type Bogus1.t
となる。
BOGUS1シグネチャの定義を
val f: t -> t -> t
としても、結果は同じ。
ここでわからなくなった。
どうすれば、BOGUS1 のシグネチャを満たすことができるのだろう?
これは、検討課題として、後日を期すこととする。