4

コードに奇妙な問題があり、それを解決する方法が本当にわかりません:私のコードは次のとおりです:

module type tGraphe = sig
    type node
    type arc
    end;;

module One : tGraphe with type node=int and type arc=int = struct
    type noeud=int
    type arc=int
end;;

module Two : tGraphe with type node=int and type arc=Empty|Node of(int*int) = struct
    type node=int
    type arc=Empty|Node of(int*int)
end;;

モジュール 1 では問題は発生しませんが、モジュール 2 ではタイプ arc の構文エラーがあることが示されています。また、「with」を使用せずにモジュールの型を使用する方法を誰かが説明してくれれば、ありがたいです。すでに試した

module Two : tGraphe= struct
        type node=int
        type arc=Empty|Node of(int*int)
end;;
open Two;;
let z=Empty;;

しかし、うまくいきません。

4

2 に答える 2

4

定義すると

type foo = int
type bar = Leaf | Node of int * int

fooは、既存の型の型シノニム( int) とbar新しい型を導入し、新しいコンストラクターLeafを導入すると の間には基本的な違いがありNode、以前のものとは異なります。

あなたが書くとき

module M = struct
  type foo = int
  type bar = Leaf | Node of int * int
end

M.fooは と等しいintが、型M.barはそれ自体とのみ等しい: それは新しく、比較するものは何も存在しない (まったく同じコンストラクターを持つ型でさえ、異なっていて互換性がないと見なされる)。

署名で公開することは理にかなっていますが、 :はそれ自体と等しいだけであるとはM.foo = int言いませんが、署名の改良では何の情報も得られません。M.bar = Leaf | ...M.barM.barM with type bar = M.bar

tGrapheコードの問題は、抽象型を使用する署名への帰属を使用することを主張していることです。jrouquie のコードを書いても何も得られない

type global_arc = Vide | Node of (int*int)

module Two : tGraphe with type noeud = int and type arc = global_arc = struct
  type noeud = int
  type arc = global_arc
end;;

単にではなく

module Two = struct
    type noeud = int
    type arc = Vide |Node of(int*int)
end

施工M : Sはチェックだけではありません。からの入力情報を積極的に非表示Mにします。建設S with type ... = ...は、あなたがそうするよりも少し隠すことを可能にするためにそこにありますS. しかし、あなたの例では、何も隠したくない場合は、署名の帰属を使用しないでください! ただ書いてmodule M = struct ... endください。

: S間違っていないこと、フィールド名などに間違いがないことを確認するためだけに使用する場合は、純粋なチェックとして側面で使用できます。

module Two = struct ... end

let () = ignore (module Two : S) (* only a check! *)

SML には、互換性のみをチェックする「透明な」モジュール アノテーションと、互換性を強制して型情報隠す「不透明な」モジュール アノテーション (一般にsealと呼ばれる) が区別されます。OCaml には不透明なシールしかないので、使いすぎないように注意する必要があります。

PS: StackOverflow で質問する場合は、フランス語よりも英語でコードを記述した方がよいでしょう。

于 2013-04-16T11:55:41.190 に答える
0

あなたが何を望んでいるのかよくわかりません。次のコードは機能しますが、ニーズを満たすかどうかはわかりません。

module type tGraphe = sig
  type noeud
  type arc
end;;

type global_arc = Vide | Node of (int*int)

module Two : tGraphe with type noeud = int and type arc = global_arc = struct
  type noeud = int
  type arc = global_arc
end;;

let z: Two.arc = Vide;;
于 2013-04-16T08:41:04.993 に答える