Skip to content

Defining mutually recursive fixpoints as (definitional) type class instances #7913

Description

@ifazk

Version

8.8.0

Operating system

Linux

Description of the problem

Defining mutually recursive fixpoints as instances of definitional/singleton type classes is a little awkward right now. The following is an example that illustrates the problem.

Inductive bexp : Set :=
  | b_true : bexp
  | b_false : bexp
  | b_eq : aexp -> aexp -> bexp
with aexp : Set :=
     | a_of_bool : bexp -> aexp.

Class ContainsFalse (A : Set) :=
  contains_false : A -> bool.

Fixpoint bexp_ContainsFalse (b : bexp) :=
  match b with
  | b_true => false
  | b_false => true
  | b_eq a1 a2 => orb
                   (@contains_false aexp aexp_ContainsFalse a1)
                   (@contains_false aexp aexp_ContainsFalse a2)
  end with
aexp_ContainsFalse (a : aexp) :=
  match a with
  | a_of_bool b => (@contains_false bexp bexp_ContainsFalse b)
  end.

Instance BexpContainsFalse : ContainsFalse bexp := bexp_ContainsFalse.
Instance AexpContainsFalse : ContainsFalse aexp := aexp_ContainsFalse.

In the above code there are two awkward patterns. Firstly, I had to manually pass implicit parameters to contains_false, since I couldn't declare the instances a priori. Secondly, I had to rebind bexp_ContainsFalse and aexp_ContainsFalse to BexpContainsFalse and AexpContainsFalse so that I could declare them as instances.

Ideally I should be able to define instances with something like the following:

Fixpoint BexpContainsFalse (b : bexp) :=
  match b with
  | b_true => false
  | b_false => true
  | b_eq a1 a2 => orb (contains_false a1) (contains_false a2)
  end with
AexpContainsFalse (a : aexp) :=
  match a with
  | a_of_bool b => (contains_false b)
  end
with Instance BexpContainsFalse : ContainsFalse bexp
with Instance AexpContainsFalse : ContainsFalse aexp.

Could we extend coq's syntax for Fixpoint or Instance to support recursive singleton instances?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: design discussionDiscussion about the design of a feature.kind: enhancementEnhancement to an existing user-facing feature, tactic, etc.part: inductivesInductive types, fixpoints, etc.part: typeclassesThe typeclass mechanism.

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions