| author | wenzelm | 
| Tue, 20 May 2025 17:02:10 +0200 | |
| changeset 82637 | c6c20afb29c2 | 
| parent 77811 | ae9e6218443d | 
| permissions | -rw-r--r-- | 
| 59720 | 1 | (* Title: HOL/Library/Code_Test.thy | 
| 64578 | 2 | Author: Andreas Lochbihler, ETH Zürich | 
| 58039 | 3 | |
| 64578 | 4 | Test infrastructure for the code generator. | 
| 58039 | 5 | *) | 
| 6 | ||
| 77811 | 7 | section \<open>Test infrastructure for the code generator\<close> | 
| 8 | ||
| 58039 | 9 | theory Code_Test | 
| 10 | imports Main | |
| 58348 
2d47c7d10b62
add target language evaluators for the value command;
 Andreas Lochbihler parents: 
58310diff
changeset | 11 | keywords "test_code" :: diag | 
| 58039 | 12 | begin | 
| 13 | ||
| 69593 | 14 | subsection \<open>YXML encoding for \<^typ>\<open>Code_Evaluation.term\<close>\<close> | 
| 58039 | 15 | |
| 58626 | 16 | datatype (plugins del: code size "quickcheck") yxml_of_term = YXML | 
| 58039 | 17 | |
| 18 | lemma yot_anything: "x = (y :: yxml_of_term)" | |
| 19 | by(cases x y rule: yxml_of_term.exhaust[case_product yxml_of_term.exhaust])(simp) | |
| 20 | ||
| 21 | definition yot_empty :: yxml_of_term where [code del]: "yot_empty = YXML" | |
| 22 | definition yot_literal :: "String.literal \<Rightarrow> yxml_of_term" | |
| 23 | where [code del]: "yot_literal _ = YXML" | |
| 24 | definition yot_append :: "yxml_of_term \<Rightarrow> yxml_of_term \<Rightarrow> yxml_of_term" | |
| 25 | where [code del]: "yot_append _ _ = YXML" | |
| 26 | definition yot_concat :: "yxml_of_term list \<Rightarrow> yxml_of_term" | |
| 27 | where [code del]: "yot_concat _ = YXML" | |
| 28 | ||
| 69593 | 29 | text \<open>Serialise \<^typ>\<open>yxml_of_term\<close> to native string of target language\<close> | 
| 58039 | 30 | |
| 31 | code_printing type_constructor yxml_of_term | |
| 32 | \<rightharpoonup> (SML) "string" | |
| 33 | and (OCaml) "string" | |
| 34 | and (Haskell) "String" | |
| 35 | and (Scala) "String" | |
| 36 | | constant yot_empty | |
| 37 | \<rightharpoonup> (SML) "\"\"" | |
| 38 | and (OCaml) "\"\"" | |
| 39 | and (Haskell) "\"\"" | |
| 40 | and (Scala) "\"\"" | |
| 41 | | constant yot_literal | |
| 42 | \<rightharpoonup> (SML) "_" | |
| 43 | and (OCaml) "_" | |
| 44 | and (Haskell) "_" | |
| 45 | and (Scala) "_" | |
| 46 | | constant yot_append | |
| 47 | \<rightharpoonup> (SML) "String.concat [(_), (_)]" | |
| 48 | and (OCaml) "String.concat \"\" [(_); (_)]" | |
| 49 | and (Haskell) infixr 5 "++" | |
| 50 | and (Scala) infixl 5 "+" | |
| 51 | | constant yot_concat | |
| 52 | \<rightharpoonup> (SML) "String.concat" | |
| 53 | and (OCaml) "String.concat \"\"" | |
| 54 | and (Haskell) "Prelude.concat" | |
| 55 | and (Scala) "_.mkString(\"\")" | |
| 56 | ||
| 60500 | 57 | text \<open> | 
| 63680 | 58 | Stripped-down implementations of Isabelle's XML tree with YXML encoding as | 
| 59 | defined in \<^file>\<open>~~/src/Pure/PIDE/xml.ML\<close>, \<^file>\<open>~~/src/Pure/PIDE/yxml.ML\<close> | |
| 69593 | 60 | sufficient to encode \<^typ>\<open>Code_Evaluation.term\<close> as in | 
| 63680 | 61 | \<^file>\<open>~~/src/Pure/term_xml.ML\<close>. | 
| 60500 | 62 | \<close> | 
| 58039 | 63 | |
| 58626 | 64 | datatype (plugins del: code size "quickcheck") xml_tree = XML_Tree | 
| 58039 | 65 | |
| 66 | lemma xml_tree_anything: "x = (y :: xml_tree)" | |
| 67 | by(cases x y rule: xml_tree.exhaust[case_product xml_tree.exhaust])(simp) | |
| 68 | ||
| 69 | context begin | |
| 60500 | 70 | local_setup \<open>Local_Theory.map_background_naming (Name_Space.mandatory_path "xml")\<close> | 
| 58039 | 71 | |
| 72 | type_synonym attributes = "(String.literal \<times> String.literal) list" | |
| 73 | type_synonym body = "xml_tree list" | |
| 74 | ||
| 75 | definition Elem :: "String.literal \<Rightarrow> attributes \<Rightarrow> xml_tree list \<Rightarrow> xml_tree" | |
| 76 | where [code del]: "Elem _ _ _ = XML_Tree" | |
| 77 | ||
| 78 | definition Text :: "String.literal \<Rightarrow> xml_tree" | |
| 79 | where [code del]: "Text _ = XML_Tree" | |
| 80 | ||
| 81 | definition node :: "xml_tree list \<Rightarrow> xml_tree" | |
| 82 | where "node ts = Elem (STR '':'') [] ts" | |
| 83 | ||
| 84 | definition tagged :: "String.literal \<Rightarrow> String.literal option \<Rightarrow> xml_tree list \<Rightarrow> xml_tree" | |
| 85 | where "tagged tag x ts = Elem tag (case x of None \<Rightarrow> [] | Some x' \<Rightarrow> [(STR ''0'', x')]) ts" | |
| 86 | ||
| 87 | definition list where "list f xs = map (node \<circ> f) xs" | |
| 88 | ||
| 68033 
ad4b8b6892c3
uniform tagging for printable and non-printable literals
 haftmann parents: 
68028diff
changeset | 89 | definition X :: yxml_of_term where "X = yot_literal (STR 0x05)" | 
| 
ad4b8b6892c3
uniform tagging for printable and non-printable literals
 haftmann parents: 
68028diff
changeset | 90 | definition Y :: yxml_of_term where "Y = yot_literal (STR 0x06)" | 
| 58039 | 91 | definition XY :: yxml_of_term where "XY = yot_append X Y" | 
| 92 | definition XYX :: yxml_of_term where "XYX = yot_append XY X" | |
| 93 | ||
| 94 | end | |
| 95 | ||
| 96 | code_datatype xml.Elem xml.Text | |
| 97 | ||
| 98 | definition yxml_string_of_xml_tree :: "xml_tree \<Rightarrow> yxml_of_term \<Rightarrow> yxml_of_term" | |
| 99 | where [code del]: "yxml_string_of_xml_tree _ _ = YXML" | |
| 100 | ||
| 101 | lemma yxml_string_of_xml_tree_code [code]: | |
| 102 | "yxml_string_of_xml_tree (xml.Elem name atts ts) rest = | |
| 103 | yot_append xml.XY ( | |
| 104 | yot_append (yot_literal name) ( | |
| 64578 | 105 | foldr (\<lambda>(a, x) rest. | 
| 58039 | 106 | yot_append xml.Y ( | 
| 107 | yot_append (yot_literal a) ( | |
| 108 | yot_append (yot_literal (STR ''='')) ( | |
| 109 | yot_append (yot_literal x) rest)))) atts ( | |
| 110 | foldr yxml_string_of_xml_tree ts ( | |
| 111 | yot_append xml.XYX rest))))" | |
| 112 | "yxml_string_of_xml_tree (xml.Text s) rest = yot_append (yot_literal s) rest" | |
| 113 | by(rule yot_anything)+ | |
| 114 | ||
| 115 | definition yxml_string_of_body :: "xml.body \<Rightarrow> yxml_of_term" | |
| 116 | where "yxml_string_of_body ts = foldr yxml_string_of_xml_tree ts yot_empty" | |
| 117 | ||
| 60500 | 118 | text \<open> | 
| 69593 | 119 | Encoding \<^typ>\<open>Code_Evaluation.term\<close> into XML trees as defined in | 
| 63680 | 120 | \<^file>\<open>~~/src/Pure/term_xml.ML\<close>. | 
| 60500 | 121 | \<close> | 
| 58039 | 122 | |
| 123 | definition xml_of_typ :: "Typerep.typerep \<Rightarrow> xml.body" | |
| 124 | where [code del]: "xml_of_typ _ = [XML_Tree]" | |
| 125 | ||
| 126 | definition xml_of_term :: "Code_Evaluation.term \<Rightarrow> xml.body" | |
| 127 | where [code del]: "xml_of_term _ = [XML_Tree]" | |
| 128 | ||
| 129 | lemma xml_of_typ_code [code]: | |
| 130 | "xml_of_typ (typerep.Typerep t args) = [xml.tagged (STR ''0'') (Some t) (xml.list xml_of_typ args)]" | |
| 131 | by(simp add: xml_of_typ_def xml_tree_anything) | |
| 132 | ||
| 133 | lemma xml_of_term_code [code]: | |
| 134 | "xml_of_term (Code_Evaluation.Const x ty) = [xml.tagged (STR ''0'') (Some x) (xml_of_typ ty)]" | |
| 135 | "xml_of_term (Code_Evaluation.App t1 t2) = [xml.tagged (STR ''5'') None [xml.node (xml_of_term t1), xml.node (xml_of_term t2)]]" | |
| 136 | "xml_of_term (Code_Evaluation.Abs x ty t) = [xml.tagged (STR ''4'') (Some x) [xml.node (xml_of_typ ty), xml.node (xml_of_term t)]]" | |
| 69593 | 137 | \<comment> \<open>FIXME: \<^const>\<open>Code_Evaluation.Free\<close> is used only in \<^theory>\<open>HOL.Quickcheck_Narrowing\<close> to represent | 
| 138 | uninstantiated parameters in constructors. Here, we always translate them to \<^ML>\<open>Free\<close> variables.\<close> | |
| 58039 | 139 | "xml_of_term (Code_Evaluation.Free x ty) = [xml.tagged (STR ''1'') (Some x) (xml_of_typ ty)]" | 
| 140 | by(simp_all add: xml_of_term_def xml_tree_anything) | |
| 141 | ||
| 142 | definition yxml_string_of_term :: "Code_Evaluation.term \<Rightarrow> yxml_of_term" | |
| 143 | where "yxml_string_of_term = yxml_string_of_body \<circ> xml_of_term" | |
| 144 | ||
| 60500 | 145 | subsection \<open>Test engine and drivers\<close> | 
| 58039 | 146 | |
| 69605 | 147 | ML_file \<open>code_test.ML\<close> | 
| 58039 | 148 | |
| 149 | end |