Type errors when using same name

孤人 提交于 2020-03-03 11:41:05

问题


I have 3 files:

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

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

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

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.

The code extraction.v:

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

where ExtrOcamlString

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

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

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

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.

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

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

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


回答1:


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

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


来源:https://stackoverflow.com/questions/10026597/type-errors-when-using-same-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!