OCaml错误:构造函数中的表达式类型错误

OCaml错误:构造函数中的表达式类型错误,ocaml,Ocaml,我有一个函数save,它接受标准输入,单独使用时如下所示: ./try < input.txt (* save function is in try file *) 现在我把这个函数放到另一个名为path.ml的文件中,它是我的解释器的一部分。现在我在定义Save函数的类型时遇到了一个问题,这是因为Save函数在_通道中有类型,但是当我写 type term = Save of in_channel ocamlc抱怨命令函数中的参数 如何修复此错误?这就是为什么在我在stackover

我有一个函数
save
,它接受标准输入,单独使用时如下所示:

./try < input.txt (* save function is in try file *)
现在我把这个函数放到另一个名为
path.ml的文件中,它是我的解释器的一部分。现在我在定义Save函数的类型时遇到了一个问题,这是因为Save函数在_通道中有类型,但是当我写

type term = Save of in_channel
ocamlc抱怨
命令
函数中的参数

如何修复此错误?这就是为什么在我在stackoverflow上发布的最后一个问题中,我询问如何表达一个接受任何类型的变量。我理解答案,但实际上这对代码的运行没有多大帮助

这是我的代码:

(* Data types *)

open Printf
type term = Print_line_in_file of int*string
        | Print of string       
        | Save of in_channel  (* error here *)  
;;


let input_line_opt ic =
  try Some (input_line ic)
  with End_of_file -> None

let nth_line n filename =
  let ic = open_in filename in
  let rec aux i =
    match input_line_opt ic with
      | Some line ->
          if i = n then begin
            close_in ic;
            (line)
          end else aux (succ i)
      | None ->
          close_in ic;
          failwith "end of file reached"
  in
    aux 1

(* get all lines *)
let k = ref 1
let first = ref ""
let second = ref ""
let sequence = ref []
let append_item lst a = lst @ [a]

let save () = 
    try
        while true do
            let line = input_line stdin in
                if k = ref 1
                    then
                    begin
                        first := line;
                        incr k;
                    end else 
                if k = ref 2
                    then
                    begin
                        second := line;
                        incr k;
                    end else                
                    begin
                        sequence := append_item !sequence line;
                        incr k;
                    end
            done;
        None
    with
End_of_file -> None;;



let rec command term = match term with
    | Print (n) -> print_endline n
    | Print_line_in_file (n, f) -> print_endline (nth_line n f)
    | Save () -> save ()
;;
编辑

代码错误:

在U通道中保存:

Error: This pattern matches values of type unit
       but a pattern was expected which matches values of type in_channel
保存单位:

Error: This expression has type 'a option
       but an expression was expected of type unit

这段代码中有很多错误,所以很难知道从哪里开始

一个问题是:您的
save
函数具有类型
unit->'选项
。因此,它与final
match
的其他分支的类型不同。修复方法很简单:
save
应该返回
()
,而不是
None
。在OCaml中,这些是完全不同的事情

眼前的问题似乎是,您的匹配中有
Save()
,但已声明
Save
使用输入通道。您当前的代码无法将输入通道传递到
save
功能,但如果它传递了,您可能希望在匹配中使用类似的功能:

| Save ch -> save ch

像这样的错误(对我来说)表明您不太熟悉OCaml的类型系统。如果您在编写更多代码之前阅读了某种教程,可能会为您节省很多麻烦。您可以在上找到教程。

请编辑您的文章,并显示准确的错误消息。我刚刚添加了它!非常感谢。如果您使用ocamlc以正常方式编译此代码,那么它将告诉您发现错误的行和列,这将使调试更加容易。(如果你已经这样做了,请在问题中包含这些信息。)我不明白你想用这个
保存
做什么。你能解释一下吗?因为这个path.ml文件是我解释器的一部分。关键字在lexer中定义,关键字的标记在解析器中定义,现在我希望save关键字可以执行一个操作,即读取stdin。因此,save文件执行此任务,但使用我的lexer中定义的另一个关键字。
| Save ch -> save ch