doc-src/IsarAdvanced/Codegen/Thy/document/Codegen.tex
changeset 28143 e5c6c4aac52c
parent 27609 b23c9ad0fe7d
equal deleted inserted replaced
28142:cf8da9bbc584 28143:e5c6c4aac52c
   379 \begin{isamarkuptext}%
   379 \begin{isamarkuptext}%
   380 \noindent This theorem now is used for generating code:
   380 \noindent This theorem now is used for generating code:
   381 
   381 
   382   \lstsml{Thy/examples/pick1.ML}
   382   \lstsml{Thy/examples/pick1.ML}
   383 
   383 
   384   \noindent It might be convenient to remove the pointless original
   384   \noindent The policy is that \emph{default equations} stemming from
   385   equation, using the \emph{func del} attribute:%
   385   \isa{{\isasymDEFINITION}},
   386 \end{isamarkuptext}%
   386   \isa{{\isasymPRIMREC}}, \isa{{\isasymFUN}},
   387 \isamarkuptrue%
   387   \isa{{\isasymFUNCTION}}, \isa{{\isasymCONSTDEFS}},
   388 \isacommand{lemmas}\isamarkupfalse%
   388   \isa{{\isasymRECDEF}} statements are discarded as soon as an
   389 \ {\isacharbrackleft}code\ func\ del{\isacharbrackright}\ {\isacharequal}\ pick{\isachardot}simps\ \isanewline
   389   equation is explicitly selected by means of \emph{code func}.
   390 \isanewline
   390   Further applications of \emph{code func} add theorems incrementally,
   391 \isacommand{export{\isacharunderscore}code}\isamarkupfalse%
   391   but syntactic redundancies are implicitly dropped.  For example,
   392 \ pick\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}pick{\isadigit{2}}{\isachardot}ML{\isachardoublequoteclose}%
       
   393 \begin{isamarkuptext}%
       
   394 \lstsml{Thy/examples/pick2.ML}
       
   395 
       
   396   \noindent Syntactic redundancies are implicitly dropped. For example,
       
   397   using a modified version of the \isa{fac} function
   392   using a modified version of the \isa{fac} function
   398   as defining equation, the then redundant (since
   393   as defining equation, the then redundant (since
   399   syntactically subsumed) original defining equations
   394   syntactically subsumed) original defining equations
   400   are dropped, resulting in a warning:%
   395   are dropped.
   401 \end{isamarkuptext}%
       
   402 \isamarkuptrue%
       
   403 \isacommand{lemma}\isamarkupfalse%
       
   404 \ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
       
   405 \ \ {\isachardoublequoteopen}fac\ n\ {\isacharequal}\ {\isacharparenleft}case\ n\ of\ {\isadigit{0}}\ {\isasymRightarrow}\ {\isadigit{1}}\ {\isacharbar}\ Suc\ m\ {\isasymRightarrow}\ n\ {\isacharasterisk}\ fac\ m{\isacharparenright}{\isachardoublequoteclose}\isanewline
       
   406 %
       
   407 \isadelimproof
       
   408 \ \ %
       
   409 \endisadelimproof
       
   410 %
       
   411 \isatagproof
       
   412 \isacommand{by}\isamarkupfalse%
       
   413 \ {\isacharparenleft}cases\ n{\isacharparenright}\ simp{\isacharunderscore}all%
       
   414 \endisatagproof
       
   415 {\isafoldproof}%
       
   416 %
       
   417 \isadelimproof
       
   418 \isanewline
       
   419 %
       
   420 \endisadelimproof
       
   421 \isanewline
       
   422 \isacommand{export{\isacharunderscore}code}\isamarkupfalse%
       
   423 \ fac\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}fac{\isacharunderscore}case{\isachardot}ML{\isachardoublequoteclose}%
       
   424 \begin{isamarkuptext}%
       
   425 \lstsml{Thy/examples/fac_case.ML}
       
   426 
   396 
   427   \begin{warn}
   397   \begin{warn}
   428     The attributes \emph{code} and \emph{code del}
   398     The attributes \emph{code} and \emph{code del}
   429     associated with the existing code generator also apply to
   399     associated with the existing code generator also apply to
   430     the new one: \emph{code} implies \emph{code func},
   400     the new one: \emph{code} implies \emph{code func},
   618   out: \emph{preprocessing}.  In essence, the preprocessor
   588   out: \emph{preprocessing}.  In essence, the preprocessor
   619   consists of two components: a \emph{simpset} and \emph{function transformers}.
   589   consists of two components: a \emph{simpset} and \emph{function transformers}.
   620 
   590 
   621   The \emph{simpset} allows to employ the full generality of the Isabelle
   591   The \emph{simpset} allows to employ the full generality of the Isabelle
   622   simplifier.  Due to the interpretation of theorems
   592   simplifier.  Due to the interpretation of theorems
   623   of defining equations, rewrites are applied to the right
   593   as defining equations, rewrites are applied to the right
   624   hand side and the arguments of the left hand side of an
   594   hand side and the arguments of the left hand side of an
   625   equation, but never to the constant heading the left hand side.
   595   equation, but never to the constant heading the left hand side.
   626   An important special case are \emph{inline theorems} which may be
   596   An important special case are \emph{inline theorems} which may be
   627   declared an undeclared using the
   597   declared an undeclared using the
   628   \emph{code inline} or \emph{code inline del} attribute respectively.
   598   \emph{code inline} or \emph{code inline del} attribute respectively.
   701 \emph{Function transformers} provide a very general interface,
   671 \emph{Function transformers} provide a very general interface,
   702   transforming a list of function theorems to another
   672   transforming a list of function theorems to another
   703   list of function theorems, provided that neither the heading
   673   list of function theorems, provided that neither the heading
   704   constant nor its type change.  The \isa{{\isadigit{0}}} / \isa{Suc}
   674   constant nor its type change.  The \isa{{\isadigit{0}}} / \isa{Suc}
   705   pattern elimination implemented in
   675   pattern elimination implemented in
   706   theory \isa{Efficient{\isacharunderscore}Nat} (\secref{eff_nat}) uses this
   676   theory \isa{Efficient{\isacharunderscore}Nat} (see \secref{eff_nat}) uses this
   707   interface.
   677   interface.
   708 
   678 
   709   \noindent The current setup of the preprocessor may be inspected using
   679   \noindent The current setup of the preprocessor may be inspected using
   710   the \isa{{\isasymPRINTCODESETUP}} command.
   680   the \isa{{\isasymPRINTCODESETUP}} command.
   711 
   681 
   982 
   952 
   983   In some cases, it may be convenient to alter or
   953   In some cases, it may be convenient to alter or
   984   extend this table;  as an example, we will develope an alternative
   954   extend this table;  as an example, we will develope an alternative
   985   representation of natural numbers as binary digits, whose
   955   representation of natural numbers as binary digits, whose
   986   size does increase logarithmically with its value, not linear
   956   size does increase logarithmically with its value, not linear
   987   \footnote{Indeed, the \isa{Efficient{\isacharunderscore}Nat} theory \ref{eff_nat}
   957   \footnote{Indeed, the \isa{Efficient{\isacharunderscore}Nat} theory (see \ref{eff_nat})
   988     does something similar}.  First, the digit representation:%
   958     does something similar}.  First, the digit representation:%
   989 \end{isamarkuptext}%
   959 \end{isamarkuptext}%
   990 \isamarkuptrue%
   960 \isamarkuptrue%
   991 \isacommand{definition}\isamarkupfalse%
   961 \isacommand{definition}\isamarkupfalse%
   992 \ Dig{\isadigit{0}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
   962 \ Dig{\isadigit{0}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
  1420 \begin{isamarkuptext}%
  1390 \begin{isamarkuptext}%
  1421 The serializer provides ML interfaces to set up
  1391 The serializer provides ML interfaces to set up
  1422   pretty serializations for expressions like lists, numerals
  1392   pretty serializations for expressions like lists, numerals
  1423   and characters;  these are
  1393   and characters;  these are
  1424   monolithic stubs and should only be used with the
  1394   monolithic stubs and should only be used with the
  1425   theories introduces in \secref{sec:library}.%
  1395   theories introduced in \secref{sec:library}.%
  1426 \end{isamarkuptext}%
  1396 \end{isamarkuptext}%
  1427 \isamarkuptrue%
  1397 \isamarkuptrue%
  1428 %
  1398 %
  1429 \isamarkupsubsection{Cyclic module dependencies%
  1399 \isamarkupsubsection{Cyclic module dependencies%
  1430 }
  1400 }
  1535      \isa{const} to executable content.
  1505      \isa{const} to executable content.
  1536 
  1506 
  1537   \item \verb|Code.map_pre|~\isa{f}~\isa{thy} changes
  1507   \item \verb|Code.map_pre|~\isa{f}~\isa{thy} changes
  1538      the preprocessor simpset.
  1508      the preprocessor simpset.
  1539 
  1509 
  1540     \item \verb|Code.add_functrans|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds
  1510   \item \verb|Code.add_functrans|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds
  1541      function transformer \isa{f} (named \isa{name}) to executable content;
  1511      function transformer \isa{f} (named \isa{name}) to executable content;
  1542      \isa{f} is a transformer of the defining equations belonging
  1512      \isa{f} is a transformer of the defining equations belonging
  1543      to a certain function definition, depending on the
  1513      to a certain function definition, depending on the
  1544      current theory context.  Returning \isa{NONE} indicates that no
  1514      current theory context.  Returning \isa{NONE} indicates that no
  1545      transformation took place;  otherwise, the whole process will be iterated
  1515      transformation took place;  otherwise, the whole process will be iterated
  1578 %
  1548 %
  1579 \isatagmlref
  1549 \isatagmlref
  1580 %
  1550 %
  1581 \begin{isamarkuptext}%
  1551 \begin{isamarkuptext}%
  1582 \begin{mldecls}
  1552 \begin{mldecls}
  1583   \indexml{CodeUnit.read\_const}\verb|CodeUnit.read_const: theory -> string -> string| \\
  1553   \indexml{Code\_Unit.read\_const}\verb|Code_Unit.read_const: theory -> string -> string| \\
  1584   \indexml{CodeUnit.head\_func}\verb|CodeUnit.head_func: thm -> string * ((string * sort) list * typ)| \\
  1554   \indexml{Code\_Unit.head\_func}\verb|Code_Unit.head_func: thm -> string * ((string * sort) list * typ)| \\
  1585   \indexml{CodeUnit.rewrite\_func}\verb|CodeUnit.rewrite_func: MetaSimplifier.simpset -> thm -> thm| \\
  1555   \indexml{Code\_Unit.rewrite\_func}\verb|Code_Unit.rewrite_func: MetaSimplifier.simpset -> thm -> thm| \\
  1586   \end{mldecls}
  1556   \end{mldecls}
  1587 
  1557 
  1588   \begin{description}
  1558   \begin{description}
  1589 
  1559 
  1590   \item \verb|CodeUnit.read_const|~\isa{thy}~\isa{s}
  1560   \item \verb|Code_Unit.read_const|~\isa{thy}~\isa{s}
  1591      reads a constant as a concrete term expression \isa{s}.
  1561      reads a constant as a concrete term expression \isa{s}.
  1592 
  1562 
  1593   \item \verb|CodeUnit.head_func|~\isa{thm}
  1563   \item \verb|Code_Unit.head_func|~\isa{thm}
  1594      extracts the constant and its type from a defining equation \isa{thm}.
  1564      extracts the constant and its type from a defining equation \isa{thm}.
  1595 
  1565 
  1596   \item \verb|CodeUnit.rewrite_func|~\isa{ss}~\isa{thm}
  1566   \item \verb|Code_Unit.rewrite_func|~\isa{ss}~\isa{thm}
  1597      rewrites a defining equation \isa{thm} with a simpset \isa{ss};
  1567      rewrites a defining equation \isa{thm} with a simpset \isa{ss};
  1598      only arguments and right hand side are rewritten,
  1568      only arguments and right hand side are rewritten,
  1599      not the head of the defining equation.
  1569      not the head of the defining equation.
  1600 
  1570 
  1601   \end{description}%
  1571   \end{description}%