使用相同名称时输入错误 [英] Type errors when using same name

查看:59
本文介绍了使用相同名称时输入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个文件:

1)cpf0.ml

type string = char list
type url = string
type var = string
type name = string
type symbol =
| Symbol_name of name

2)problem.ml:

type symbol =
  | Ident of string

3)test.ml

open Problem;;
open Cpf0;;

let symbol b = function
  | Symbol_name n -> Ident n

当我组合test.ml时:ocamlc -c test.ml. 我收到一个错误:

When I combine test.ml: ocamlc -c test.ml. I received an error:

此表达式的类型为Cpf0.name =字符列表 但是需要一个字符串类型的表达式

This expression has type Cpf0.name = char list but an expression was expected of type string

能帮我改正吗?非常感谢

Could you please help me to correct it? Thank you very much

编辑:谢谢您的回答.我想详细说明这3个文件: 因为我正在用Coq到Ocaml类型进行提取,所以cpf0.ml从以下位置生成 cpf.v:

EDIT: Thank you for your answer. I want to explain more about these 3 files: Because I am working with extraction in Coq to Ocaml type: cpf0.ml is generated from cpf.v :

 Require Import String.
 Definition string := string.
 Definition name := string.
 Inductive symbol := 
  | Symbol_name : name -> symbol.

代码extraction.v:

Set Extraction Optimize.
Extraction Language Ocaml.
Require ExtrOcamlBasic ExtrOcamlString.
Extraction Blacklist cpf list.

其中 ExtrOcamlString

我在problem.ml中打开了open Cpf0;;,但遇到了一个新问题,因为在problem.ml中,它们对string类型有另一个定义

I opened: open Cpf0;; in problem.ml, and I got a new problem because in problem.ml they have another definition for type string

此表达式的类型为Cpf0.string =字符列表 但是期望使用Util.StrSet.elt = string

This expression has type Cpf0.string = char list but an expression was expected of type Util.StrSet.elt = string

这是util.ml定义的类型字符串中的定义:

Here is a definition in util.ml defined type string:

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;;

我试图将t = string更改为t = char list,但是如果这样做,我必须更改它所依赖的许多功能(例如,上面的set_add_chk).你能给我一个好主意吗?在这种情况下我该怎么做.

I was trying to change t = string to t = char list, but if I do that I have to change a lot of function it depend on (for example: set_add_chk above). Could you please give me a good idea? how I would do in this case.

编辑2 :很抱歉,我多次对此问题进行了编辑.遵循答案后,我修复了文件问题.ml

EDIT 2: I am sorry to edit this question many times. After follow the answer, I fixed the file problem.ml

type symbol =
  | Ident of Cpf0.string

problem.ml中,它们具有另一个类似的定义.而且类型一再次不被接受.

In problem.ml they have another definition like this. And the type one again does not accepted.

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;;

此表达式的类型为Cpf0.string =字符列表,但应使用Util.StrSet.elt = string类型的表达式

推荐答案

您需要在issue.ml中打开模块Cpf0,因为模块Cfp0和Problem中的类型字符串不同.

You need to open module Cpf0 in problem.ml because the type string in modules Cfp0 and Problem is not the same.

problem.ml:

problem.ml:

open Cpf0
type symbol =
  | Ident of string

或者更好的是,不要打开模块并为类型字符串加上前缀:

or better, don't open the module and prefix the type string like this:

type symbol =
  | Ident of Cpf0.string

这篇关于使用相同名称时输入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆