2

(Beginner Coq question)

Related to Defining recursive function over product type, I'm trying to define a recursive function over a product type. The difference here is there's a mutually recursive definition. I keep running into this error:

Recursive definition of printObjItem is ill-formed.

Recursive call to printJson has principal argument equal to "val" instead of a subterm of "item".

Conceptually it seems the recursion should go through since val is a subterm of item, is a subterm of items, is a subterm of x. I understand Coq is struggling with that first assertion but I'm not sure how to resolve. Is there a straightforward way without an explicit well-foundedness proof?

Require Import List.
Require Import String.
Import ListNotations.

Inductive Json :=
  | Atom : Json
  | String : string -> Json
  | Array : nat -> list Json -> Json
  | Object : list (string * Json) -> Json.

Fixpoint printJson (x : Json) :=
  match x with
  | Atom => "atom"
  | String n => "'" ++ n ++ "'"
  | Array _ els => "[" ++ (String.concat ", " (map printJson els)) ++ "]"
  | Object items => "{" ++ (String.concat ", " (map printObjItem items)) ++ "}"
  end%string
with printObjItem (item : string * Json) :=
       let (key, val) := item in key ++ ": " ++ (printJson val).

1 Answer 1

2

One solution could be to make printObjItem a local definition:

Fixpoint printJson (x : Json) :=
  let printObjItem (item : string * Json) :=
    (let (key, val) := item in key ++ ": " ++ (printJson val))%string
  in
  match x with
  | Atom => "atom"
  | String n => "'" ++ n ++ "'"
  | Array _ els => "[" ++ (String.concat ", " (map printJson els)) ++ "]"
  | Object items => "{" ++ (String.concat ", " (map printObjItem items)) ++ "}"
  end%string.
2
  • 1
    This worked! Any intuition around why this works and the mutual recursion doesn't?
    – Felipe
    Commented Dec 4, 2019 at 23:37
  • Mutual recursion is used for mutually inductive types. printObjItem only refers to printJson, so either inlining it or making a local definition should work. Hope it helps. Commented Dec 5, 2019 at 6:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.