【发布时间】:2015-08-06 14:56:46
【问题描述】:
我正在编写一个 ocaml 程序来处理基本的算术命令和符号数学命令。但是,该代码目前给了我一个奇怪的类型错误。我觉得可能会出现这种情况,因为有两种不同的变体使用 binop 类型,但我不确定。
open Core.Std
type binop =
|Add
|Subtract
|Multiply
|Divide
let apply_binop_int a b = function
|Add -> a + b
|Subtract -> a - b
|Multiply -> a * b
|Divide -> a / b
let rec apply_binop a b op =
match (a,b) with
|ExprTerm (Const a), ExprTerm (Const b) -> apply_binop_int a b op
|BinopExpr (op1,a1,b1),_ -> apply_binop (apply_binop a1 b1 op1) b op
|_,BinopExpr (op1,a1,b1) -> apply_binop a (apply_binop a1 b1 op1) op
let precedence = function
|Add |Subtract -> 0
|Multiply |Divide -> 1
type term =
|Const of int
|Var of string
type token =
|Term of term
|Operator of binop
type expr =
|ExprTerm of term
|BinopExpr of binop * expr * expr
let operator_of_string = function
|"+" -> Add
|"-" -> Subtract
|"*" -> Multiply
|"/" -> Divide
|_ -> failwith "Unidentified operator"
let token_of_string s =
try Term (Const (int_of_string s))with
|_ -> Operator (operator_of_string s)
let tokens s =
String.split ~on:' ' s
|> List.map ~f:token_of_string
let process_operator ops exprs =
match (ops,exprs) with
|op::op_tl,b::a::expr_tl -> op_tl,(BinopExpr (op,a,b))::expr_tl
|_,_ -> failwith "Malformed expression"
let rec pop_stack (ops,exprs) =
match (ops,exprs) with
|_::_, _::_::_ -> pop_stack (process_operator ops exprs)
|_,[x] -> x
|_,_ -> failwith "Malformed expression"
let build_expr ts =
let rec aux ops exprs toks =
match (toks,ops) with
|Term t::tl,_ -> aux ops ((ExprTerm t)::exprs) tl
|Operator op2::tl,op::_ when precedence op >= precedence op2 ->
let ops,exprs = process_operator ops exprs in
aux ops exprs toks
|Operator op::tl,_ -> aux (op::ops) exprs tl
|[],_ -> pop_stack (ops,exprs) in
aux [] [] ts
let expr s = build_expr (tokens s)
let rec eval = function
|BinopExpr (op,a,b) ->
apply_binop (eval a) (eval b) op
|ExprTerm t -> t
我得到的错误:
utop # #use "calc.ml";;
type binop = Add | Subtract | Multiply | Divide
val apply_binop_int : int -> int -> binop -> int = <fun>
File "calc.ml", line 18, characters 63-66:
Error: This expression has type binop/1405061 but an expression was expected of type
binop/1740597
【问题讨论】:
标签: types ocaml calculator variant