src/Doc/Datatypes/Datatypes.thy
changeset 53534 de2027f9aff3
parent 53491 2479b39d9d09
child 53535 d0c163c6c725
equal deleted inserted replaced
53533:24ce26e8af12 53534:de2027f9aff3
   398 used to ensure that the tail of the empty list is the empty list (instead of
   398 used to ensure that the tail of the empty list is the empty list (instead of
   399 being left unspecified).
   399 being left unspecified).
   400 
   400 
   401 Because @{const Nil} is a nullary constructor, it is also possible to use
   401 Because @{const Nil} is a nullary constructor, it is also possible to use
   402 @{term "\<lambda>xs. xs = Nil"} as a discriminator. This is specified by
   402 @{term "\<lambda>xs. xs = Nil"} as a discriminator. This is specified by
   403 entering ``@{text "="}'' instead of the identifier @{const null} in the
   403 entering ``@{text "="}'' instead of the identifier @{const null}. Although this
   404 declaration above. Although this may look appealing, the mixture of constructors
   404 may look appealing, the mixture of constructors and selectors in the resulting
   405 and selectors in the resulting characteristic theorems can lead Isabelle's
   405 characteristic theorems can lead Isabelle's automation to switch between the
   406 automation to switch between the constructor and the destructor view in
   406 constructor and the destructor view in surprising ways.
   407 surprising ways.
   407 
   408 *}
       
   409 
       
   410 text {*
       
   411 The usual mixfix syntaxes are available for both types and constructors. For
   408 The usual mixfix syntaxes are available for both types and constructors. For
   412 example:
   409 example:
   413 *}
   410 *}
   414 
   411 
   415 (*<*)
   412 (*<*)
   438 
   435 
   439 text {*
   436 text {*
   440 Datatype definitions have the following general syntax:
   437 Datatype definitions have the following general syntax:
   441 
   438 
   442 @{rail "
   439 @{rail "
   443   @@{command datatype_new} @{syntax target}? @{syntax dt_options}? \\
   440   @@{command_def datatype_new} target? @{syntax dt_options}? \\
   444     (@{syntax dt_name} '=' (@{syntax ctor} + '|') + @'and')
   441     (@{syntax dt_name} '=' (@{syntax ctor} + '|') + @'and')
   445   ;
   442   ;
   446   @{syntax_def dt_options}: '(' ((@'no_discs_sels' | @'rep_compat') + ',') ')'
   443   @{syntax_def dt_options}: '(' ((@'no_discs_sels' | @'rep_compat') + ',') ')'
   447 "}
   444 "}
   448 
   445 
   449 The syntactic quantity @{syntax target} can be used to specify a local context
   446 The syntactic quantity \synt{target} can be used to specify a local
   450 (e.g., @{text "(in linorder)"}). It is documented in the Isar reference manual
   447 context---e.g., @{text "(in linorder)"}. It is documented in the Isar reference
   451 \cite{isabelle-isar-ref}.
   448 manual \cite{isabelle-isar-ref}.
   452 
   449 %
   453 The optional target is followed by optional options:
   450 The optional target is optionally followed by datatype-specific options:
   454 
   451 
   455 \begin{itemize}
   452 \begin{itemize}
   456 \setlength{\itemsep}{0pt}
   453 \setlength{\itemsep}{0pt}
   457 
   454 
   458 \item
   455 \item
   459 The \keyw{no\_discs\_sels} option indicates that no destructors (i.e.,
   456 The \keyw{no\_discs\_sels} option indicates that no destructors (i.e.,
   460 discriminators and selectors) should be generated.
   457 discriminators and selectors) should be generated.
   461 
   458 
   462 \item
   459 \item
   463 The \keyw{rep\_compat} option indicates that the names generated by the
   460 The \keyw{rep\_compat} option indicates that the names generated by the
   464 package should contain optional (and normally not displayed) @{text "new."}
   461 package should contain optional (and normally not displayed) ``@{text "new."}''
   465 components to prevent clashes with a later call to @{command rep_datatype}. See
   462 components to prevent clashes with a later call to @{command rep_datatype}. See
   466 Section~\ref{ssec:datatype-compatibility-issues} for details.
   463 Section~\ref{ssec:datatype-compatibility-issues} for details.
   467 \end{itemize}
   464 \end{itemize}
   468 
   465 
   469 The left-hand sides of the datatype equations specify the name of the type to
   466 The left-hand sides of the datatype equations specify the name of the type to
   470 define, its type parameters, and optional additional information:
   467 define, its type parameters, and additional information:
   471 
   468 
   472 @{rail "
   469 @{rail "
   473   @{syntax_def dt_name}: @{syntax tyargs}? @{syntax name}
   470   @{syntax_def dt_name}: @{syntax tyargs}? name @{syntax map_rel}? mixfix?
   474     @{syntax map_rel}? @{syntax mixfix}?
       
   475   ;
   471   ;
   476   @{syntax_def tyargs}: @{syntax typefree} | '(' ((@{syntax name} ':')? @{syntax typefree} + ',') ')'
   472   @{syntax_def tyargs}: typefree | '(' ((name ':')? typefree + ',') ')'
   477   ;
   473   ;
   478   @{syntax_def map_rel}: '(' ((('map' | 'rel') ':' @{syntax name}) +) ')'
   474   @{syntax_def map_rel}: '(' ((('map' | 'rel') ':' name) +) ')'
   479 "}
   475 "}
   480 
   476 
   481 \noindent
   477 \noindent
   482 The syntactic quantity @{syntax name} denotes an identifier, @{syntax typefree}
   478 The syntactic quantity \synt{name} denotes an identifier, \synt{typefree}
   483 denotes fixed type variable (@{typ 'a}, @{typ 'b}, \ldots), and @{syntax
   479 denotes fixed type variable (@{typ 'a}, @{typ 'b}, \ldots), and \synt{mixfix}
   484 mixfix} denotes the usual parenthesized mixfix notation. They are documented in
   480 denotes the usual parenthesized mixfix notation. They are documented in the Isar
   485 the Isar reference manual \cite{isabelle-isar-ref}.
   481 reference manual \cite{isabelle-isar-ref}.
   486 
   482 
   487 The optional names preceding the type variables allow to override the default
   483 The optional names preceding the type variables allow to override the default
   488 names of the set functions (@{text t_set1}, \ldots, @{text t_setM}).
   484 names of the set functions (@{text t_set1}, \ldots, @{text t_setM}).
   489 Inside a mutually recursive datatype specification, all defined datatypes must
   485 Inside a mutually recursive datatype specification, all defined datatypes must
   490 specify exactly the same type variables in the same order.
   486 specify exactly the same type variables in the same order.
   491 
   487 
   492 @{rail "
   488 @{rail "
   493   @{syntax_def ctor}: (@{syntax name} ':')? @{syntax name} (@{syntax ctor_arg} * ) \\
   489   @{syntax_def ctor}: (name ':')? name (@{syntax ctor_arg} * ) \\
   494     @{syntax dt_sel_defaults}? @{syntax mixfix}?
   490     @{syntax dt_sel_defaults}? mixfix?
   495 "}
   491 "}
   496 
   492 
   497 \noindent
   493 \noindent
   498 The main constituents of a constructor specification is the name of the
   494 The main constituents of a constructor specification is the name of the
   499 constructor and the list of its argument types. An optional discriminator name
   495 constructor and the list of its argument types. An optional discriminator name
   500 can be supplied at the front to override the default name
   496 can be supplied at the front to override the default name
   501 (@{text t.un_C}$_{ij}$).
   497 (@{text t.un_C}$_{ij}$).
   502 
   498 
   503 @{rail "
   499 @{rail "
   504   @{syntax_def ctor_arg}: @{syntax type} | '(' @{syntax name} ':' @{syntax type} ')'
   500   @{syntax_def ctor_arg}: type | '(' name ':' type ')'
   505 "}
   501 "}
   506 
   502 
   507 \noindent
   503 \noindent
   508 In addition to the type of a constructor argument, it is possible to specify a
   504 In addition to the type of a constructor argument, it is possible to specify a
   509 name for the corresponding selector to override the default name
   505 name for the corresponding selector to override the default name
   510 (@{text t.un_C}$_{ij}$). The same selector names can be reused for several
   506 (@{text t.un_C}$_{ij}$). The same selector names can be reused for several
   511 constructors as long as they have the same type.
   507 constructors as long as they have the same type.
   512 
   508 
   513 @{rail "
   509 @{rail "
   514   @{syntax_def dt_sel_defaults}: '(' @'defaults' (@{syntax name} ':' @{syntax term} +) ')'
   510   @{syntax_def dt_sel_defaults}: '(' @'defaults' (name ':' term +) ')'
   515 "}
   511 "}
   516 
   512 
   517 \noindent
   513 \noindent
   518 Given a constructor
   514 Given a constructor
   519 @{text "C \<Colon> \<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<sigma>"},
   515 @{text "C \<Colon> \<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<sigma>"},
   809 
   805 
   810 text {*
   806 text {*
   811 Primitive recursive functions have the following general syntax:
   807 Primitive recursive functions have the following general syntax:
   812 
   808 
   813 @{rail "
   809 @{rail "
   814   @@{command primrec_new} @{syntax target}? @{syntax \"fixes\"} \\ @'where'
   810   @@{command primrec_new} target? fixes \\ @'where'
   815     (@{syntax primrec_equation} + '|')
   811     (@{syntax primrec_equation} + '|')
   816   ;
   812   ;
   817   @{syntax_def primrec_equation}: @{syntax thmdecl}? @{syntax prop}
   813   @{syntax_def primrec_equation}: thmdecl? prop
   818 "}
   814 "}
   819 *}
   815 *}
   820 
   816 
   821 
   817 
   822 subsection {* Generated Theorems
   818 subsection {* Generated Theorems
   964 
   960 
   965 text {*
   961 text {*
   966 Primitive corecursive definitions have the following general syntax:
   962 Primitive corecursive definitions have the following general syntax:
   967 
   963 
   968 @{rail "
   964 @{rail "
   969   @@{command primcorec} @{syntax target}? @{syntax \"fixes\"} \\ @'where'
   965   @@{command primcorec} target? fixes \\ @'where'
   970     (@{syntax primcorec_formula} + '|')
   966     (@{syntax primcorec_formula} + '|')
   971   ;
   967   ;
   972   @{syntax_def primcorec_formula}: @{syntax thmdecl}? @{syntax prop}
   968   @{syntax_def primcorec_formula}: thmdecl? prop (@'of' (term * ))?
   973     (@'of' (@{syntax term} * ))?
       
   974 "}
   969 "}
   975 *}
   970 *}
   976 
   971 
   977 
   972 
   978 subsection {* Generated Theorems
   973 subsection {* Generated Theorems
  1009   \label{ssec:bnf-syntax} *}
  1004   \label{ssec:bnf-syntax} *}
  1010 
  1005 
  1011 text {*
  1006 text {*
  1012 
  1007 
  1013 @{rail "
  1008 @{rail "
  1014   @@{command bnf} @{syntax target}? (@{syntax name} ':')? @{syntax term} \\
  1009   @@{command bnf} target? (name ':')? term \\
  1015     @{syntax term_list} @{syntax term} @{syntax term_list} @{syntax term}?
  1010     term_list term term_list term?
  1016   ;
  1011   ;
  1017   @{syntax_def X_list}: '[' (@{syntax X} + ',') ']'
  1012   X_list: '[' (X + ',') ']'
  1018 "}
  1013 "}
  1019 
  1014 
  1020 options: no_discs_sels rep_compat
  1015 options: no_discs_sels rep_compat
  1021 *}
  1016 *}
  1022 
  1017 
  1048 
  1043 
  1049 text {*
  1044 text {*
  1050 Free constructor wrapping has the following general syntax:
  1045 Free constructor wrapping has the following general syntax:
  1051 
  1046 
  1052 @{rail "
  1047 @{rail "
  1053   @@{command wrap_free_constructors} @{syntax target}? @{syntax dt_options} \\
  1048   @@{command wrap_free_constructors} target? @{syntax dt_options} \\
  1054     @{syntax term_list} @{syntax name} @{syntax fc_discs_sels}?
  1049     term_list name @{syntax fc_discs_sels}?
  1055   ;
  1050   ;
  1056   @{syntax_def fc_discs_sels}: @{syntax name_list} (@{syntax name_list_list} @{syntax name_term_list_list}? )?
  1051   @{syntax_def fc_discs_sels}: name_list (name_list_list name_term_list_list? )?
  1057   ;
  1052   ;
  1058   @{syntax_def name_term}: (@{syntax name} ':' @{syntax term})
  1053   @{syntax_def name_term}: (name ':' term)
  1059 "}
  1054 "}
  1060 
  1055 
  1061 options: no_discs_sels rep_compat
  1056 options: no_discs_sels rep_compat
  1062 
  1057 
  1063 X_list is as for BNF
  1058 X_list is as for BNF