English 中文(简体)
• 如何用手套把 par子打入GADT的表达方式?
原标题:How to use menhir to parse into a GADT expression?

我刚刚在OCaml至Real World OCaml学习过一些有关GADT的内容,并想设法将其中第一点语言翻译成口译员,从而使用男子。

最确切的定义与实例完全相同。 这里是:

type _ value =
  | Int : int -> int value
  | Bool : bool -> bool value

type _ expr = 
  | Value:  a value ->  a expr
  | Eq : int expr * int expr -> bool expr
  | Plus : int expr * int expr -> int expr
  | If : bool expr *  a expr *  a expr ->  a expr
  

let eval_value : type a. a value -> a = function
  | Int x -> x
  | Bool x -> x ;;
  
let rec eval: type a. a expr -> a = function
  | Value v -> eval_value v
  | If (c, t, e) -> if eval c then eval t else eval e
  | Eq (x, y) -> eval x = eval y
  | Plus (x, y) -> eval x + eval y;
 

这里是我的法理和教法特写。

{
    open Parser
    exception Error of string
}

rule token = parse
| [     	 ] {token lexbuf}
|  
  {Lexing.new_line lexbuf; token lexbuf}
| "if" {IF}
| "then" {THEN}
| "else" {ELSE}
|  =  {EQ}
|  +  {PLUS}
| "true" {BOOL (true)}
| "FALSE" {BOOL (false)}
| [ 0 - 9 ]+ as i {NUM (int_of_string i)}
|  - [ 0 - 9 ]+ as i { NUM (int_of_string i) }
| eof {EOF}
| _ { raise (Error (Printf.sprintf "At offset %d: unexpected character.
" (Lexing.lexeme_start lexbuf))) }
%{
    open Ast
%}

%token <int> NUM
%token <bool> BOOL
%token PLUS
%token IF
%token THEN
%token ELSE
%token EQ
%token EOF

%left PLUS

%start < a Ast.expr> expr_toplevel

%%

expr_toplevel:
| e = expr EOF {e}

expr:
| x = expr PLUS y = expr {Plus (x, y)} 
| IF c = expr THEN x = expr ELSE y = expr {If (c, x, y)}
| x = expr EQ y = expr {EQ (x, y)}
| b = BOOL {Value (Bool b)}
| n = NUM {Value (Int n)}

所有这一切,以及上述法典未能建立,其错误是:

File "bin/parser.mly", line 27, characters 18-26:
Error: This expression has type bool value
       but an expression was expected of type int value
       Type bool is not compatible with type int 

这里指的是:

| b = BOOL {Value (Bool b)}

这一错误与我们在第<条码>、当地摘要型和多形态再入侵/代码”第一组代码中发现的错误完全相同,作者试图将GADT视为普通差异。 这里的问题是同一问题吗? 如果是,我如何能够确定这一点,使之发挥作用。

男子手法中没有多少关于东帝汶国防军的内容。 这一点在国旗上被提及——投机。 我已尝试过这种变化,但没有任何变化。

问题回答

根本问题是,你试图书写一种类型的功能。

val parse: (x:string) -> f(x) Ast.expr

where the type of the parsed ast depends on the value of the input. This is only possible in dependently typed language, and OCaml is not dependently typed.

值得考虑的一个解决办法是使母子回归成为一种代表<条码>的任何Ast.expr的类型,这种类型可以用一种现成的量化加以界定:

type any_expr = Any:  a Ast.expr -> any_expr [@@unboxed]

然后expr 分配职能类型如下:

val parse: string -> any_expr

并且仍然有可能配备能够回归特定类型的<代码>expr的分机。





相关问题
ocamlc, module compilation

I wrote an app in ocaml. It consist of several modules: Util (util.ml) Work1 (work1.ml) -- open Util Work2 (work2.ml) -- open Util, too Main (main.ml) -- open all of them. When i compile its, using ...

How can I simplify this ocaml pattern-matching code?

I m writing a simple little ocaml program that reads an algebraic statement in from a file, parses it into an AST using ocamllex/ocamlyacc, reduces it, and then prints it. The part where I m reducing ...

How can I create a type with multiple parameters in OCaml?

I m trying to create a type that has multiple type parameters. I know how to make a type with one parameter: type a foo = a * int But I need to have two parameters, so that I can parameterize the ...

Hashtable indexed on several fields

I m currently programming an OCaml module defining a type corresponding to a CPU register. The interface of this module is the following : (* * Defines a type which represents a R3000 register. *) ...

Extending an existing type in OCaml

I ve been doing some OCaml programming lately to learn the language and to get more acquainted with functional programming. Recently, I ve started to think that I d like to be able to extend an ...

Ocaml Syntax Error

What s wrong with this code? I can t figure it out: let parent (rules : grammar) (symbol1 : string) (symbol2 : string) : (SymbolSet.t) = try SymbolSet.singleton (getParent [symbol1; symbol2] ...

热门标签