doc-src/IsarAdvanced/Codegen/Thy/document/Codegen.tex
changeset 22292 3b118010ec08
parent 22188 a63889770d57
child 22385 cc2be3315e72
equal deleted inserted replaced
22291:bfaba62cc92c 22292:3b118010ec08
    59   Isabelle system \cite{isa-tutorial}.
    59   Isabelle system \cite{isa-tutorial}.
    60   Generic in the sense that the
    60   Generic in the sense that the
    61   \qn{target language} for which code shall ultimately be
    61   \qn{target language} for which code shall ultimately be
    62   generated is not fixed but may be an arbitrary state-of-the-art
    62   generated is not fixed but may be an arbitrary state-of-the-art
    63   functional programming language (currently, the implementation
    63   functional programming language (currently, the implementation
    64   supports SML \cite{web:sml} and Haskell \cite{web:haskell}).
    64   supports SML \cite{SML}, OCaml \cite{OCaml} and Haskell \cite{haskell-revised-report}).
    65   We aim to provide a
    65   We aim to provide a
    66   versatile environment
    66   versatile environment
    67   suitable for software development and verification,
    67   suitable for software development and verification,
    68   structuring the process
    68   structuring the process
    69   of code generation into a small set of orthogonal principles
    69   of code generation into a small set of orthogonal principles
    70   while achieving a big coverage of application areas
    70   while achieving a big coverage of application areas
    71   with maximum flexibility.
    71   with maximum flexibility.
    72 
    72 
    73   For readers, some familiarity and experience
    73   For readers, some familiarity and experience
    74   with the the ingredients
    74   with the ingredients
    75   of the HOL \emph{Main} theory is assumed.%
    75   of the HOL \emph{Main} theory is assumed.%
    76 \end{isamarkuptext}%
    76 \end{isamarkuptext}%
    77 \isamarkuptrue%
    77 \isamarkuptrue%
    78 %
    78 %
    79 \isamarkupsubsection{Overview%
    79 \isamarkupsubsection{Overview%
   287 }
   287 }
   288 \isamarkuptrue%
   288 \isamarkuptrue%
   289 %
   289 %
   290 \begin{isamarkuptext}%
   290 \begin{isamarkuptext}%
   291 The list of all defining equations in a theory may be inspected
   291 The list of all defining equations in a theory may be inspected
   292   using the \isa{{\isasymPRINTCODETHMS}} command:%
   292   using the \isa{{\isasymPRINTCODESETUP}} command:%
   293 \end{isamarkuptext}%
   293 \end{isamarkuptext}%
   294 \isamarkuptrue%
   294 \isamarkuptrue%
   295 \isacommand{print{\isacharunderscore}codethms}\isamarkupfalse%
   295 \isacommand{print{\isacharunderscore}codesetup}\isamarkupfalse%
   296 %
   296 %
   297 \begin{isamarkuptext}%
   297 \begin{isamarkuptext}%
   298 \noindent which displays a table of constant with corresponding
   298 \noindent which displays a table of constant with corresponding
   299   defining equations (the additional stuff displayed
   299   defining equations (the additional stuff displayed
   300   shall not bother us for the moment). If this table does
   300   shall not bother us for the moment). If this table does
   392 \ fac\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}fac{\isacharunderscore}case{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
   392 \ fac\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}fac{\isacharunderscore}case{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
   393 \begin{isamarkuptext}%
   393 \begin{isamarkuptext}%
   394 \lstsml{Thy/examples/fac_case.ML}
   394 \lstsml{Thy/examples/fac_case.ML}
   395 
   395 
   396   \begin{warn}
   396   \begin{warn}
   397     Some statements in this section have to be treated with some
   397     The attributes \emph{code} and \emph{code del}
   398     caution. First, since the HOL function package is still
       
   399     under development, its setup with respect to code generation
       
   400     may differ from what is presumed here.
       
   401     Further, the attributes \emph{code} and \emph{code del}
       
   402     associated with the existing code generator also apply to
   398     associated with the existing code generator also apply to
   403     the new one: \emph{code} implies \emph{code func},
   399     the new one: \emph{code} implies \emph{code func},
   404     and \emph{code del} implies \emph{code nofunc}.
   400     and \emph{code del} implies \emph{code nofunc}.
   405   \end{warn}%
   401   \end{warn}%
   406 \end{isamarkuptext}%
   402 \end{isamarkuptext}%
   487   some Haskell systems enforce, each module ends
   483   some Haskell systems enforce, each module ends
   488   up in a single file. The module hierarchy is reflected in
   484   up in a single file. The module hierarchy is reflected in
   489   the file system, with root given by the user.%
   485   the file system, with root given by the user.%
   490 \end{isamarkuptext}%
   486 \end{isamarkuptext}%
   491 \isamarkuptrue%
   487 \isamarkuptrue%
   492 %
       
   493 \isadelimML
       
   494 %
       
   495 \endisadelimML
       
   496 %
       
   497 \isatagML
       
   498 \isacommand{ML}\isamarkupfalse%
       
   499 \ {\isacharverbatimopen}\ set\ Toplevel{\isachardot}debug\ {\isacharverbatimclose}%
       
   500 \endisatagML
       
   501 {\isafoldML}%
       
   502 %
       
   503 \isadelimML
       
   504 %
       
   505 \endisadelimML
       
   506 \isanewline
       
   507 \isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
   488 \isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
   508 \ dummy\ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}examples{\isacharslash}{\isachardoublequoteclose}{\isacharparenright}%
   489 \ dummy\ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}examples{\isacharslash}{\isachardoublequoteclose}{\isacharparenright}%
   509 \begin{isamarkuptext}%
   490 \begin{isamarkuptext}%
   510 \lsthaskell{Thy/examples/Codegen.hs}
   491 \lsthaskell{Thy/examples/Codegen.hs}
   511 
   492 
   516 \isamarkuptrue%
   497 \isamarkuptrue%
   517 \isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
   498 \isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
   518 \ dummy\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}class{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
   499 \ dummy\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}class{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
   519 \begin{isamarkuptext}%
   500 \begin{isamarkuptext}%
   520 \lstsml{Thy/examples/class.ML}%
   501 \lstsml{Thy/examples/class.ML}%
       
   502 \end{isamarkuptext}%
       
   503 \isamarkuptrue%
       
   504 %
       
   505 \begin{isamarkuptext}%
       
   506 or in OCaml:%
       
   507 \end{isamarkuptext}%
       
   508 \isamarkuptrue%
       
   509 \isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
       
   510 \ dummy\ {\isacharparenleft}OCaml\ {\isachardoublequoteopen}examples{\isacharslash}class{\isachardot}ocaml{\isachardoublequoteclose}{\isacharparenright}%
       
   511 \begin{isamarkuptext}%
       
   512 \lstsml{Thy/examples/class.ocaml}%
   521 \end{isamarkuptext}%
   513 \end{isamarkuptext}%
   522 \isamarkuptrue%
   514 \isamarkuptrue%
   523 %
   515 %
   524 \isamarkupsubsection{Incremental code generation%
   516 \isamarkupsubsection{Incremental code generation%
   525 }
   517 }
   537   list of serialization expressions may be dropped.  If no
   529   list of serialization expressions may be dropped.  If no
   538   serialization expressions are given, only abstract code
   530   serialization expressions are given, only abstract code
   539   is generated and cached; if no constants are given, the
   531   is generated and cached; if no constants are given, the
   540   current cache is serialized.
   532   current cache is serialized.
   541 
   533 
   542   For explorative purpose, an extended version of the
   534   For explorative purpose, the
   543   \isa{{\isasymCODEGEN}} command may prove useful:%
   535   \isa{{\isasymCODETHMS}} command may prove useful:%
   544 \end{isamarkuptext}%
   536 \end{isamarkuptext}%
   545 \isamarkuptrue%
   537 \isamarkuptrue%
   546 \isacommand{print{\isacharunderscore}codethms}\isamarkupfalse%
   538 \isacommand{code{\isacharunderscore}thms}\isamarkupfalse%
   547 \ {\isacharparenleft}{\isacharparenright}%
   539 %
   548 \begin{isamarkuptext}%
   540 \begin{isamarkuptext}%
   549 \noindent print all cached defining equations (i.e.~\emph{after}
   541 \noindent print all cached defining equations (i.e.~\emph{after}
   550   any applied transformation). Inside the brackets a
   542   any applied transformation).  A
   551   list of constants may be given; their function
   543   list of constants may be given; their function
   552   equations are added to the cache if not already present.%
   544   equations are added to the cache if not already present.%
   553 \end{isamarkuptext}%
   545 \end{isamarkuptext}%
   554 \isamarkuptrue%
   546 \isamarkuptrue%
   555 %
   547 %
   685 %
   677 %
   686 \end{itemize}
   678 \end{itemize}
   687 %
   679 %
   688 \begin{isamarkuptext}%
   680 \begin{isamarkuptext}%
   689 The current set of inline theorems may be inspected using
   681 The current set of inline theorems may be inspected using
   690   the \isa{{\isasymPRINTCODETHMS}} command.
   682   the \isa{{\isasymPRINTCODESETUP}} command.
   691 
   683 
   692   \emph{Inline procedures} are a generalized version of inline
   684   \emph{Inline procedures} are a generalized version of inline
   693   theorems written in ML -- rewrite rules are generated dependent
   685   theorems written in ML -- rewrite rules are generated dependent
   694   on the function theorems for a certain function.  One
   686   on the function theorems for a certain function.  One
   695   application is the implicit expanding of \isa{nat} numerals
   687   application is the implicit expanding of \isa{nat} numerals
   870   during pretty printing.
   862   during pretty printing.
   871 
   863 
   872   So far, we did only provide more idiomatic serializations for
   864   So far, we did only provide more idiomatic serializations for
   873   constructs which would be executable on their own.  Target-specific
   865   constructs which would be executable on their own.  Target-specific
   874   serializations may also be used to \emph{implement} constructs
   866   serializations may also be used to \emph{implement} constructs
   875   which have no implicit notion of executability.  For example,
   867   which have no explicit notion of executability.  For example,
   876   take the HOL integers:%
   868   take the HOL integers:%
   877 \end{isamarkuptext}%
   869 \end{isamarkuptext}%
   878 \isamarkuptrue%
   870 \isamarkuptrue%
   879 \isacommand{definition}\isamarkupfalse%
   871 \isacommand{definition}\isamarkupfalse%
   880 \isanewline
   872 \isanewline
  1376 \begin{isamarkuptext}%
  1368 \begin{isamarkuptext}%
  1377 Then code generation for \isa{dummy{\isacharunderscore}set} will fail.
  1369 Then code generation for \isa{dummy{\isacharunderscore}set} will fail.
  1378   Why? A glimpse at the defining equations will offer:%
  1370   Why? A glimpse at the defining equations will offer:%
  1379 \end{isamarkuptext}%
  1371 \end{isamarkuptext}%
  1380 \isamarkuptrue%
  1372 \isamarkuptrue%
  1381 \isacommand{print{\isacharunderscore}codethms}\isamarkupfalse%
  1373 \isacommand{code{\isacharunderscore}thms}\isamarkupfalse%
  1382 \ {\isacharparenleft}insert{\isacharparenright}%
  1374 \ insert%
  1383 \begin{isamarkuptext}%
  1375 \begin{isamarkuptext}%
  1384 This reveals the defining equation \isa{insert\ {\isacharquery}a\ {\isacharquery}B\ {\isasymequiv}\ {\isacharbraceleft}x{\isachardot}\ x\ {\isacharequal}\ {\isacharquery}a{\isacharbraceright}\ {\isasymunion}\ {\isacharquery}B}
  1376 This reveals the defining equation \isa{insert\ {\isacharquery}a\ {\isacharquery}B\ {\isasymequiv}\ {\isacharbraceleft}x{\isachardot}\ x\ {\isacharequal}\ {\isacharquery}a{\isacharbraceright}\ {\isasymunion}\ {\isacharquery}B}
  1385   for \isa{insert}, which is operationally meaningless
  1377   for \isa{insert}, which is operationally meaningless
  1386   but forces an equality constraint on the set members
  1378   but forces an equality constraint on the set members
  1387   (which is not satisfiable if the set members are functions).
  1379   (which is not satisfiable if the set members are functions).
  1427   Reflexive defining equations by convention are dropped.
  1419   Reflexive defining equations by convention are dropped.
  1428   But their presence prevents primitive definitions to be
  1420   But their presence prevents primitive definitions to be
  1429   used as defining equations:%
  1421   used as defining equations:%
  1430 \end{isamarkuptext}%
  1422 \end{isamarkuptext}%
  1431 \isamarkuptrue%
  1423 \isamarkuptrue%
  1432 \isacommand{print{\isacharunderscore}codethms}\isamarkupfalse%
  1424 \isacommand{code{\isacharunderscore}thms}\isamarkupfalse%
  1433 \ {\isacharparenleft}insert{\isacharparenright}%
  1425 \ insert%
  1434 \begin{isamarkuptext}%
  1426 \begin{isamarkuptext}%
  1435 will show \emph{no} defining equations for insert.
  1427 will show \emph{no} defining equations for insert.
  1436 
  1428 
  1437   Note that the sort constraints of reflexive equations
  1429   Note that the sort constraints of reflexive equations
  1438   are considered; so%
  1430   are considered; so%
  1675 %
  1667 %
  1676 \isadelimmlref
  1668 \isadelimmlref
  1677 %
  1669 %
  1678 \endisadelimmlref
  1670 \endisadelimmlref
  1679 %
  1671 %
  1680 \isamarkupsubsubsection{Executable content%
  1672 \isamarkupsubsubsection{Managing executable content%
  1681 }
  1673 }
  1682 \isamarkuptrue%
  1674 \isamarkuptrue%
  1683 %
  1675 %
  1684 \isadelimmlref
  1676 \isadelimmlref
  1685 %
  1677 %
  1771 %
  1763 %
  1772 \isadelimmlref
  1764 \isadelimmlref
  1773 %
  1765 %
  1774 \endisadelimmlref
  1766 \endisadelimmlref
  1775 %
  1767 %
  1776 \isamarkupsubsection{defining equation systems: codegen\_funcgr.ML%
  1768 \isamarkupsubsection{Auxiliary%
  1777 }
       
  1778 \isamarkuptrue%
       
  1779 %
       
  1780 \begin{isamarkuptext}%
       
  1781 Out of the executable content of a theory, a normalized
       
  1782   defining equation systems may be constructed containing
       
  1783   function definitions for constants.  The system is cached
       
  1784   until its underlying executable content changes.%
       
  1785 \end{isamarkuptext}%
       
  1786 \isamarkuptrue%
       
  1787 %
       
  1788 \isadelimmlref
       
  1789 %
       
  1790 \endisadelimmlref
       
  1791 %
       
  1792 \isatagmlref
       
  1793 %
       
  1794 \begin{isamarkuptext}%
       
  1795 \begin{mldecls}
       
  1796   \indexmltype{CodegenFuncgr.T}\verb|type CodegenFuncgr.T| \\
       
  1797   \indexml{CodegenFuncgr.make}\verb|CodegenFuncgr.make: theory -> CodegenConsts.const list -> CodegenFuncgr.T| \\
       
  1798   \indexml{CodegenFuncgr.funcs}\verb|CodegenFuncgr.funcs: CodegenFuncgr.T -> CodegenConsts.const -> thm list| \\
       
  1799   \indexml{CodegenFuncgr.typ}\verb|CodegenFuncgr.typ: CodegenFuncgr.T -> CodegenConsts.const -> typ| \\
       
  1800   \indexml{CodegenFuncgr.deps}\verb|CodegenFuncgr.deps: CodegenFuncgr.T|\isasep\isanewline%
       
  1801 \verb|    -> CodegenConsts.const list -> CodegenConsts.const list list| \\
       
  1802   \indexml{CodegenFuncgr.all}\verb|CodegenFuncgr.all: CodegenFuncgr.T -> CodegenConsts.const list|
       
  1803   \end{mldecls}
       
  1804 
       
  1805   \begin{description}
       
  1806 
       
  1807   \item \verb|CodegenFuncgr.T| represents
       
  1808     a normalized defining equation system.
       
  1809 
       
  1810   \item \verb|CodegenFuncgr.make|~\isa{thy}~\isa{cs}
       
  1811     returns a normalized defining equation system,
       
  1812     with the assertion that it contains any function
       
  1813     definition for constants \isa{cs} (if existing).
       
  1814 
       
  1815   \item \verb|CodegenFuncgr.funcs|~\isa{funcgr}~\isa{c}
       
  1816     retrieves function definition for constant \isa{c}.
       
  1817 
       
  1818   \item \verb|CodegenFuncgr.typ|~\isa{funcgr}~\isa{c}
       
  1819     retrieves function type for constant \isa{c}.
       
  1820 
       
  1821   \item \verb|CodegenFuncgr.deps|~\isa{funcgr}~\isa{cs}
       
  1822     returns the transitive closure of dependencies for
       
  1823     constants \isa{cs} as a partitioning where each partition
       
  1824     corresponds to a strongly connected component of
       
  1825     dependencies and any partition does \emph{not}
       
  1826     depend on partitions further left.
       
  1827 
       
  1828   \item \verb|CodegenFuncgr.all|~\isa{funcgr}
       
  1829     returns all currently represented constants.
       
  1830 
       
  1831   \end{description}%
       
  1832 \end{isamarkuptext}%
       
  1833 \isamarkuptrue%
       
  1834 %
       
  1835 \endisatagmlref
       
  1836 {\isafoldmlref}%
       
  1837 %
       
  1838 \isadelimmlref
       
  1839 %
       
  1840 \endisadelimmlref
       
  1841 %
       
  1842 \isamarkupsubsection{Further auxiliary%
       
  1843 }
  1769 }
  1844 \isamarkuptrue%
  1770 \isamarkuptrue%
  1845 %
  1771 %
  1846 \isadelimmlref
  1772 \isadelimmlref
  1847 %
  1773 %
  1854   \indexml{CodegenConsts.const-ord}\verb|CodegenConsts.const_ord: CodegenConsts.const * CodegenConsts.const -> order| \\
  1780   \indexml{CodegenConsts.const-ord}\verb|CodegenConsts.const_ord: CodegenConsts.const * CodegenConsts.const -> order| \\
  1855   \indexml{CodegenConsts.eq-const}\verb|CodegenConsts.eq_const: CodegenConsts.const * CodegenConsts.const -> bool| \\
  1781   \indexml{CodegenConsts.eq-const}\verb|CodegenConsts.eq_const: CodegenConsts.const * CodegenConsts.const -> bool| \\
  1856   \indexml{CodegenConsts.consts-of}\verb|CodegenConsts.consts_of: theory -> term -> CodegenConsts.const list| \\
  1782   \indexml{CodegenConsts.consts-of}\verb|CodegenConsts.consts_of: theory -> term -> CodegenConsts.const list| \\
  1857   \indexml{CodegenConsts.read-const}\verb|CodegenConsts.read_const: theory -> string -> CodegenConsts.const| \\
  1783   \indexml{CodegenConsts.read-const}\verb|CodegenConsts.read_const: theory -> string -> CodegenConsts.const| \\
  1858   \indexmlstructure{CodegenConsts.Consttab}\verb|structure CodegenConsts.Consttab| \\
  1784   \indexmlstructure{CodegenConsts.Consttab}\verb|structure CodegenConsts.Consttab| \\
  1859   \indexmlstructure{CodegenFuncgr.Constgraph}\verb|structure CodegenFuncgr.Constgraph| \\
       
  1860   \indexml{CodegenFunc.typ-func}\verb|CodegenFunc.typ_func: thm -> typ| \\
  1785   \indexml{CodegenFunc.typ-func}\verb|CodegenFunc.typ_func: thm -> typ| \\
  1861   \indexml{CodegenFunc.rewrite-func}\verb|CodegenFunc.rewrite_func: thm list -> thm -> thm| \\
  1786   \indexml{CodegenFunc.rewrite-func}\verb|CodegenFunc.rewrite_func: thm list -> thm -> thm| \\
  1862   \end{mldecls}
  1787   \end{mldecls}
  1863 
  1788 
  1864   \begin{description}
  1789   \begin{description}
  1865 
  1790 
  1866   \item \verb|CodegenConsts.const_ord|,~\verb|CodegenConsts.eq_const|
  1791   \item \verb|CodegenConsts.const_ord|,~\verb|CodegenConsts.eq_const|
  1867      provide order and equality on constant identifiers.
  1792      provide order and equality on constant identifiers.
  1868 
  1793 
  1869   \item \verb|CodegenConsts.Consttab|,~\verb|CodegenFuncgr.Constgraph|
  1794   \item \verb|CodegenConsts.Consttab|
  1870      provide advanced data structures with constant identifiers as keys.
  1795      provides table structures with constant identifiers as keys.
  1871 
  1796 
  1872   \item \verb|CodegenConsts.consts_of|~\isa{thy}~\isa{t}
  1797   \item \verb|CodegenConsts.consts_of|~\isa{thy}~\isa{t}
  1873      returns all constant identifiers mentioned in a term \isa{t}.
  1798      returns all constant identifiers mentioned in a term \isa{t}.
  1874 
  1799 
  1875   \item \verb|CodegenConsts.read_const|~\isa{thy}~\isa{s}
  1800   \item \verb|CodegenConsts.read_const|~\isa{thy}~\isa{s}