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>"}, |