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} |
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 |
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 |
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 {* |
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}" |
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 |