Skip to content

Decreases for functions in datatypes #2257

Answered by MikaelMayer
gancherj asked this question in Q&A
Discussion options

You must be logged in to vote

I think the following works for you. Dafny does not compare functions and trees, so you need to provide the comparison explicitely.

datatype Tree_ = | Node(n : int) | Branch(f : bool -> Tree_) {
  predicate WellFormed() {
    match this {
      case Node(i) => true
      case Branch(f) => forall b: bool :: f(b) < this && f(b).WellFormed()
    }
  }
}

type Tree = t : Tree_ | t.WellFormed() witness Node(0)

function Size(t : Tree) : int
	decreases t
{
	match t
		case Node(_) => 0
		case Branch(f) => 1 + Size(f(false)) + Size(f(true)) // Error : decreases clause might not decrease
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by gancherj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2255 on June 15, 2022 15:40.