Documentation

PfsProgs25.Unit05.HCF

GCD: Termination example #

As an example of proving termination, we define the function hcf that computes the greatest common divisor of two natural numbers. We prove that hcf terminates by showing that the argument decreases in each recursive call.

@[irreducible]
def hcfSimple (a b : ) :

Simple version of the GCD function

Equations
Instances For
    @[irreducible]
    def hcf (a b : ) :

    The function hcf computes the greatest common divisor of two natural numbers. We have to prove that it terminates.

    Equations
    • hcf a b = if h : b = 0 then a else if h' : a < b then hcf b a else let_fun h₁ := ; let_fun this := ; let_fun this := ; hcf (a % b) b
    Instances For

      We cannot always make partial definitions.

      partial def not_wrong {α : Type} [Nonempty α] (x : ) :
      α

      A recursively defined function that loops forever but is a legal definition as its codomain is empty.