doc-src/IsarAdvanced/Codegen/Thy/Codegen.thy
changeset 27557 151731493264
parent 27103 d8549f4d900b
child 27609 b23c9ad0fe7d
equal deleted inserted replaced
27556:292098f2efdf 27557:151731493264
   499 subsection {* Preprocessing *}
   499 subsection {* Preprocessing *}
   500 
   500 
   501 text {*
   501 text {*
   502   Before selected function theorems are turned into abstract
   502   Before selected function theorems are turned into abstract
   503   code, a chain of definitional transformation steps is carried
   503   code, a chain of definitional transformation steps is carried
   504   out: \emph{preprocessing}. There are three possibilities
   504   out: \emph{preprocessing}.  In essence, the preprocessort
   505   to customize preprocessing: \emph{inline theorems},
   505   consists of two components: a \emph{simpset} and \emph{function transformators}.
   506   \emph{inline procedures} and \emph{generic preprocessors}.
   506 
   507 
   507   The \emph{simpset} allows to employ the full generality of the Isabelle
   508   \emph{Inline theorems} are rewriting rules applied to each
   508   simplifier.  Due to the interpretation of theorems
   509   defining equation.  Due to the interpretation of theorems
       
   510   of defining equations, rewrites are applied to the right
   509   of defining equations, rewrites are applied to the right
   511   hand side and the arguments of the left hand side of an
   510   hand side and the arguments of the left hand side of an
   512   equation, but never to the constant heading the left hand side.
   511   equation, but never to the constant heading the left hand side.
   513   Inline theorems may be declared an undeclared using the
   512   An important special case are \emph{inline theorems} which may be
       
   513   declared an undeclared using the
   514   \emph{code inline} or \emph{code inline del} attribute respectively.
   514   \emph{code inline} or \emph{code inline del} attribute respectively.
   515   Some common applications:
   515   Some common applications:
   516 *}
   516 *}
   517 
   517 
   518 text_raw {*
   518 text_raw {*
   543 text_raw {*
   543 text_raw {*
   544   \end{itemize}
   544   \end{itemize}
   545 *}
   545 *}
   546 
   546 
   547 text {*
   547 text {*
   548   \noindent The current set of inline theorems may be inspected using
   548 
   549   the @{text "\<PRINTCODESETUP>"} command.
   549   \emph{Function transformators} provide a most general interface,
   550 
       
   551   \emph{Inline procedures} are a generalized version of inline
       
   552   theorems written in ML -- rewrite rules are generated dependent
       
   553   on the function theorems for a certain function.  One
       
   554   application is the implicit expanding of @{typ nat} numerals
       
   555   to @{term "0\<Colon>nat"} / @{const Suc} representation.  See further
       
   556   \secref{sec:ml}
       
   557 
       
   558   \emph{Generic preprocessors} provide a most general interface,
       
   559   transforming a list of function theorems to another
   550   transforming a list of function theorems to another
   560   list of function theorems, provided that neither the heading
   551   list of function theorems, provided that neither the heading
   561   constant nor its type change.  The @{term "0\<Colon>nat"} / @{const Suc}
   552   constant nor its type change.  The @{term "0\<Colon>nat"} / @{const Suc}
   562   pattern elimination implemented in
   553   pattern elimination implemented in
   563   theory @{text "EfficientNat"} (\secref{eff_nat}) uses this
   554   theory @{text "EfficientNat"} (\secref{eff_nat}) uses this
   564   interface.
   555   interface.
   565 
   556 
       
   557   \noindent The current setup of the preprocessor may be inspected using
       
   558   the @{text "\<PRINTCODESETUP>"} command.
       
   559 
   566   \begin{warn}
   560   \begin{warn}
   567     The order in which single preprocessing steps are carried
   561     Preprocessing is \emph{no} fix point process.  Keep this in mind when
   568     out currently is not specified; in particular, preprocessing
       
   569     is \emph{no} fix point process.  Keep this in mind when
       
   570     setting up the preprocessor.
   562     setting up the preprocessor.
   571 
   563 
   572     Further, the attribute \emph{code unfold}
   564     Further, the attribute \emph{code unfold}
   573     associated with the existing code generator also applies to
   565     associated with the existing code generator also applies to
   574     the new one: \emph{code unfold} implies \emph{code inline}.
   566     the new one: \emph{code unfold} implies \emph{code inline}.
  1100 text %mlref {*
  1092 text %mlref {*
  1101   \begin{mldecls}
  1093   \begin{mldecls}
  1102   @{index_ML Code.add_func: "thm -> theory -> theory"} \\
  1094   @{index_ML Code.add_func: "thm -> theory -> theory"} \\
  1103   @{index_ML Code.del_func: "thm -> theory -> theory"} \\
  1095   @{index_ML Code.del_func: "thm -> theory -> theory"} \\
  1104   @{index_ML Code.add_funcl: "string * thm list Susp.T -> theory -> theory"} \\
  1096   @{index_ML Code.add_funcl: "string * thm list Susp.T -> theory -> theory"} \\
  1105   @{index_ML Code.add_inline: "thm -> theory -> theory"} \\
  1097   @{index_ML Code.map_pre: "(MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory"} \\
  1106   @{index_ML Code.del_inline: "thm -> theory -> theory"} \\
  1098   @{index_ML Code.map_post: "(MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory"} \\
  1107   @{index_ML Code.add_inline_proc: "string * (theory -> cterm list -> thm list)
  1099   @{index_ML Code.add_functrans: "string * (theory -> thm list -> thm list)
  1108     -> theory -> theory"} \\
  1100     -> theory -> theory"} \\
  1109   @{index_ML Code.del_inline_proc: "string -> theory -> theory"} \\
  1101   @{index_ML Code.del_functrans: "string -> theory -> theory"} \\
  1110   @{index_ML Code.add_preproc: "string * (theory -> thm list -> thm list)
       
  1111     -> theory -> theory"} \\
       
  1112   @{index_ML Code.del_preproc: "string -> theory -> theory"} \\
       
  1113   @{index_ML Code.add_datatype: "(string * typ) list -> theory -> theory"} \\
  1102   @{index_ML Code.add_datatype: "(string * typ) list -> theory -> theory"} \\
  1114   @{index_ML Code.get_datatype: "theory -> string
  1103   @{index_ML Code.get_datatype: "theory -> string
  1115     -> (string * sort) list * (string * typ list) list"} \\
  1104     -> (string * sort) list * (string * typ list) list"} \\
  1116   @{index_ML Code.get_datatype_of_constr: "theory -> string -> string option"}
  1105   @{index_ML Code.get_datatype_of_constr: "theory -> string -> string option"}
  1117   \end{mldecls}
  1106   \end{mldecls}
  1126 
  1115 
  1127   \item @{ML Code.add_funcl}~@{text "(const, lthms)"}~@{text "thy"} adds
  1116   \item @{ML Code.add_funcl}~@{text "(const, lthms)"}~@{text "thy"} adds
  1128      suspended defining equations @{text lthms} for constant
  1117      suspended defining equations @{text lthms} for constant
  1129      @{text const} to executable content.
  1118      @{text const} to executable content.
  1130 
  1119 
  1131   \item @{ML Code.add_inline}~@{text "thm"}~@{text "thy"} adds
  1120   \item @{ML Code.map_pre}~@{text "f"}~@{text "thy"} changes
  1132      inlining theorem @{text thm} to executable content.
  1121      the preprocessor simpset.
  1133 
  1122 
  1134   \item @{ML Code.del_inline}~@{text "thm"}~@{text "thy"} remove
  1123     \item @{ML Code.add_functrans}~@{text "(name, f)"}~@{text "thy"} adds
  1135      inlining theorem @{text thm} from executable content, if present.
  1124      function transformator @{text f} (named @{text name}) to executable content;
  1136 
       
  1137   \item @{ML Code.add_inline_proc}~@{text "(name, f)"}~@{text "thy"} adds
       
  1138      inline procedure @{text f} (named @{text name}) to executable content;
       
  1139      @{text f} is a computation of rewrite rules dependent on
       
  1140      the current theory context and the list of all arguments
       
  1141      and right hand sides of the defining equations belonging
       
  1142      to a certain function definition.
       
  1143 
       
  1144   \item @{ML Code.del_inline_proc}~@{text "name"}~@{text "thy"} removes
       
  1145      inline procedure named @{text name} from executable content.
       
  1146 
       
  1147   \item @{ML Code.add_preproc}~@{text "(name, f)"}~@{text "thy"} adds
       
  1148      generic preprocessor @{text f} (named @{text name}) to executable content;
       
  1149      @{text f} is a transformation of the defining equations belonging
  1125      @{text f} is a transformation of the defining equations belonging
  1150      to a certain function definition, depending on the
  1126      to a certain function definition, depending on the
  1151      current theory context.
  1127      current theory context.
  1152 
  1128 
  1153   \item @{ML Code.del_preproc}~@{text "name"}~@{text "thy"} removes
  1129   \item @{ML Code.del_functrans}~@{text "name"}~@{text "thy"} removes
  1154      generic preprcoessor named @{text name} from executable content.
  1130      function transformator named @{text name} from executable content.
  1155 
  1131 
  1156   \item @{ML Code.add_datatype}~@{text cs}~@{text thy} adds
  1132   \item @{ML Code.add_datatype}~@{text cs}~@{text thy} adds
  1157      a datatype to executable content, with generation
  1133      a datatype to executable content, with generation
  1158      set @{text cs}.
  1134      set @{text cs}.
  1159 
  1135 
  1169 
  1145 
  1170 text %mlref {*
  1146 text %mlref {*
  1171   \begin{mldecls}
  1147   \begin{mldecls}
  1172   @{index_ML CodeUnit.read_const: "theory -> string -> string"} \\
  1148   @{index_ML CodeUnit.read_const: "theory -> string -> string"} \\
  1173   @{index_ML CodeUnit.head_func: "thm -> string * ((string * sort) list * typ)"} \\
  1149   @{index_ML CodeUnit.head_func: "thm -> string * ((string * sort) list * typ)"} \\
  1174   @{index_ML CodeUnit.rewrite_func: "thm list -> thm -> thm"} \\
  1150   @{index_ML CodeUnit.rewrite_func: "MetaSimplifier.simpset -> thm -> thm"} \\
  1175   \end{mldecls}
  1151   \end{mldecls}
  1176 
  1152 
  1177   \begin{description}
  1153   \begin{description}
  1178 
  1154 
  1179   \item @{ML CodeUnit.read_const}~@{text thy}~@{text s}
  1155   \item @{ML CodeUnit.read_const}~@{text thy}~@{text s}
  1180      reads a constant as a concrete term expression @{text s}.
  1156      reads a constant as a concrete term expression @{text s}.
  1181 
  1157 
  1182   \item @{ML CodeUnit.head_func}~@{text thm}
  1158   \item @{ML CodeUnit.head_func}~@{text thm}
  1183      extracts the constant and its type from a defining equation @{text thm}.
  1159      extracts the constant and its type from a defining equation @{text thm}.
  1184 
  1160 
  1185   \item @{ML CodeUnit.rewrite_func}~@{text rews}~@{text thm}
  1161   \item @{ML CodeUnit.rewrite_func}~@{text ss}~@{text thm}
  1186      rewrites a defining equation @{text thm} with a set of rewrite
  1162      rewrites a defining equation @{text thm} with a simpset @{text ss};
  1187      rules @{text rews}; only arguments and right hand side are rewritten,
  1163      only arguments and right hand side are rewritten,
  1188      not the head of the defining equation.
  1164      not the head of the defining equation.
  1189 
  1165 
  1190   \end{description}
  1166   \end{description}
  1191 
  1167 
  1192 *}
  1168 *}