Isabelle 在包含自身映射的数据类型上终止函数

问题描述

Isabelle 是否可以定义一个终止递归函数 f where

  • f一个 t 类型的参数,这样 t 类型的值可以包含到 t 类型的值的映射,并且
  • f 对此类地图范围内的所有元素执行递归调用

例如考虑理论Trie_Fun中定义的数据类型trie

datatype 'a trie = Nd bool "'a ⇒ 'a trie option"

以及我尝试使用一个简单的函数 height 来计算尝试的高度(具有有限多的出边):

theory Scratch
  imports "HOL-Data_Structures.Trie_Fun"
begin

function height :: "'a trie ⇒ nat" where
  "height (Nd _ edges) = (if dom edges = Set.empty ∨ ¬ finite (dom edges)
    then 0
    else 1 + Max (height ` ran edges))"
  by pat_completeness auto
termination (* ??? *)

end

此处 lexicographic_order 不足以证明该函数是终止的,但到目前为止,我还无法在 trie(用于终止)上制定任何本身不需要类似的度量递归。 在此我必须承认,我不确定我是否正确理解了 Isabelle/HOL 中的数据类型(即,上述定义的 trie 是否实际上总是有限高度)。

是否可以证明 height 终止?

解决方法

根据 Peter Zeller 的评论,我能够通过在定义中添加 height 然后使用事实 (domintros)trie 进行归纳来证明 height.domintros 的终止1}},产生以下终止证明:

function (domintros) height :: "'a trie ⇒ nat" where
  "height (Nd _ edges) = (if dom edges = Set.empty ∨ ¬ finite (dom edges)
    then 0
    else 1 + Max (height ` ran edges))"
  by pat_completeness auto
termination apply auto
proof -
  fix x :: "'a trie"
  show "height_dom x"
  proof (induction)
    case (Nd b edges)

    have "(⋀x. x ∈ ran edges ⟹ height_dom x)"
    proof -
      fix x assume "x ∈ ran edges" 
      then have "∃a. edges a = Some x"
        unfolding ran_def by blast
      then have "∃a. Some x = edges a"
        by (metis (no_types))
      then have "Some x ∈ range edges"
        by blast
      then show "height_dom x"
        using Nd by auto
    qed
    then show ?case
      using height.domintros by blast
  qed
qed