doc-src/IsarAdvanced/Codegen/Thy/Codegen.thy
changeset 21323 74ab7c0980c7
parent 21223 b3bdc1abc7d3
child 21341 3844037a8e2d
equal deleted inserted replaced
21322:26f64e7a67b5 21323:74ab7c0980c7
   154 *}
   154 *}
   155 
   155 
   156 code_gen fac (SML "examples/fac.ML")
   156 code_gen fac (SML "examples/fac.ML")
   157 
   157 
   158 text {*
   158 text {*
   159   The \isasymCODEGEN command takes a space-separated list of
   159   The @{text "\<CODEGEN>"} command takes a space-separated list of
   160   constants together with \qn{serialization directives}
   160   constants together with \qn{serialization directives}
   161   in parentheses. These start with a \qn{target language}
   161   in parentheses. These start with a \qn{target language}
   162   identifier, followed by arguments, their semantics
   162   identifier, followed by arguments, their semantics
   163   depending on the target. In the SML case, a filename
   163   depending on the target. In the SML case, a filename
   164   is given where to write the generated code to.
   164   is given where to write the generated code to.
   229 
   229 
   230 subsection {* Theorem selection *}
   230 subsection {* Theorem selection *}
   231 
   231 
   232 text {*
   232 text {*
   233   The list of all function equations in a theory may be inspected
   233   The list of all function equations in a theory may be inspected
   234   using the \isasymPRINTCODETHMS command:
   234   using the @{text "\<PRINTCODETHMS>"} command:
   235 *}
   235 *}
   236 
   236 
   237 print_codethms
   237 print_codethms
   238 
   238 
   239 text {*
   239 text {*
   243   not provide at least one function
   243   not provide at least one function
   244   equation, the table of primitive definitions is searched
   244   equation, the table of primitive definitions is searched
   245   whether it provides one.
   245   whether it provides one.
   246 
   246 
   247   The typical HOL tools are already set up in a way that
   247   The typical HOL tools are already set up in a way that
   248   function definitions introduced by \isasymFUN, \isasymFUNCTION,
   248   function definitions introduced by @{text "\<FUN>"},
   249   \isasymPRIMREC, \isasymRECDEF are implicitly propagated
   249   @{text "\<FUNCTION>"}, @{text "\<PRIMREC>"}
       
   250   @{text "\<RECDEF>"} are implicitly propagated
   250   to this function equation table. Specific theorems may be
   251   to this function equation table. Specific theorems may be
   251   selected using an attribute: \emph{code func}. As example,
   252   selected using an attribute: \emph{code func}. As example,
   252   a weight selector function:
   253   a weight selector function:
   253 *}
   254 *}
   254 
   255 
   390   The cache may be partially or fully dropped if the underlying
   391   The cache may be partially or fully dropped if the underlying
   391   executable content of the theory changes.
   392   executable content of the theory changes.
   392   Implementation of caching is supposed to transparently
   393   Implementation of caching is supposed to transparently
   393   hid away the details from the user.  Anyway, caching
   394   hid away the details from the user.  Anyway, caching
   394   reaches the surface by using a slightly more general form
   395   reaches the surface by using a slightly more general form
   395   of the \isasymCODEGEN: either the list of constants or the
   396   of the @{text "\<CODEGEN>"}: either the list of constants or the
   396   list of serialization expressions may be dropped.  If no
   397   list of serialization expressions may be dropped.  If no
   397   serialization expressions are given, only abstract code
   398   serialization expressions are given, only abstract code
   398   is generated and cached; if no constants are given, the
   399   is generated and cached; if no constants are given, the
   399   current cache is serialized.
   400   current cache is serialized.
   400 
   401 
   401   For explorative reasons, an extended version of the
   402   For explorative purpose, an extended version of the
   402   \isasymCODEGEN command may prove useful:
   403   @{text "\<CODEGEN>"} command may prove useful:
   403 *}
   404 *}
   404 
   405 
   405 print_codethms ()
   406 print_codethms ()
   406 
   407 
   407 text {*
   408 text {*
   440   library; beside being useful in applications, they may serve
   441   library; beside being useful in applications, they may serve
   441   as a tutorial for customizing the code generator setup.
   442   as a tutorial for customizing the code generator setup.
   442 
   443 
   443   \begin{description}
   444   \begin{description}
   444 
   445 
   445     \item[ExecutableSet] allows to generate code
   446     \item[@{theory "ExecutableSet"}] allows to generate code
   446        for finite sets using lists.
   447        for finite sets using lists.
   447     \item[ExecutableRat] \label{exec_rat} implements rational
   448     \item[@{theory "ExecutableRat"}] \label{exec_rat} implements rational
   448        numbers as triples @{text "(sign, enumerator, denominator)"}.
   449        numbers as triples @{text "(sign, enumerator, denominator)"}.
   449     \item[EfficientNat] \label{eff_nat} implements natural numbers by integers,
   450     \item[@{theory "EfficientNat"}] \label{eff_nat} implements natural numbers by integers,
   450        which in general will result in higher efficency; pattern
   451        which in general will result in higher efficency; pattern
   451        matching with @{const "0\<Colon>nat"} / @{const "Suc"}
   452        matching with @{const "0\<Colon>nat"} / @{const "Suc"}
   452        is eliminated.
   453        is eliminated.
   453     \item[MLString] provides an additional datatype @{text "mlstring"};
   454     \item[@{theory "MLString"}] provides an additional datatype @{text "mlstring"};
   454        in the HOL default setup, strings in HOL are mapped to list
   455        in the HOL default setup, strings in HOL are mapped to list
   455        of chars in SML; values of type @{text "mlstring"} are
   456        of chars in SML; values of type @{text "mlstring"} are
   456        mapped to strings in SML.
   457        mapped to strings in SML.
   457 
   458 
   458   \end{description}
   459   \end{description}
   504   \end{itemize}
   505   \end{itemize}
   505 *}
   506 *}
   506 
   507 
   507 text {*
   508 text {*
   508   The current set of inline theorems may be inspected using
   509   The current set of inline theorems may be inspected using
   509   the \isasymPRINTCODETHMS command.
   510   the @{text "\<PRINTCODETHMS>"} command.
   510 
   511 
   511   \emph{Inline procedures} are a generalized version of inline
   512   \emph{Inline procedures} are a generalized version of inline
   512   theorems written in ML -- rewrite rules are generated dependent
   513   theorems written in ML -- rewrite rules are generated dependent
   513   on the function theorems for a certain function.  One
   514   on the function theorems for a certain function.  One
   514   application is the implicit expanding of @{typ nat} numerals
   515   application is the implicit expanding of @{typ nat} numerals
   517 
   518 
   518   \emph{Generic preprocessors} provide a most general interface,
   519   \emph{Generic preprocessors} provide a most general interface,
   519   transforming a list of function theorems to another
   520   transforming a list of function theorems to another
   520   list of function theorems, provided that neither the heading
   521   list of function theorems, provided that neither the heading
   521   constant nor its type change.  The @{const "0\<Colon>nat"} / @{const Suc}
   522   constant nor its type change.  The @{const "0\<Colon>nat"} / @{const Suc}
   522   pattern elimination implemented in \secref{eff_nat} uses this
   523   pattern elimination implemented in
       
   524   theory @{theory "EfficientNat"} (\secref{eff_nat}) uses this
   523   interface.
   525   interface.
   524 
   526 
   525   \begin{warn}
   527   \begin{warn}
   526     The order in which single preprocessing steps are carried
   528     The order in which single preprocessing steps are carried
   527     out currently is not specified; in particular, preprocessing
   529     out currently is not specified; in particular, preprocessing
   548 (*<*)
   550 (*<*)
   549 declare in_interval.simps [code func]
   551 declare in_interval.simps [code func]
   550 (*>*)
   552 (*>*)
   551 
   553 
   552 (*<*)
   554 (*<*)
   553 code_type bool
   555 code_type %tt bool
   554   (SML)
   556   (SML)
   555 code_const True and False and "op \<and>" and Not
   557 code_const %tt True and False and "op \<and>" and Not
   556   (SML and and and)
   558   (SML and and and)
   557 (*>*)
   559 (*>*)
   558 
   560 
   559 code_gen in_interval (SML "examples/bool1.ML")
   561 code_gen in_interval (SML "examples/bool_literal.ML")
   560 
   562 
   561 text {*
   563 text {*
   562   \lstsml{Thy/examples/bool1.ML}
   564   \lstsml{Thy/examples/bool_literal.ML}
   563 
   565 
   564   Though this is correct code, it is a little bit unsatisfactory:
   566   Though this is correct code, it is a little bit unsatisfactory:
   565   boolean values and operators are materialized as distinguished
   567   boolean values and operators are materialized as distinguished
   566   entities with have nothing to do with the SML-builtin notion
   568   entities with have nothing to do with the SML-builtin notion
   567   of \qt{bool}.  This results in less readable code;
   569   of \qt{bool}.  This results in less readable code;
   570   the existing SML \qt{bool} would be used.  To map
   572   the existing SML \qt{bool} would be used.  To map
   571   the HOL \qt{bool} on SML \qt{bool}, we may use
   573   the HOL \qt{bool} on SML \qt{bool}, we may use
   572   \qn{custom serializations}:
   574   \qn{custom serializations}:
   573 *}
   575 *}
   574 
   576 
   575 code_type bool
   577 code_type %tt bool
   576   (SML "bool")
   578   (SML "bool")
   577 code_const True and False and "op \<and>"
   579 code_const %tt True and False and "op \<and>"
   578   (SML "true" and "false" and "_ andalso _")
   580   (SML "true" and "false" and "_ andalso _")
   579 
   581 
   580 text {*
   582 text {*
   581   The \isasymCODETYPE commad takes a type constructor
   583   The @{text "\<CODETYPE>"} commad takes a type constructor
   582   as arguments together with a list of custom serializations.
   584   as arguments together with a list of custom serializations.
   583   Each custom serialization starts with a target language
   585   Each custom serialization starts with a target language
   584   identifier followed by an expression, which during
   586   identifier followed by an expression, which during
   585   code serialization is inserted whenever the type constructor
   587   code serialization is inserted whenever the type constructor
   586   would occur.  For constants, \isasymCODECONST implements
   588   would occur.  For constants, @{text "\<CODECONST>"} implements
   587   the corresponding mechanism.  Each \qt{\_} in
   589   the corresponding mechanism.  Each ``@{verbatim "_"}'' in
   588   a serialization expression is treated as a placeholder
   590   a serialization expression is treated as a placeholder
   589   for the type constructor's (the constant's) arguments.
   591   for the type constructor's (the constant's) arguments.
   590 *}
   592 *}
   591 
   593 
   592 code_reserved SML
   594 code_reserved SML
   593   bool true false
   595   bool true false
   594 
   596 
   595 text {*
   597 text {*
   596   To assert that the existing \qt{bool}, \qt{true} and \qt{false}
   598   To assert that the existing \qt{bool}, \qt{true} and \qt{false}
   597   is not used for generated code, we use \isasymCODERESERVED.
   599   is not used for generated code, we use @{text "\<CODERESERVED>"}.
   598 
   600 
   599   After this setup, code looks quite more readable:
   601   After this setup, code looks quite more readable:
   600 *}
   602 *}
   601 
   603 
   602 code_gen in_interval (SML "examples/bool2.ML")
   604 code_gen in_interval (SML "examples/bool_mlbool.ML")
   603 
   605 
   604 text {*
   606 text {*
   605   \lstsml{Thy/examples/bool2.ML}
   607   \lstsml{Thy/examples/bool_mlbool.ML}
   606 
   608 
   607   This still is not perfect: the parentheses
   609   This still is not perfect: the parentheses
   608   around \qt{andalso} are superfluous.  Though the serializer
   610   around the \qt{andalso} expression are superfluous.
       
   611   Though the serializer
   609   by no means attempts to imitate the rich Isabelle syntax
   612   by no means attempts to imitate the rich Isabelle syntax
   610   framework, it provides some common idioms, notably
   613   framework, it provides some common idioms, notably
   611   associative infixes with precedences which may be used here:
   614   associative infixes with precedences which may be used here:
   612 *}
   615 *}
   613 
   616 
   614 code_const "op \<and>"
   617 code_const %tt "op \<and>"
   615   (SML infixl 1 "andalso")
   618   (SML infixl 1 "andalso")
   616 
   619 
   617 code_gen in_interval (SML "examples/bool3.ML")
   620 code_gen in_interval (SML "examples/bool_infix.ML")
   618 
   621 
   619 text {*
   622 text {*
   620   \lstsml{Thy/examples/bool3.ML}
   623   \lstsml{Thy/examples/bool_infix.ML}
   621 
   624 
   622   Next, we try to map HOL pairs to SML pairs, using the
   625   Next, we try to map HOL pairs to SML pairs, using the
   623   infix \qt{ * } type constructor and parentheses:
   626   infix ``@{verbatim "*"}'' type constructor and parentheses:
   624 *}
   627 *}
   625 
   628 
   626 (*<*)
   629 (*<*)
   627 code_type *
   630 code_type *
   628   (SML)
   631   (SML)
   629 code_const Pair
   632 code_const Pair
   630   (SML)
   633   (SML)
   631 (*>*)
   634 (*>*)
   632 
   635 
   633 code_type *
   636 code_type %tt *
   634   (SML infix 2 "*")
   637   (SML infix 2 "*")
   635 
   638 
   636 code_const Pair
   639 code_const %tt Pair
   637   (SML "!((_),/ (_))")
   640   (SML "!((_),/ (_))")
   638 
   641 
   639 text {*
   642 text {*
   640   The initial bang \qt{!} tells the serializer to never put
   643   The initial bang ``@{verbatim "!"}'' tells the serializer to never put
   641   parentheses around the whole expression (they are already present),
   644   parentheses around the whole expression (they are already present),
   642   while the parentheses around argument place holders
   645   while the parentheses around argument place holders
   643   tell not to put parentheses around the arguments.
   646   tell not to put parentheses around the arguments.
   644   The slash \qt{/} (followed by arbitrary white space)
   647   The slash ``@{verbatim "/"}'' (followed by arbitrary white space)
   645   inserts a space which may be used as a break if necessary
   648   inserts a space which may be used as a break if necessary
   646   during pretty printing.
   649   during pretty printing.
   647 
   650 
   648   So far, we did only provide more idiomatic serializations for
   651   So far, we did only provide more idiomatic serializations for
   649   constructs which would be executable on their own.  Target-specific
   652   constructs which would be executable on their own.  Target-specific
   664   \footnote{Eventually, we also want to provide executability
   667   \footnote{Eventually, we also want to provide executability
   665   for quotients.}.  However, we could use the SML builtin
   668   for quotients.}.  However, we could use the SML builtin
   666   integers:
   669   integers:
   667 *}
   670 *}
   668 
   671 
   669 code_type int
   672 code_type %tt int
   670   (SML "IntInf.int")
   673   (SML "IntInf.int")
   671 
   674 
   672 code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
   675 code_const %tt "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
   673     and "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
   676     and "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
   674   (SML "IntInf.+ (_, _)" and "IntInf.* (_, _)")
   677   (SML "IntInf.+ (_, _)" and "IntInf.* (_, _)")
   675 
   678 
   676 code_gen double_inc (SML "examples/integers.ML")
   679 code_gen double_inc (SML "examples/integers.ML")
   677 
   680 
   688   inconsistencies -- or, in other words:
   691   inconsistencies -- or, in other words:
   689   custom serializations are completely axiomatic.
   692   custom serializations are completely axiomatic.
   690 
   693 
   691   A further noteworthy details is that any special
   694   A further noteworthy details is that any special
   692   character in a custom serialization may be quoted
   695   character in a custom serialization may be quoted
   693   using \qt{'}; thus, in \qt{fn '\_ => \_} the first
   696   using ``@{verbatim "'"}''; thus, in
   694   \qt{'\_} is a proper underscore while the
   697   ``@{verbatim "fn '_ => _"}'' the first
   695   second \qt{\_} is a placeholder.
   698   ``@{verbatim "_"}'' is a proper underscore while the
       
   699   second ``@{verbatim "_"}'' is a placeholder.
   696 
   700 
   697   The HOL theories provide further
   701   The HOL theories provide further
   698   examples for custom serializations and form
   702   examples for custom serializations and form
   699   a recommended tutorial on how to use them properly.
   703   a recommended tutorial on how to use them properly.
   700 *}
   704 *}
   776 
   780 
   777 text {*
   781 text {*
   778   Examine the following:
   782   Examine the following:
   779 *}
   783 *}
   780 
   784 
   781 code_const "op = \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
   785 code_const %tt "op = \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
   782   (SML "!(_ : IntInf.int = _)")
   786   (SML "!(_ : IntInf.int = _)")
   783 
   787 
   784 text {*
   788 text {*
   785   What is wrong here? Since @{const "op ="} is nothing else then
   789   What is wrong here? Since @{const "op ="} is nothing else then
   786   a plain constant, this customary serialization will refer
   790   a plain constant, this customary serialization will refer
   787   to polymorphic equality @{const "op = \<Colon> 'a \<Rightarrow> 'a \<Rightarrow> bool"}.
   791   to polymorphic equality @{const "op = \<Colon> 'a \<Rightarrow> 'a \<Rightarrow> bool"}.
   788   Instead, we want the specific equality on @{typ int},
   792   Instead, we want the specific equality on @{typ int},
   789   by using the overloaded constant @{const "Code_Generator.eq"}:
   793   by using the overloaded constant @{const "Code_Generator.eq"}:
   790 *}
   794 *}
   791 
   795 
   792 code_const "Code_Generator.eq \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
   796 code_const %tt "Code_Generator.eq \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
   793   (SML "!(_ : IntInf.int = _)")
   797   (SML "!(_ : IntInf.int = _)")
   794 
   798 
   795 subsubsection {* typedecls interpreted by customary serializations *}
   799 subsubsection {* typedecls interpreted by customary serializations *}
   796 
   800 
   797 text {*
   801 text {*
   808 termination by (auto_term "measure (length o fst)")
   812 termination by (auto_term "measure (length o fst)")
   809 (*<*)
   813 (*<*)
   810 lemmas [code func] = lookup.simps
   814 lemmas [code func] = lookup.simps
   811 (*>*)
   815 (*>*)
   812 
   816 
   813 code_type key
   817 code_type %tt key
   814   (SML "string")
   818   (SML "string")
   815 
   819 
   816 text {*
   820 text {*
   817   This, though, is not sufficient: @{typ key} is no instance
   821   This, though, is not sufficient: @{typ key} is no instance
   818   of @{text eq} since @{typ key} is no datatype; the instance
   822   of @{text eq} since @{typ key} is no datatype; the instance
   820   for the particular instance of @{const "Code_Generator.eq"}:
   824   for the particular instance of @{const "Code_Generator.eq"}:
   821 *}
   825 *}
   822 
   826 
   823 instance key :: eq ..
   827 instance key :: eq ..
   824 
   828 
   825 code_const "Code_Generator.eq \<Colon> key \<Rightarrow> key \<Rightarrow> bool"
   829 code_const %tt "Code_Generator.eq \<Colon> key \<Rightarrow> key \<Rightarrow> bool"
   826   (SML "!(_ : string = _)")
   830   (SML "!(_ : string = _)")
   827 
   831 
   828 text {*
   832 text {*
   829   Then everything goes fine:
   833   Then everything goes fine:
   830 *}
   834 *}
   894 
   898 
   895 text {*
   899 text {*
   896   For convenience, the default
   900   For convenience, the default
   897   HOL setup for Haskell maps the @{text eq} class to
   901   HOL setup for Haskell maps the @{text eq} class to
   898   its counterpart in Haskell, giving custom serializations
   902   its counterpart in Haskell, giving custom serializations
   899   for the class (\isasymCODECLASS) and its operation:
   903   for the class (@{text "\<CODECLASS>"}) and its operation:
   900 *}
   904 *}
   901 
   905 
   902 (*<*)
   906 (*<*)
   903 setup {* Sign.add_path "bar" *}
   907 setup {* Sign.add_path "bar" *}
   904 class eq = fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
   908 class eq = fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
   905 (*>*)
   909 (*>*)
   906 
   910 
   907 code_class eq
   911 code_class %tt eq
   908   (Haskell "Eq" where eq \<equiv> "(==)")
   912   (Haskell "Eq" where eq \<equiv> "(==)")
   909 
   913 
   910 code_const eq
   914 code_const %tt eq
   911   (Haskell infixl 4 "==")
   915   (Haskell infixl 4 "==")
   912 
   916 
   913 (*<*)
   917 (*<*)
   914 hide "class" eq
   918 hide "class" eq
   915 hide const eq
   919 hide const eq
   925 
   929 
   926 typedecl bar
   930 typedecl bar
   927 
   931 
   928 instance bar :: eq ..
   932 instance bar :: eq ..
   929 
   933 
   930 code_type bar
   934 code_type %tt bar
   931   (Haskell "Integer")
   935   (Haskell "Integer")
   932 
   936 
   933 text {*
   937 text {*
   934     The code generator would produce
   938     The code generator would produce
   935     an additional instance, which of course is rejected.
   939     an additional instance, which of course is rejected.
   936     To suppress this additional instance, use
   940     To suppress this additional instance, use
   937     \isasymCODEINSTANCE:
   941     @{text "\<CODEINSTANCE>"}:
   938 *}
   942 *}
   939 
   943 
   940 code_instance bar :: eq
   944 code_instance %tt bar :: eq
   941   (Haskell -)
   945   (Haskell -)
   942 
   946 
   943 subsection {* Types matter *}
   947 subsection {* Types matter *}
   944 
   948 
   945 text {*
   949 text {*
   946   Imagine the following quick-and-dirty setup for implementing
   950   Imagine the following quick-and-dirty setup for implementing
   947   some kind of sets as lists in SML:
   951   some kind of sets as lists in SML:
   948 *}
   952 *}
   949 
   953 
   950 code_type set
   954 code_type %tt set
   951   (SML "_ list")
   955   (SML "_ list")
   952 
   956 
   953 code_const "{}" and insert
   957 code_const %tt "{}" and insert
   954   (SML "![]" and infixl 7 "::")
   958   (SML "![]" and infixl 7 "::")
   955 
   959 
   956 definition
   960 definition
   957   dummy_set :: "(nat \<Rightarrow> nat) set"
   961   dummy_set :: "(nat \<Rightarrow> nat) set"
   958   "dummy_set = {Suc}"
   962   "dummy_set = {Suc}"
  1057   to identify the famous HOL constant @{const arbitrary}
  1061   to identify the famous HOL constant @{const arbitrary}
  1058   of type @{typ "'a option"} with @{const None}.
  1062   of type @{typ "'a option"} with @{const None}.
  1059   By brute force:
  1063   By brute force:
  1060 *}
  1064 *}
  1061 
  1065 
  1062 axioms
  1066 axiomatization where
  1063   arbitrary_option: "arbitrary = None"
  1067   "arbitrary = None"
  1064 
  1068 
  1065 text {*
  1069 text {*
  1066   However this has to be considered harmful since this axiom,
  1070   However this has to be considered harmful since this axiom,
  1067   though probably justifiable for generated code, could
  1071   though probably justifiable for generated code, could
  1068   introduce serious inconsistencies into the logic.
  1072   introduce serious inconsistencies into the logic.
  1095 
  1099 
  1096 definition
  1100 definition
  1097   "None' = None"
  1101   "None' = None"
  1098 
  1102 
  1099 text {*
  1103 text {*
  1100   Then finally we are enabled to use \isasymCODEAXIOMS:
  1104   Then finally we are enabled to use @{text "\<CODEAXIOMS>"}:
  1101 *}
  1105 *}
  1102 
  1106 
  1103 code_axioms
  1107 code_axioms
  1104   arbitrary_option \<equiv> None'
  1108   arbitrary_option \<equiv> None'
  1105 
  1109 
  1121 text {*
  1125 text {*
  1122   \lstsml{Thy/examples/arbitrary.ML}
  1126   \lstsml{Thy/examples/arbitrary.ML}
  1123 
  1127 
  1124   Another axiomatic extension is code generation
  1128   Another axiomatic extension is code generation
  1125   for abstracted types.  For this, the  
  1129   for abstracted types.  For this, the  
  1126   \emph{ExecutableRat} (see \secref{exec_rat})
  1130   @{theory "ExecutableRat"} (see \secref{exec_rat})
  1127   forms a good example.
  1131   forms a good example.
  1128 *}
  1132 *}
  1129 
  1133 
  1130 
  1134 
  1131 section {* ML interfaces \label{sec:ml} *}
  1135 section {* ML interfaces \label{sec:ml} *}
  1144   constants by unique identifiers.
  1148   constants by unique identifiers.
  1145 *}
  1149 *}
  1146 
  1150 
  1147 text %mlref {*
  1151 text %mlref {*
  1148   \begin{mldecls}
  1152   \begin{mldecls}
  1149   @{index_ML_type CodegenConsts.const} \\
  1153   @{index_ML_type CodegenConsts.const: "string * typ list"} \\
  1150   @{index_ML CodegenConsts.norm_of_typ: "theory -> string * typ -> CodegenConsts.const"} \\
  1154   @{index_ML CodegenConsts.norm_of_typ: "theory -> string * typ -> CodegenConsts.const"} \\
  1151   @{index_ML CodegenConsts.typ_of_inst: "theory -> CodegenConsts.const -> string * typ"} \\
  1155   @{index_ML CodegenConsts.typ_of_inst: "theory -> CodegenConsts.const -> string * typ"} \\
  1152  \end{mldecls}
  1156  \end{mldecls}
  1153 
  1157 
  1154   \begin{description}
  1158   \begin{description}
  1178 
  1182 
  1179 subsubsection {* Suspended theorems *}
  1183 subsubsection {* Suspended theorems *}
  1180 
  1184 
  1181 text %mlref {*
  1185 text %mlref {*
  1182   \begin{mldecls}
  1186   \begin{mldecls}
  1183   @{index_ML_type CodegenData.lthms} \\
       
  1184   @{index_ML CodegenData.lazy: "(unit -> thm list) -> CodegenData.lthms"}
  1187   @{index_ML CodegenData.lazy: "(unit -> thm list) -> CodegenData.lthms"}
  1185   \end{mldecls}
  1188   \end{mldecls}
  1186 
  1189 
  1187   \begin{description}
  1190   \begin{description}
  1188 
  1191 
  1189   \item @{ML_type CodegenData.lthms} is an abstract view
       
  1190      on suspended theorems.  Suspensions are evaluated on demand.
       
  1191 
       
  1192   \item @{ML CodegenData.lazy}~@{text f} turns an abstract
  1192   \item @{ML CodegenData.lazy}~@{text f} turns an abstract
  1193      theorem computation @{text f} into suspended theorems.
  1193      theorem computation @{text f} into a suspension of theorems.
  1194 
  1194 
  1195   \end{description}
  1195   \end{description}
  1196 *}
  1196 *}
  1197 
  1197 
  1198 subsubsection {* Executable content *}
  1198 subsubsection {* Executable content *}
  1199 
  1199 
  1200 text %mlref {*
  1200 text %mlref {*
  1201   \begin{mldecls}
  1201   \begin{mldecls}
  1202   @{index_ML_type CodegenData.lthms} \\
       
  1203   @{index_ML CodegenData.add_func: "thm -> theory -> theory"} \\
  1202   @{index_ML CodegenData.add_func: "thm -> theory -> theory"} \\
  1204   @{index_ML CodegenData.del_func: "thm -> theory -> theory"} \\
  1203   @{index_ML CodegenData.del_func: "thm -> theory -> theory"} \\
  1205   @{index_ML CodegenData.add_funcl: "CodegenConsts.const * CodegenData.lthms -> theory -> theory"} \\
  1204   @{index_ML CodegenData.add_funcl: "CodegenConsts.const * CodegenData.lthms -> theory -> theory"} \\
  1206   @{index_ML CodegenData.add_inline: "thm -> theory -> theory"} \\
  1205   @{index_ML CodegenData.add_inline: "thm -> theory -> theory"} \\
  1207   @{index_ML CodegenData.del_inline: "thm -> theory -> theory"} \\
  1206   @{index_ML CodegenData.del_inline: "thm -> theory -> theory"} \\
  1414 
  1413 
  1415 subsubsection {* Datatype hooks *}
  1414 subsubsection {* Datatype hooks *}
  1416 
  1415 
  1417 text %mlref {*
  1416 text %mlref {*
  1418   \begin{mldecls}
  1417   \begin{mldecls}
  1419   @{index_ML_type DatatypeHooks.hook} \\
  1418   @{index_ML_type DatatypeHooks.hook: "string list -> theory -> theory"} \\
  1420   @{index_ML DatatypeHooks.add: "DatatypeHooks.hook -> theory -> theory"}
  1419   @{index_ML DatatypeHooks.add: "DatatypeHooks.hook -> theory -> theory"}
  1421   \end{mldecls}
  1420   \end{mldecls}
  1422 *}
  1421 *}
  1423 
  1422 
  1424 text %mlref {*
  1423 text %mlref {*
  1425   \begin{mldecls}
  1424   \begin{mldecls}
  1426   @{index_ML_type TypecopyPackage.info} \\
  1425   @{index_ML_type TypecopyPackage.info: "{
       
  1426     vs: (string * sort) list,
       
  1427     constr: string,
       
  1428     typ: typ,
       
  1429     inject: thm,
       
  1430     proj: string * typ,
       
  1431     proj_def: thm
       
  1432   }"} \\
  1427   @{index_ML TypecopyPackage.add_typecopy: "
  1433   @{index_ML TypecopyPackage.add_typecopy: "
  1428     bstring * string list -> typ -> (bstring * bstring) option
  1434     bstring * string list -> typ -> (bstring * bstring) option
  1429     -> theory -> (string * TypecopyPackage.info) * theory"} \\
  1435     -> theory -> (string * TypecopyPackage.info) * theory"} \\
  1430   @{index_ML TypecopyPackage.get_typecopies: "theory -> string list"} \\
  1436   @{index_ML TypecopyPackage.get_typecopies: "theory -> string list"} \\
  1431   @{index_ML TypecopyPackage.get_typecopy_info: "theory
  1437   @{index_ML TypecopyPackage.get_typecopy_info: "theory
  1437   \end{mldecls}
  1443   \end{mldecls}
  1438 *}
  1444 *}
  1439 
  1445 
  1440 text %mlref {*
  1446 text %mlref {*
  1441   \begin{mldecls}
  1447   \begin{mldecls}
  1442   @{index_ML_type DatatypeCodegen.hook} \\
  1448   @{index_ML_type DatatypeCodegen.hook: "(string * (bool * ((string * sort) list * (string * typ list) list))) list
       
  1449     -> theory -> theory"} \\
  1443   @{index_ML DatatypeCodegen.add_codetypes_hook_bootstrap: "
  1450   @{index_ML DatatypeCodegen.add_codetypes_hook_bootstrap: "
  1444       DatatypeCodegen.hook -> theory -> theory"}
  1451       DatatypeCodegen.hook -> theory -> theory"}
  1445   \end{mldecls}
  1452   \end{mldecls}
  1446 *}
  1453 *}
  1447 
  1454