Skip to content

termination_by? code action unergonomic for mutual recursion #4546

Closed

Description

@nomeata

The termination_by? command suffix shows the inferred termination argument to the user and offers a “Try this” command to replace it. It works fine for non-mutual recursive function, but for mutual recursive functions, there is a problem.

Consider

mutual
  def foo  : Nat → Nat
  | 0 => 0
  | n+1 => bar n
  termination_by? -- Try this: termination_by x1 => x1

  def bar : Nat → Nat
  | 0 => 0
  | n+1 => foo n
end

If the user clicks on the Try this action, they get a broken file

mutual
  def foo  : Nat → Nat
  | 0 => 0
  | n+1 => bar n
  termination_by x1 => x1

  def bar : Nat → Nat -- Missing `termination_by`; this function is mutually
                      -- recursive with foo, which has a `termination_by` clause.
  | 0 => 0
  | n+1 => foo n
end

because (right now) either every or no function of a clique can have a termination_by annotation.

Possible ways forward:

  • Show the inferred termination measure for every function, and offer a code action that replaces
    all of them at once.

    This requires extending the Try this functionality.

  • If some functions have a termination_by and others don’t, ignore the termination_by (with a warning), so that the user can write

    mutual
      def foo  : Nat → Nat
      | 0 => 0
      | n+1 => bar n
      termination_by x1 => x1 -- Warning: ignored until every function has one
    
      def bar : Nat → Nat
      | 0 => 0
      | n+1 => foo n
      termination_by?
    end
    

    and still have GuessLex running and be able to click on termination_by?.

  • If some functions have a termination_by and others don’t, do not complain, run GuessLex as usual, and afterwards check that the inferred measure matches the given one.

    So termination_by can be used to assert the inferred measure and document it, and only if all functions in a clique have one, one gets the benefit of not running GuessLex at all.

  • If some functions have a termination_by and others don’t, do not complain, and run GuessLex in a way that extends the given termination argument to the other functions. (I don’t think this is easily possible or worth it, just listing for completeness.)

So far nobody complained, so this isn’t very pressing.

Versions

4.8.0

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Activity

  1. added
    bug
    on Jun 24, 2024
  2. added a commit that references this issue on Jul 1, 2024
    fb0c46a
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions