src/Doc/Datatypes/Datatypes.thy
changeset 54187 80259d79a81e
parent 54185 3fe3b5d33e41
child 54190 7bbe8209c253
equal deleted inserted replaced
54186:ea92cce67f09 54187:80259d79a81e
    39     datatype_new flag = Less | Eq | Greater
    39     datatype_new flag = Less | Eq | Greater
    40     end
    40     end
    41 
    41 
    42 text {*
    42 text {*
    43 \noindent
    43 \noindent
    44 The package also provides some convenience, notably automatically generated
    44 Furthermore, the package provides a lot of convenience, including automatically
    45 discriminators and selectors.
    45 generated discriminators, selectors, and relators as well as a wealth of
    46 
    46 properties about them.
    47 In addition to plain inductive datatypes, the new package supports coinductive
    47 
    48 datatypes, or \emph{codatatypes}, which may have infinite values. For example,
    48 In addition to inductive datatypes, the new package supports coinductive
    49 the following command introduces the type of lazy lists, which comprises both
    49 datatypes, or \emph{codatatypes}, which allow infinite values. For example, the
    50 finite and infinite values:
    50 following command introduces the type of lazy lists, which comprises both finite
       
    51 and infinite values:
    51 *}
    52 *}
    52 
    53 
    53 (*<*)
    54 (*<*)
    54     locale early
    55     locale early
    55     locale late
    56     locale late
    66     datatype_new (*<*)(in early) (*>*)'a tree\<^sub>f\<^sub>i = Node\<^sub>f\<^sub>i 'a "'a tree\<^sub>f\<^sub>i llist"
    67     datatype_new (*<*)(in early) (*>*)'a tree\<^sub>f\<^sub>i = Node\<^sub>f\<^sub>i 'a "'a tree\<^sub>f\<^sub>i llist"
    67     codatatype (*<*)(in early) (*>*)'a tree\<^sub>i\<^sub>f = Node\<^sub>i\<^sub>f 'a "'a tree\<^sub>i\<^sub>f list"
    68     codatatype (*<*)(in early) (*>*)'a tree\<^sub>i\<^sub>f = Node\<^sub>i\<^sub>f 'a "'a tree\<^sub>i\<^sub>f list"
    68     codatatype (*<*)(in early) (*>*)'a tree\<^sub>i\<^sub>i = Node\<^sub>i\<^sub>i 'a "'a tree\<^sub>i\<^sub>i llist"
    69     codatatype (*<*)(in early) (*>*)'a tree\<^sub>i\<^sub>i = Node\<^sub>i\<^sub>i 'a "'a tree\<^sub>i\<^sub>i llist"
    69 
    70 
    70 text {*
    71 text {*
    71 The first two tree types allow only finite branches, whereas the last two allow
    72 The first two tree types allow only paths of finite length, whereas the last two
    72 branches of infinite length. Orthogonally, the nodes in the first and third
    73 allow infinite paths. Orthogonally, the nodes in the first and third types have
    73 types have finite branching, whereas those of the second and fourth may have
    74 finitely many direct subtrees, whereas those of the second and fourth may have
    74 infinitely many direct subtrees.
    75 infinite branching.
    75 
    76 
    76 To use the package, it is necessary to import the @{theory BNF} theory, which
    77 To use the package, it is necessary to import the @{theory BNF} theory, which
    77 can be precompiled into the \texttt{HOL-BNF} image. The following commands show
    78 can be precompiled into the \texttt{HOL-BNF} image. The following commands show
    78 how to launch jEdit/PIDE with the image loaded and how to build the image
    79 how to launch jEdit/PIDE with the image loaded and how to build the image
    79 without launching jEdit:
    80 without launching jEdit:
   239 
   240 
   240     datatype_new nat = Zero | Suc nat
   241     datatype_new nat = Zero | Suc nat
   241 
   242 
   242 text {*
   243 text {*
   243 \noindent
   244 \noindent
   244 Lists were shown in the introduction. Terminated lists are a variant:
   245 Lists were shown in the introduction. Terminated lists are a variant that
       
   246 stores a value of type @{typ 'b} at the very end:
   245 *}
   247 *}
   246 
   248 
   247     datatype_new (*<*)(in early) (*>*)('a, 'b) tlist = TNil 'b | TCons 'a "('a, 'b) tlist"
   249     datatype_new (*<*)(in early) (*>*)('a, 'b) tlist = TNil 'b | TCons 'a "('a, 'b) tlist"
   248 
   250 
   249 
   251 
   289 text {*
   291 text {*
   290 \noindent
   292 \noindent
   291 Not all nestings are admissible. For example, this command will fail:
   293 Not all nestings are admissible. For example, this command will fail:
   292 *}
   294 *}
   293 
   295 
   294     datatype_new 'a wrong = Wrong (*<*)'a
   296     datatype_new 'a wrong = W1 | W2 (*<*)'a
   295     typ (*>*)"'a wrong \<Rightarrow> 'a"
   297     typ (*>*)"'a wrong \<Rightarrow> 'a"
   296 
   298 
   297 text {*
   299 text {*
   298 \noindent
   300 \noindent
   299 The issue is that the function arrow @{text "\<Rightarrow>"} allows recursion
   301 The issue is that the function arrow @{text "\<Rightarrow>"} allows recursion
   300 only through its right-hand side. This issue is inherited by polymorphic
   302 only through its right-hand side. This issue is inherited by polymorphic
   301 datatypes defined in terms of~@{text "\<Rightarrow>"}:
   303 datatypes defined in terms of~@{text "\<Rightarrow>"}:
   302 *}
   304 *}
   303 
   305 
   304     datatype_new ('a, 'b) fn = Fn "'a \<Rightarrow> 'b"
   306     datatype_new ('a, 'b) fn = Fn "'a \<Rightarrow> 'b"
   305     datatype_new 'a also_wrong = Also_Wrong (*<*)'a
   307     datatype_new 'a also_wrong = W1 | W2 (*<*)'a
   306     typ (*>*)"('a also_wrong, 'a) fn"
   308     typ (*>*)"('a also_wrong, 'a) fn"
   307 
   309 
   308 text {*
   310 text {*
   309 \noindent
   311 \noindent
   310 This is legal:
   312 This is legal:
   323 Type constructors must be registered as BNFs to have live arguments. This is
   325 Type constructors must be registered as BNFs to have live arguments. This is
   324 done automatically for datatypes and codatatypes introduced by the @{command
   326 done automatically for datatypes and codatatypes introduced by the @{command
   325 datatype_new} and @{command codatatype} commands.
   327 datatype_new} and @{command codatatype} commands.
   326 Section~\ref{sec:registering-bounded-natural-functors} explains how to register
   328 Section~\ref{sec:registering-bounded-natural-functors} explains how to register
   327 arbitrary type constructors as BNFs.
   329 arbitrary type constructors as BNFs.
   328 *}
   330 
   329 
   331 Here is another example that fails:
   330 
   332 *}
   331 subsubsection {* Custom Names and Syntaxes
   333 
   332   \label{sssec:datatype-custom-names-and-syntaxes} *}
   334     datatype_new 'a pow_list = PNil 'a (*<*)'a
       
   335     datatype_new 'a pow_list' = PNil' 'a (*>*)| PCons "('a * 'a) pow_list"
       
   336 
       
   337 text {*
       
   338 \noindent
       
   339 This one features a different flavor of nesting, where the recursive call in the
       
   340 type specification occurs around (rather than inside) another type constructor.
       
   341 *}
       
   342 
       
   343 subsubsection {* Auxiliary Constants and Properties
       
   344   \label{sssec:datatype-auxiliary-constants-and-properties} *}
   333 
   345 
   334 text {*
   346 text {*
   335 The @{command datatype_new} command introduces various constants in addition to
   347 The @{command datatype_new} command introduces various constants in addition to
   336 the constructors. With each datatype are associated set functions, a map
   348 the constructors. With each datatype are associated set functions, a map
   337 function, a relator, discriminators, and selectors, all of which can be given
   349 function, a relator, discriminators, and selectors, all of which can be given
   338 custom names. In the example below, the traditional names
   350 custom names. In the example below, the familiar names @{text null}, @{text hd},
   339 @{text set}, @{text map}, @{text list_all2}, @{text null}, @{text hd}, and
   351 @{text tl}, @{text set}, @{text map}, and @{text list_all2}, override the
   340 @{text tl} override the default names @{text list_set}, @{text list_map}, @{text
   352 default names @{text is_Nil}, @{text un_Cons1}, @{text un_Cons2},
   341 list_rel}, @{text is_Nil}, @{text un_Cons1}, and @{text un_Cons2}:
   353 @{text list_set}, @{text list_map}, and @{text list_rel}:
   342 *}
   354 *}
   343 
   355 
   344 (*<*)
   356 (*<*)
   345     no_translations
   357     no_translations
   346       "[x, xs]" == "x # [xs]"
   358       "[x, xs]" == "x # [xs]"
   359       null: Nil (defaults tl: Nil)
   371       null: Nil (defaults tl: Nil)
   360     | Cons (hd: 'a) (tl: "'a list")
   372     | Cons (hd: 'a) (tl: "'a list")
   361 
   373 
   362 text {*
   374 text {*
   363 \noindent
   375 \noindent
   364 The command introduces a discriminator @{const null} and a pair of selectors
   376 
   365 @{const hd} and @{const tl} characterized as follows:
   377 \begin{tabular}{@ {}ll@ {}}
       
   378 Constructors: &
       
   379   @{text "Nil \<Colon> 'a list"} \\
       
   380 &
       
   381   @{text "Cons \<Colon> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list"} \\
       
   382 Discriminator: &
       
   383   @{text "null \<Colon> 'a list \<Rightarrow> bool"} \\
       
   384 Selectors: &
       
   385   @{text "hd \<Colon> 'a list \<Rightarrow> 'a"} \\
       
   386 &
       
   387   @{text "tl \<Colon> 'a list \<Rightarrow> 'a list"} \\
       
   388 Set function: &
       
   389   @{text "set \<Colon> 'a list \<Rightarrow> 'a set"} \\
       
   390 Map function: &
       
   391   @{text "map \<Colon> ('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list"} \\
       
   392 Relator: &
       
   393   @{text "list_all2 \<Colon> ('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> bool"}
       
   394 \end{tabular}
       
   395 
       
   396 The discriminator @{const null} and the selectors @{const hd} and @{const tl}
       
   397 are characterized as follows:
   366 %
   398 %
   367 \[@{thm list.collapse(1)[of xs, no_vars]}
   399 \[@{thm list.collapse(1)[of xs, no_vars]}
   368   \qquad @{thm list.collapse(2)[of xs, no_vars]}\]
   400   \qquad @{thm list.collapse(2)[of xs, no_vars]}\]
   369 %
   401 %
   370 For two-constructor datatypes, a single discriminator constant suffices. The
   402 For two-constructor datatypes, a single discriminator constant is sufficient.
   371 discriminator associated with @{const Cons} is simply
   403 The discriminator associated with @{const Cons} is simply
   372 @{term "\<lambda>xs. \<not> null xs"}.
   404 @{term "\<lambda>xs. \<not> null xs"}.
   373 
   405 
   374 The @{text defaults} clause following the @{const Nil} constructor specifies a
   406 The @{text defaults} clause following the @{const Nil} constructor specifies a
   375 default value for selectors associated with other constructors. Here, it is used
   407 default value for selectors associated with other constructors. Here, it is used
   376 to ensure that the tail of the empty list is itself (instead of being left
   408 to ensure that the tail of the empty list is itself (instead of being left
  2206   X_list: '[' (X + ',') ']'
  2238   X_list: '[' (X + ',') ']'
  2207 "}
  2239 "}
  2208 *}
  2240 *}
  2209 
  2241 
  2210 
  2242 
       
  2243 subsubsection {* \keyw{bnf\_decl}
       
  2244   \label{sssec:bnf-decl} *}
       
  2245 
       
  2246 text {*
       
  2247 %%% TODO: use command_def once the command is available
       
  2248 \begin{matharray}{rcl}
       
  2249   @{text "bnf_decl"} & : & @{text "local_theory \<rightarrow> local_theory"}
       
  2250 \end{matharray}
       
  2251 
       
  2252 @{rail "
       
  2253   @@{command bnf} target? dt_name
       
  2254 "}
       
  2255 *}
       
  2256 
       
  2257 
  2211 subsubsection {* \keyw{print\_bnfs}
  2258 subsubsection {* \keyw{print\_bnfs}
  2212   \label{sssec:print-bnfs} *}
  2259   \label{sssec:print-bnfs} *}
  2213 
  2260 
  2214 text {*
  2261 text {*
  2215 \begin{matharray}{rcl}
  2262 \begin{matharray}{rcl}