Syntax Ocaml类型不匹配?应为单位,但为已定义类型
我得到下面的错误,我无法理解为什么会出现这种情况。感谢您的帮助。该程序是Ocaml中的一个解释器。由tryOcaml加下划线的代码段是“processor tokens[];;”代码的最后一位,不确定为什么会出现类型不匹配,或者不匹配发生在代码中的什么位置 错误:此表达式具有stackValue列表类型 但应为unit类型的表达式Syntax Ocaml类型不匹配?应为单位,但为已定义类型,syntax,compiler-errors,functional-programming,ocaml,Syntax,Compiler Errors,Functional Programming,Ocaml,我得到下面的错误,我无法理解为什么会出现这种情况。感谢您的帮助。该程序是Ocaml中的一个解释器。由tryOcaml加下划线的代码段是“processor tokens[];;”代码的最后一位,不确定为什么会出现类型不匹配,或者不匹配发生在代码中的什么位置 错误:此表达式具有stackValue列表类型 但应为unit类型的表达式 type stackValue = NUM of int | BOOL of bool | ERROR | STRING of string | NAME of st
type stackValue = NUM of int | BOOL of bool | ERROR | STRING of string | NAME of string | UNIT
type com = PUSH of stackValue | POP | ADD | SUB | MUL | DIV | REM | NEG | SWAP | QUIT | TOSTRING | PRINTLN (*| CAT | AND | OR | NOT | LESSTHAN | EQUAL | IF | BIND let com {com} end | funBind com {com} [return] funEnd | call*)
let interpreter ( (inFile : string), (outFile : string )) : unit =
let ic = open_in inFile
in
let oc = open_out outFile
in
let rec loop_read acc =
try
let l = String.trim(input_line ic) in loop_read (l::acc)
with
| End_of_file -> List.rev acc
in
let ls_str = loop_read []
in
let checkrest s =
match s.[0] with
| '"' -> if s.[(String.length s) -1]='"' then STRING(String.sub s 1 ((String.length s)-2)) else ERROR
| '-' | '0'..'9' -> (try NUM(int_of_string s) with _ ->ERROR)
| '_' | 'a'..'z' | 'A'..'Z' -> NAME(s)
|_ -> ERROR
in
let str2sv s =
match s with
|":true:" -> BOOL(true)
|":false:" -> BOOL(false)
|":unit:" -> UNIT
|":error:" -> ERROR
|_-> checkrest s
in
let str2com s =
match s with
|"quit" -> QUIT
|"add" -> ADD
|"sub" -> SUB
|"mul" -> MUL
|"div" -> DIV
|"rem" -> REM
|"pop" -> POP
|"neg" -> NEG
|"swap" -> SWAP
(*|"cat" -> CAT
|"and" -> AND
|"or" -> OR
|"not" -> NOT
|"lessthan" -> LESSTHAN
|"equal" -> EQUAL
|"if" -> IF
|"bind" -> BIND*)
|"toString" -> TOSTRING
|_ -> if String.sub s 0 4 = "push" then let x = str2sv (String.sub s 5 ((String.length s) -5)) in PUSH(x) else PUSH(ERROR)
in
let tokens = List.map str2com ls_str
in
let sv2str sv =
match sv with
|BOOL(true) -> STRING(":true:")
|BOOL(false) -> STRING(":false:")
|UNIT -> STRING(":unit:")
|ERROR -> STRING(":error:")
|STRING(s) -> STRING(s)
|NAME(s) -> STRING(s)
|NUM(x) -> STRING(string_of_int(x))
in
let file_write value = Printf.fprintf oc "%s\n" value
in
let rec processor comlist stack =
match (comlist,stack) with
|(PUSH(x)::comst, stack) -> processor comst (x::stack)
|(POP::comst, stackValue::reststack) -> processor comst reststack
|(POP::comst, []) -> processor comst (ERROR::stack)
|(ADD::comst, NUM(a)::NUM(b)::reststack) -> processor comst (NUM(a+b)::reststack)
|(ADD::comst, stack) -> processor comst (ERROR::stack)
|(SUB::comst, NUM(a)::NUM(b)::reststack) -> processor comst (NUM(b-a)::reststack)
|(SUB::comst, stack) -> processor comst (ERROR::stack)
|(MUL::comst, NUM(a)::NUM(b)::reststack) -> processor comst (NUM(a*b)::reststack)
|(MUL::comst, stack) -> processor comst (ERROR::stack)
|(DIV::comst, NUM(a)::NUM(b)::reststack) -> if (NUM(a)=NUM(0)) then (processor comst (ERROR::NUM(a)::NUM(b)::reststack)) else (processor comst (NUM(b/a)::reststack))
|(DIV::comst, stack) -> processor comst (ERROR::stack)
|(REM::comst, NUM(a)::NUM(b)::reststack) -> if (NUM(a)=NUM(0)) then (processor comst (ERROR::NUM(a)::NUM(b)::reststack)) else (processor comst (NUM(b mod a)::reststack))
|(REM::comst, stack) -> processor comst (ERROR::stack)
|(NEG::comst, NUM(a)::reststack) -> processor comst (NUM(-a)::reststack)
|(NEG::comst, stack) -> processor comst (ERROR::stack)
|(SWAP::comst, x::xs::reststack) -> processor comst (xs::x::reststack)
|(SWAP::comst, stack) -> processor comst (ERROR::stack)
|(TOSTRING::comst, x::reststack) -> let s = sv2str x in processor comst (s::reststack)
|(TOSTRING::comst, []) -> processor comst (ERROR::stack)
|(PRINTLN::comst, x::reststack) -> (match x with
|STRING(x) -> file_write x; processor comst reststack
|_ -> ERROR::stack)
|(PRINTLN::comst, []) -> processor comst (ERROR::stack)
|(QUIT::comst, stack) -> []
|(_,_) -> []
in
processor tokens [];;
让我最小化您的代码以突出问题
let interpreter ( (inFile : string), (outFile : string )) : unit =
... <snip> ...
let rec processor comlist stack =
match (comlist,stack) with
|(PUSH(x)::comst, stack) -> processor comst (x::stack)
|(QUIT::comst, stack) -> []
... <snip> ...
|(_,_) -> []
in
processor tokens []
其中,ignore
是一个简单的函数,大致有以下实现:
let ignore x = ()
i、 例如,它只是忽略它的参数,因此它具有类型'a->unit
。您也可以编写
let _stack = processor tokens [] in
()
因此,您忽略返回的堆栈(惯例是使用underscope作为忽略值的前缀),并返回
()
值,而不是\u堆栈
您不明白的具体内容是什么?您指定解释器
函数应返回单元
,但其中的最后一个表达式是调用处理器
函数,该函数返回一个堆栈值列表
。因此类型不匹配。我想如何返回单位?我不知道如何返回一个单元,我真的不需要这个单元做任何事情,因为这个程序的功能就像一个空函数,并且在处理器内部处理所有事情。我对Ocaml非常陌生,这种功能不是我的强项。因此,忽略是否只是执行函数,然后在括号内扔掉函数的返回?我把它放进去,那个错误就消失了,但我还有一个错误。“错误:非法字符(\r)”我昨晚看到了这个,但我不知道我做了什么来修复它。对ignore很有帮助,从不知道它是一个东西是的,ignore
计算它的表达式,然后基本上丢弃结果。如果你还没有弄明白你的新问题,你应该提出一个新问题。仅仅从错误消息无法判断发生了什么。一般来说,在源代码中包含'\r'
(回车)并不违法。所以上下文很重要,你可以这样认为,但它只是一个函数。OCaml是一种热切的语言,每次编写函数应用程序时,例如,print\u int 42
都会立即执行,无论它是传递给另一个函数还是被忽略。基本上,OCaml机制(或学者演讲中的语义)是执行所有函数应用程序,直到什么都不剩:)我用更多细节更新了答案。
let _stack = processor tokens [] in
()