src/Doc/Codegen/Foundations.thy
changeset 68254 3a7f257dcac7
parent 61781 e1e6bb36b27a
child 68484 59793df7f853
equal deleted inserted replaced
68253:a8660a39e304 68254:3a7f257dcac7
   150 
   150 
   151   \noindent The current setup of the pre- and postprocessor may be inspected
   151   \noindent The current setup of the pre- and postprocessor may be inspected
   152   using the @{command_def print_codeproc} command.  @{command_def
   152   using the @{command_def print_codeproc} command.  @{command_def
   153   code_thms} (see \secref{sec:equations}) provides a convenient
   153   code_thms} (see \secref{sec:equations}) provides a convenient
   154   mechanism to inspect the impact of a preprocessor setup on code
   154   mechanism to inspect the impact of a preprocessor setup on code
   155   equations.
   155   equations.  Attribute @{attribute code_preproc_trace} allows
   156 \<close>
   156   for low-level tracing:
       
   157 \<close>
       
   158 
       
   159 declare %quote [[code_preproc_trace]]
       
   160 
       
   161 declare %quote [[code_preproc_trace only: distinct remdups]]
       
   162 
       
   163 declare %quote [[code_preproc_trace off]]
   157 
   164 
   158 
   165 
   159 subsection \<open>Understanding code equations \label{sec:equations}\<close>
   166 subsection \<open>Understanding code equations \label{sec:equations}\<close>
   160 
   167 
   161 text \<open>
   168 text \<open>
   280 text %quotetypewriter \<open>
   287 text %quotetypewriter \<open>
   281   @{code_stmts strict_dequeue (consts) strict_dequeue (Haskell)}
   288   @{code_stmts strict_dequeue (consts) strict_dequeue (Haskell)}
   282 \<close>
   289 \<close>
   283 
   290 
   284 text \<open>
   291 text \<open>
   285   \noindent In some cases it is desirable to have this
   292   \noindent In some cases it is desirable to state this
   286   pseudo-\qt{partiality} more explicitly, e.g.~as follows:
   293   pseudo-\qt{partiality} more explicitly, e.g.~as follows:
   287 \<close>
   294 \<close>
   288 
   295 
   289 axiomatization %quote empty_queue :: 'a
   296 axiomatization %quote empty_queue :: 'a
   290 
   297 
   291 definition %quote strict_dequeue' :: "'a queue \<Rightarrow> 'a \<times> 'a queue" where
   298 definition %quote strict_dequeue' :: "'a queue \<Rightarrow> 'a \<times> 'a queue" where
   292   "strict_dequeue' q = (case dequeue q of (Some x, q') \<Rightarrow> (x, q') | _ \<Rightarrow> empty_queue)"
   299   "strict_dequeue' q = (case dequeue q of (Some x, q') \<Rightarrow> (x, q')
       
   300     | _ \<Rightarrow> empty_queue)"
   293 
   301 
   294 lemma %quote strict_dequeue'_AQueue [code]:
   302 lemma %quote strict_dequeue'_AQueue [code]:
   295   "strict_dequeue' (AQueue xs []) = (if xs = [] then empty_queue
   303   "strict_dequeue' (AQueue xs []) = (if xs = [] then empty_queue
   296      else strict_dequeue' (AQueue [] (rev xs)))"
   304      else strict_dequeue' (AQueue [] (rev xs)))"
   297   "strict_dequeue' (AQueue xs (y # ys)) =
   305   "strict_dequeue' (AQueue xs (y # ys)) =
   299   by (simp_all add: strict_dequeue'_def split: list.splits)
   307   by (simp_all add: strict_dequeue'_def split: list.splits)
   300 
   308 
   301 text \<open>
   309 text \<open>
   302   Observe that on the right hand side of the definition of @{const
   310   Observe that on the right hand side of the definition of @{const
   303   "strict_dequeue'"}, the unspecified constant @{const empty_queue} occurs.
   311   "strict_dequeue'"}, the unspecified constant @{const empty_queue} occurs.
   304 
   312   An attempt to generate code for @{const strict_dequeue'} would
   305   Normally, if constants without any code equations occur in a
   313   make the code generator complain that @{const empty_queue} has
   306   program, the code generator complains (since in most cases this is
   314   no associated code equations.  In most situations unimplemented
   307   indeed an error).  But such constants can also be thought
   315   constants indeed indicated a broken program; however such
   308   of as function definitions which always fail,
   316   constants can also be thought of as function definitions which always fail,
   309   since there is never a successful pattern match on the left hand
   317   since there is never a successful pattern match on the left hand
   310   side.  In order to categorise a constant into that category
   318   side.  In order to categorise a constant into that category
   311   explicitly, use the @{attribute code} attribute with
   319   explicitly, use the @{attribute code} attribute with
   312   @{text abort}:
   320   @{text abort}:
   313 \<close>
   321 \<close>
   323   @{code_stmts strict_dequeue' (consts) empty_queue strict_dequeue' (Haskell)}
   331   @{code_stmts strict_dequeue' (consts) empty_queue strict_dequeue' (Haskell)}
   324 \<close>
   332 \<close>
   325 
   333 
   326 text \<open>
   334 text \<open>
   327   \noindent This feature however is rarely needed in practice.  Note
   335   \noindent This feature however is rarely needed in practice.  Note
   328   also that the HOL default setup already declares @{const undefined},
   336   that the HOL default setup already includes
   329   which is most likely to be used in such situations, as
   337 \<close>
   330   @{text "code abort"}.
   338 
       
   339 declare %quote [[code abort: undefined]]
       
   340 
       
   341 text \<open>
       
   342   \noindent -- hence @{const undefined} can always be used in such
       
   343   situations.
   331 \<close>
   344 \<close>
   332 
   345 
   333 
   346 
   334 subsection \<open>If something goes utterly wrong \label{sec:utterly_wrong}\<close>
   347 subsection \<open>If something goes utterly wrong \label{sec:utterly_wrong}\<close>
   335 
   348