我有3个文件:
1)cpf0.ml
type string = char list
type url = string
type var = string
type name = string
type symbol =
| Symbol_name of name
Run Code Online (Sandbox Code Playgroud)
2)problem.ml:
type symbol =
| Ident of string
Run Code Online (Sandbox Code Playgroud)
3)test.ml
open Problem;;
open Cpf0;;
let symbol b = function
| Symbol_name n -> Ident n
Run Code Online (Sandbox Code Playgroud)
当我结合时test.ml:ocamlc -c test.ml. 我收到一个错误:
该表达式的类型为 Cpf0.name = char list,但表达式应为字符串类型
你能帮我改正吗?非常感谢
编辑:谢谢您的回答。我想详细解释一下这 3 个文件:因为我正在 Coq 中提取到 Ocaml 类型:cpf0.ml生成自
cpf.v:
Require Import String.
Definition string := string.
Definition name := string.
Inductive symbol :=
| Symbol_name : name -> symbol.
Run Code Online (Sandbox Code Playgroud)
代码extraction.v:
Set Extraction Optimize.
Extraction Language Ocaml.
Require ExtrOcamlBasic ExtrOcamlString.
Extraction Blacklist cpf list.
Run Code Online (Sandbox Code Playgroud)
我打开:open Cpf0;;in problem.ml,我遇到了一个新问题,因为problem.ml他们对类型有另一个定义string
此表达式的类型为 Cpf0.string = char list,但表达式的类型应为 Util.StrSet.elt = string
这是定义类型字符串的定义util.ml:
module Str = struct type t = string end;;
module StrOrd = Ord.Make (Str);;
module StrSet = Set.Make (StrOrd);;
module StrMap = Map.Make (StrOrd);;
let set_add_chk x s =
if StrSet.mem x s then failwith (x ^ " already declared")
else StrSet.add x s;;
Run Code Online (Sandbox Code Playgroud)
我试图更改t = string为t = char list,但如果我这样做,我必须更改它所依赖的很多功能(例如:set_add_chk上面)。你能给我一个好主意吗?在这种情况下我会怎么做。
编辑2:很抱歉多次编辑这个问题。按照答案后,我修复了文件 Problem.ml
type symbol =
| Ident of Cpf0.string
Run Code Online (Sandbox Code Playgroud)
他们problem.ml有另一个这样的定义。并且类型一再次不被接受。
module SymbSet = Set.Make (SymbOrd);;
let rec ident_of_symbol = function
| Ident s -> s
let idents_of_symbols s =
SymbSet.fold (fun f s -> StrSet.add (ident_of_symbol f) s) s StrSet.empty;;
Run Code Online (Sandbox Code Playgroud)
此表达式的类型为 Cpf0.string = char list,但表达式的类型应为 Util.StrSet.elt = string
您需要在problem.ml中打开模块Cpf0,因为模块Cfp0和Problem中的类型字符串不相同。
问题.ml:
open Cpf0
type symbol =
| Ident of string
Run Code Online (Sandbox Code Playgroud)
或者更好的是,不要打开模块并为类型字符串添加前缀,如下所示:
type symbol =
| Ident of Cpf0.string
Run Code Online (Sandbox Code Playgroud)