/-
Copyright (c) 2026 Terence Rokop. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Terence Rokop
-/modulepublicimportMathlib.Data.W.BasicpublicimportGeb.Mathlib.Data.FinEnummetaimportGebMeta-- shake: keep; supplies the cite docstring roleset_optiondoc.versotrue
2.1.1. The W-type fold and paramorphism: computation rules and uniqueness🔗
WType.elim is the non-dependent fold of a W-type: the morphism
into a given algebra of the polynomial endofunctor
X ↦ Σ a, β a → X. mathlib states the fold but neither its
computation rule as a named @[simp] lemma nor its uniqueness.
Together the two make WType β the initial algebra of that
endofunctor, stated concretely. WType.para generalises the fold
to a paramorphism, whose step additionally sees each node's children as
subtrees, not only as their folded values.
elim_mk holds by rfl; mathlib's equation compiler
generates the equation, and the named @[simp] form is what makes
the hypothesis shape of elim_unique usable by simp at
call sites. elim_unique drives its recursion through an explicit
WType.rec application into a Prop-valued motive.
para is WType.elim at the product carrier
WType β × γ, whose first component reconstructs the subtree;
its computation rule para_mk does not hold by rfl and is
proved through that reconstruction.
[GambinoHyland2004]Nicola Gambino, Martin Hyland (2004). “Wellfounded Trees and Dependent Polynomial Functors”. Types for Proofs and Programs (TYPES 2003) 3085, pp. 210–225.
[Meertens1992]Lambert Meertens (1992). “Paramorphisms”. Formal Aspects of Computing 4(5), pp. 413–424. https://doi.org/10.1007/BF01211391.
The fold is the unique function satisfying its computation rule.
With WType.elim itself, this is the initiality of WType β
among algebras of the polynomial endofunctor X ↦ Σ a, β a → X.
The algebra of the paramorphism fold: rebuild the node from the
children's reconstructed subtrees, and apply the step to the node.
@[`@[expose]` has no effect outside a `module` fileexpose]defparaStep{α:TypeuA}{β:α→TypeuB}(γ:TypeuC)(fγ:(Σa:α,βa→WTypeβ×γ)→γ):(Σa:α,βa→WTypeβ×γ)→WTypeβ×γ:=funx↦(mkx.1funb↦(x.2b).1,fγx)
The fold's first component reconstructs its input.
The paramorphism of a W-type: a fold whose step sees each node's
children as subtrees together with their folded values. Obtained from
WType.elim at the product carrier WType β × γ, whose
first component reconstructs the subtree, so no new recursion is
introduced. [Meertens1992]Lambert Meertens (1992). “Paramorphisms”. Formal Aspects of Computing 4(5), pp. 413–424. https://doi.org/10.1007/BF01211391.
@[`@[expose]` has no effect outside a `module` fileexpose]defpara{α:TypeuA}{β:α→TypeuB}(γ:TypeuC)(fγ:(Σa:α,βa→WTypeβ×γ)→γ):WTypeβ→γ:=funw↦(elim(WTypeβ×γ)(paraStepγfγ)w).2
The paramorphism's computation rule: it applies the step to the node's
children paired with their own paramorphisms. Unlike WType.elim_mk
this does not hold by rfl; it is paraStep_fst under a
congrArg.
Boolean equality of W-trees: compare the shapes, then compare the
children pointwise over the finite direction type. A fold by
WType.elim at the carrier WType β → Bool, so no recursion
is introduced.
@[`@[expose]` has no effect outside a `module` fileexpose]defbeq{α:TypeuA}{β:α→TypeuB}[DecidableEqα][∀a,FinEnum(βa)]:WTypeβ→WTypeβ→Bool:=elim(WTypeβ→Bool)funxt'↦ifh:x.1=(toSigmat').1thendecide(∀b:βx.1,x.2b((toSigmat').2(h▸b))=true)elsefalse
WType.beq unfolded on two constructor applications.
Equality of W-trees is decidable when shapes have decidable equality
and every direction type is finitely enumerable. mathlib reaches this
only through Encodable, which additionally requires the shape and
direction types countable.