src/Doc/Datatypes/Datatypes.thy
changeset 55351 8d7031a35991
parent 55350 cf34abe28209
child 55353 12603cbaa844
equal deleted inserted replaced
55350:cf34abe28209 55351:8d7031a35991
   110 
   110 
   111 \item Section \ref{sec:defining-corecursive-functions}, ``Defining Corecursive
   111 \item Section \ref{sec:defining-corecursive-functions}, ``Defining Corecursive
   112 Functions,'' describes how to specify corecursive functions using the
   112 Functions,'' describes how to specify corecursive functions using the
   113 @{command primcorec} and @{command primcorecursive} commands.
   113 @{command primcorec} and @{command primcorecursive} commands.
   114 
   114 
   115 \item Section \ref{sec:registering-bounded-natural-functors}, ``Registering
   115 \item Section \ref{sec:introducing-bounded-natural-functors}, ``Introducing
   116 Bounded Natural Functors,'' explains how to use the @{command bnf} command
   116 Bounded Natural Functors,'' explains how to use the @{command bnf} command
   117 to register arbitrary type constructors as BNFs.
   117 to register arbitrary type constructors as BNFs.
   118 
   118 
   119 \item Section
   119 \item Section
   120 \ref{sec:deriving-destructors-and-theorems-for-free-constructors},
   120 \ref{sec:deriving-destructors-and-theorems-for-free-constructors},
   327 @{typ 'b} is live.
   327 @{typ 'b} is live.
   328 
   328 
   329 Type constructors must be registered as BNFs to have live arguments. This is
   329 Type constructors must be registered as BNFs to have live arguments. This is
   330 done automatically for datatypes and codatatypes introduced by the @{command
   330 done automatically for datatypes and codatatypes introduced by the @{command
   331 datatype_new} and @{command codatatype} commands.
   331 datatype_new} and @{command codatatype} commands.
   332 Section~\ref{sec:registering-bounded-natural-functors} explains how to register
   332 Section~\ref{sec:introducing-bounded-natural-functors} explains how to
   333 arbitrary type constructors as BNFs.
   333 register arbitrary type constructors as BNFs.
   334 
   334 
   335 Here is another example that fails:
   335 Here is another example that fails:
   336 *}
   336 *}
   337 
   337 
   338     datatype_new 'a pow_list = PNil 'a (*<*)'a
   338     datatype_new 'a pow_list = PNil 'a (*<*)'a
   339     datatype_new 'a pow_list' = PNil' 'a (*>*)| PCons "('a * 'a) pow_list"
   339     datatype_new 'a pow_list' = PNil' 'a (*>*)| PCons "('a * 'a) pow_list"
   340 
   340 
   341 text {*
   341 text {*
   342 \noindent
   342 \noindent
   343 This one features a different flavor of nesting, where the recursive call in the
   343 This attempted definition features a different flavor of nesting, where the
   344 type specification occurs around (rather than inside) another type constructor.
   344 recursive call in the type specification occurs around (rather than inside)
       
   345 another type constructor.
   345 *}
   346 *}
   346 
   347 
   347 subsubsection {* Auxiliary Constants and Properties
   348 subsubsection {* Auxiliary Constants and Properties
   348   \label{sssec:datatype-auxiliary-constants-and-properties} *}
   349   \label{sssec:datatype-auxiliary-constants-and-properties} *}
   349 
   350 
   375       null: Nil (defaults tl: Nil)
   376       null: Nil (defaults tl: Nil)
   376     | Cons (hd: 'a) (tl: "'a list")
   377     | Cons (hd: 'a) (tl: "'a list")
   377 
   378 
   378 text {*
   379 text {*
   379 \noindent
   380 \noindent
       
   381 The introduced constants are listed below.
       
   382 
       
   383 \medskip
   380 
   384 
   381 \begin{tabular}{@ {}ll@ {}}
   385 \begin{tabular}{@ {}ll@ {}}
   382 Constructors: &
   386 Constructors: &
   383   @{text "Nil \<Colon> 'a list"} \\
   387   @{text "Nil \<Colon> 'a list"} \\
   384 &
   388 &
   395   @{text "map \<Colon> ('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list"} \\
   399   @{text "map \<Colon> ('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list"} \\
   396 Relator: &
   400 Relator: &
   397   @{text "list_all2 \<Colon> ('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> bool"}
   401   @{text "list_all2 \<Colon> ('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> bool"}
   398 \end{tabular}
   402 \end{tabular}
   399 
   403 
       
   404 \medskip
       
   405 
   400 The discriminator @{const null} and the selectors @{const hd} and @{const tl}
   406 The discriminator @{const null} and the selectors @{const hd} and @{const tl}
   401 are characterized as follows:
   407 are characterized by the following conditional equations:
   402 %
   408 %
   403 \[@{thm list.collapse(1)[of xs, no_vars]}
   409 \[@{thm list.collapse(1)[of xs, no_vars]}
   404   \qquad @{thm list.collapse(2)[of xs, no_vars]}\]
   410   \qquad @{thm list.collapse(2)[of xs, no_vars]}\]
   405 %
   411 %
   406 For two-constructor datatypes, a single discriminator constant is sufficient.
   412 For two-constructor datatypes, a single discriminator constant is sufficient.
   465     (@{syntax dt_name} '=' (@{syntax ctor} + '|') + @'and')
   471     (@{syntax dt_name} '=' (@{syntax ctor} + '|') + @'and')
   466   ;
   472   ;
   467   @{syntax_def dt_options}: '(' (('no_discs_sels' | 'no_code' | 'rep_compat') + ',') ')'
   473   @{syntax_def dt_options}: '(' (('no_discs_sels' | 'no_code' | 'rep_compat') + ',') ')'
   468 \<close>}
   474 \<close>}
   469 
   475 
       
   476 \medskip
       
   477 
       
   478 \noindent
   470 The syntactic entity \synt{target} can be used to specify a local
   479 The syntactic entity \synt{target} can be used to specify a local
   471 context---e.g., @{text "(in linorder)"}. It is documented in the Isar reference
   480 context---e.g., @{text "(in linorder)"}. It is documented in the Isar reference
   472 manual \cite{isabelle-isar-ref}.
   481 manual \cite{isabelle-isar-ref}.
   473 %
   482 %
   474 The optional target is potentially followed by datatype-specific options:
   483 The optional target is potentially followed by datatype-specific options:
   500   @{syntax_def tyargs}: typefree | '(' ((name ':')? typefree + ',') ')'
   509   @{syntax_def tyargs}: typefree | '(' ((name ':')? typefree + ',') ')'
   501   ;
   510   ;
   502   @{syntax_def map_rel}: '(' ((('map' | 'rel') ':' name) +) ')'
   511   @{syntax_def map_rel}: '(' ((('map' | 'rel') ':' name) +) ')'
   503 \<close>}
   512 \<close>}
   504 
   513 
       
   514 \medskip
       
   515 
   505 \noindent
   516 \noindent
   506 The syntactic entity \synt{name} denotes an identifier, \synt{typefree}
   517 The syntactic entity \synt{name} denotes an identifier, \synt{typefree}
   507 denotes fixed type variable (@{typ 'a}, @{typ 'b}, \ldots), and \synt{mixfix}
   518 denotes fixed type variable (@{typ 'a}, @{typ 'b}, \ldots), and \synt{mixfix}
   508 denotes the usual parenthesized mixfix notation. They are documented in the Isar
   519 denotes the usual parenthesized mixfix notation. They are documented in the Isar
   509 reference manual \cite{isabelle-isar-ref}.
   520 reference manual \cite{isabelle-isar-ref}.
   539 constructors as long as they share the same type.
   550 constructors as long as they share the same type.
   540 
   551 
   541 @{rail \<open>
   552 @{rail \<open>
   542   @{syntax_def dt_sel_defaults}: '(' 'defaults' (name ':' term +) ')'
   553   @{syntax_def dt_sel_defaults}: '(' 'defaults' (name ':' term +) ')'
   543 \<close>}
   554 \<close>}
       
   555 
       
   556 \medskip
   544 
   557 
   545 \noindent
   558 \noindent
   546 Given a constructor
   559 Given a constructor
   547 @{text "C \<Colon> \<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<sigma>"},
   560 @{text "C \<Colon> \<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<sigma>"},
   548 default values can be specified for any selector
   561 default values can be specified for any selector
   563 
   576 
   564 @{rail \<open>
   577 @{rail \<open>
   565   @@{command datatype_new_compat} names
   578   @@{command datatype_new_compat} names
   566 \<close>}
   579 \<close>}
   567 
   580 
   568 \noindent
   581 \medskip
   569 The old datatype package provides some functionality that is not yet replicated
   582 
   570 in the new package:
   583 \noindent
       
   584 The old datatype package provides some functionality that is not yet
       
   585 replicated in the new package:
   571 
   586 
   572 \begin{itemize}
   587 \begin{itemize}
   573 \setlength{\itemsep}{0pt}
   588 \setlength{\itemsep}{0pt}
   574 
   589 
   575 \item It is integrated with \keyw{fun} and \keyw{function}
   590 \item It is integrated with \keyw{fun} and \keyw{function}
  1003 
  1018 
  1004 In the other direction, there is currently no way to register old-style
  1019 In the other direction, there is currently no way to register old-style
  1005 datatypes as new-style datatypes. If the goal is to define new-style datatypes
  1020 datatypes as new-style datatypes. If the goal is to define new-style datatypes
  1006 with nested recursion through old-style datatypes, the old-style
  1021 with nested recursion through old-style datatypes, the old-style
  1007 datatypes can be registered as a BNF
  1022 datatypes can be registered as a BNF
  1008 (Section~\ref{sec:registering-bounded-natural-functors}). If the goal is
  1023 (Section~\ref{sec:introducing-bounded-natural-functors}). If the goal is
  1009 to derive discriminators and selectors, this can be achieved using @{command
  1024 to derive discriminators and selectors, this can be achieved using @{command
  1010 wrap_free_constructors}
  1025 wrap_free_constructors}
  1011 (Section~\ref{sec:deriving-destructors-and-theorems-for-free-constructors}).
  1026 (Section~\ref{sec:deriving-destructors-and-theorems-for-free-constructors}).
  1012 *}
  1027 *}
  1013 
  1028 
  1579 @{rail \<open>
  1594 @{rail \<open>
  1580   @@{command codatatype} target? \<newline>
  1595   @@{command codatatype} target? \<newline>
  1581     (@{syntax dt_name} '=' (@{syntax ctor} + '|') + @'and')
  1596     (@{syntax dt_name} '=' (@{syntax ctor} + '|') + @'and')
  1582 \<close>}
  1597 \<close>}
  1583 
  1598 
       
  1599 \medskip
       
  1600 
  1584 \noindent
  1601 \noindent
  1585 Definitions of codatatypes have almost exactly the same syntax as for datatypes
  1602 Definitions of codatatypes have almost exactly the same syntax as for datatypes
  1586 (Section~\ref{ssec:datatype-command-syntax}). The @{text "no_discs_sels"} option
  1603 (Section~\ref{ssec:datatype-command-syntax}). The @{text "no_discs_sels"} option
  1587 is not available, because destructors are a crucial notion for codatatypes.
  1604 is not available, because destructors are a crucial notion for codatatypes.
  1588 *}
  1605 *}
  2244   @{syntax_def pcr_option}: '(' ('sequential' | 'exhaustive') ')'
  2261   @{syntax_def pcr_option}: '(' ('sequential' | 'exhaustive') ')'
  2245   ;
  2262   ;
  2246   @{syntax_def pcr_formula}: thmdecl? prop (@'of' (term * ))?
  2263   @{syntax_def pcr_formula}: thmdecl? prop (@'of' (term * ))?
  2247 \<close>}
  2264 \<close>}
  2248 
  2265 
       
  2266 \medskip
       
  2267 
       
  2268 \noindent
  2249 The optional target is potentially followed by a corecursion-specific option:
  2269 The optional target is potentially followed by a corecursion-specific option:
  2250 
  2270 
  2251 \begin{itemize}
  2271 \begin{itemize}
  2252 \setlength{\itemsep}{0pt}
  2272 \setlength{\itemsep}{0pt}
  2253 
  2273 
  2281 partial_function to the rescue
  2301 partial_function to the rescue
  2282 *}
  2302 *}
  2283 *)
  2303 *)
  2284 
  2304 
  2285 
  2305 
  2286 section {* Registering Bounded Natural Functors
  2306 section {* Introducing Bounded Natural Functors
  2287   \label{sec:registering-bounded-natural-functors} *}
  2307   \label{sec:introducing-bounded-natural-functors} *}
  2288 
  2308 
  2289 text {*
  2309 text {*
  2290 The (co)datatype package can be set up to allow nested recursion through
  2310 The (co)datatype package can be set up to allow nested recursion through
  2291 arbitrary type constructors, as long as they adhere to the BNF requirements
  2311 arbitrary type constructors, as long as they adhere to the BNF requirements
  2292 and are registered as BNFs. It is also possible to declare a BNF abstractly
  2312 and are registered as BNFs. It is also possible to declare a BNF abstractly
  2491   @{syntax_def map_rel}: '(' ((('map' | 'rel') ':' name) +) ')'
  2511   @{syntax_def map_rel}: '(' ((('map' | 'rel') ':' name) +) ')'
  2492   ;
  2512   ;
  2493   @{syntax_def wit_types}: '[' 'wits' ':' types ']'
  2513   @{syntax_def wit_types}: '[' 'wits' ':' types ']'
  2494 \<close>}
  2514 \<close>}
  2495 
  2515 
       
  2516 \medskip
       
  2517 
  2496 \noindent
  2518 \noindent
  2497 The @{command bnf_decl} command declares a new type and associated constants
  2519 The @{command bnf_decl} command declares a new type and associated constants
  2498 (map, set, relator, and cardinal bound) and asserts the BNF properties for
  2520 (map, set, relator, and cardinal bound) and asserts the BNF properties for
  2499 these constants as axioms. Type arguments are live by default; they can be
  2521 these constants as axioms. Type arguments are live by default; they can be
  2500 marked as dead by entering \texttt{-} (hyphen) instead of a name for the
  2522 marked as dead by entering ``-'' (hyphen) instead of an identifier for the
  2501 corresponding set function. Witnesses can be specified by their types.
  2523 corresponding set function. Witnesses can be specified by their types.
  2502 Otherwise, the syntax of @{command bnf_decl} is
  2524 Otherwise, the syntax of @{command bnf_decl} is
  2503 identical to the left-hand side of a @{command datatype_new} or @{command
  2525 identical to the left-hand side of a @{command datatype_new} or @{command
  2504 codatatype} definition.
  2526 codatatype} definition.
  2505 
  2527 
  2572   ;
  2594   ;
  2573   @{syntax_def name_term}: (name ':' term)
  2595   @{syntax_def name_term}: (name ':' term)
  2574   ;
  2596   ;
  2575   X_list: '[' (X + ',') ']'
  2597   X_list: '[' (X + ',') ']'
  2576 \<close>}
  2598 \<close>}
       
  2599 
       
  2600 \medskip
  2577 
  2601 
  2578 % options: no_discs_sels no_code rep_compat
  2602 % options: no_discs_sels no_code rep_compat
  2579 
  2603 
  2580 \noindent
  2604 \noindent
  2581 Section~\ref{ssec:datatype-generated-theorems} lists the generated theorems.
  2605 Section~\ref{ssec:datatype-generated-theorems} lists the generated theorems.