# HG changeset patch # User nipkow # Date 1671308784 -3600 # Node ID 059a68d21f0fee10206ec7f5739db1365341b48d # Parent 89f78f76df1ce6f2ba2107fedec001ddd5887d0f Tuned text diff -r 89f78f76df1c -r 059a68d21f0f src/Doc/Codegen/Foundations.thy --- a/src/Doc/Codegen/Foundations.thy Sat Dec 17 10:18:12 2022 +0100 +++ b/src/Doc/Codegen/Foundations.thy Sat Dec 17 21:26:24 2022 +0100 @@ -263,8 +263,9 @@ subsection \Explicit partiality \label{sec:partiality}\ text \ - Partiality usually enters the game by partial patterns, as - in the following example, again for amortised queues: + Explicit partiality is caused by missing patterns + (in contrast to partiality caused by nontermination, which is covered in Section~\ref{sec:partial}). + Here is an example, again for amortised queues: \ definition %quote strict_dequeue :: "'a queue \ 'a \ 'a queue" where diff -r 89f78f76df1c -r 059a68d21f0f src/Doc/Codegen/Partial_Functions.thy --- a/src/Doc/Codegen/Partial_Functions.thy Sat Dec 17 10:18:12 2022 +0100 +++ b/src/Doc/Codegen/Partial_Functions.thy Sat Dec 17 21:26:24 2022 +0100 @@ -1,10 +1,11 @@ theory Partial_Functions -imports Main "HOL-Library.Monad_Syntax" +imports Setup "HOL-Library.Monad_Syntax" begin section \Partial Functions \label{sec:partial}\ -text \We demonstrate three approaches to defining executable partial recursive functions. +text \We demonstrate three approaches to defining executable partial recursive functions, +i.e.\ functions that do not terminate for all inputs. The main points are the definitions of the functions and the inductive proofs about them. Our concrete example represents a typical termination problem: following a data structure that @@ -23,7 +24,7 @@ lemma finite_rel[simp]: "finite(rel ns)" (*<*)by(simp add: rel_def)(*>*) -text \This relation should be acyclic +text \\noindent This relation should be acyclic to guarantee termination of the partial functions defined below.\ @@ -83,7 +84,7 @@ "count ns n = (if n < length ns then do {k \ count ns (ns!n); Some (k+1)} else Some 0)" -text \We use a Haskell-like \do\ notation (import @{theory "HOL-Library.Monad_Syntax"}) +text \\noindent We use a Haskell-like \do\ notation (import @{theory "HOL-Library.Monad_Syntax"}) to abbreviate the clumsy explicit \noindent @@ -123,8 +124,13 @@ morphisms rep_acyc abs_acyc using acyclic_rel_Nil by auto -text \This defines two functions \rep_acyc :: acyc \ nat list\ and \abs_acyc :: nat list \ acyc\. -Function @{const abs_acyc} is only defined on acyclic lists and not executable for that reason.\ +text \\noindent This defines two functions @{term [source] "rep_acyc :: acyc \ nat list"} +and @{const abs_acyc} \::\ \mbox{@{typ "nat list \ acyc"}}. +Function @{const abs_acyc} is only defined on acyclic lists and not executable for that reason. +Type \dlist\ in Section~\ref{sec:partiality} is defined in the same manner. + +The following command sets up infrastructure for lifting functions on @{typ "nat list"} +to @{typ acyc} (used by @{command_def lift_definition} below) \cite{isabelle-isar-ref}.\ setup_lifting type_definition_acyc @@ -135,7 +141,7 @@ = (let ns = rep_acyc ac in if n < length ns then follow2 ac (ns!n) else n)" by pat_completeness auto -text \Now we prepare for termination proof. +text \Now we prepare for the termination proof. Relation \rel_follow2\ is almost identical to @{const rel_follow}.\ definition "rel_follow2 = same_fst (acyclic o rel o rep_acyc) (rel o rep_acyc)" @@ -188,7 +194,7 @@ "\ns. if acyclic(rel ns) then ns else []" (*<*)by (auto simp: acyclic_rel_Nil)(*>*) -text \This works because we can easily prove that for any \ns\, +text \\noindent This works because we can easily prove that for any \ns\, the \\\-term produces an acyclic list. But it requires the possibly expensive check @{prop "acyclic(rel ns)"}.\