Problem with has type X but expected type unit

Hi,

I’m very new to ocaml. I’m working in a parser with menhir and got the following compiler error. Which I have no clue how to solve:

The function output_value gets the parsed abstract syntax tree and pretty prints it. I just tried to print Kind in the same fashion of List but the compiler throws the error below.

let rec output_value outc = function
| List l -> print_list outc l | String s → printf “"%s"” s
| Int i -> printf "INT %d" i | Null → printf “NULL”
| Kind k -> print_kind k | Ident id → printf “IDENT %s” id

ocamlbuild -use-menhir -tag thread -use-ocamlfind -quiet -pkg core metanetics.native

  • ocamlfind ocamlc -c -thread -package core -o nds.cmo nds.ml
    File “nds.ml”, line 20, characters 17-29:
    Error: This expression has type 'a * 'b * 'c * 'd
    but an expression was expected of type unit
    Command exited with code 2.

The code is available at File nds.ml, line 20, characters 17-29: · joaoepj/metanetics@a812ad9 · GitHub

The issue is in your definition of print_kind : f x, y , z, w is parsed as (f x), y , z , w. Also rather than matching on your tuple argument with let f tuple = match tuple with x,y,z,w -> …, you can write directly let f (x,y,z,w) = …. In a related note, the Fmt library provides nice combinators for writing pretty printers

2 Likes

Thank you,

The problem was the commas separating printf variables. As I understood when function returns no value its return type must be unit. With commas the function is wrong and the compiler consider function parameters as its result. I’ll check Fmt later.

let print_kind (id, i, r, b) = printf “KIND( id: %s, identify: %s, recognize: %s, behave: %s)” id i r b