src/HOL/Library/Code_Test.thy
changeset 69593 3dda49e08b9d
parent 68484 59793df7f853
child 69605 a96320074298
equal deleted inserted replaced
69592:a80d8ec6c998 69593:3dda49e08b9d
     7 theory Code_Test
     7 theory Code_Test
     8 imports Main
     8 imports Main
     9 keywords "test_code" :: diag
     9 keywords "test_code" :: diag
    10 begin
    10 begin
    11 
    11 
    12 subsection \<open>YXML encoding for @{typ Code_Evaluation.term}\<close>
    12 subsection \<open>YXML encoding for \<^typ>\<open>Code_Evaluation.term\<close>\<close>
    13 
    13 
    14 datatype (plugins del: code size "quickcheck") yxml_of_term = YXML
    14 datatype (plugins del: code size "quickcheck") yxml_of_term = YXML
    15 
    15 
    16 lemma yot_anything: "x = (y :: yxml_of_term)"
    16 lemma yot_anything: "x = (y :: yxml_of_term)"
    17 by(cases x y rule: yxml_of_term.exhaust[case_product yxml_of_term.exhaust])(simp)
    17 by(cases x y rule: yxml_of_term.exhaust[case_product yxml_of_term.exhaust])(simp)
    22 definition yot_append :: "yxml_of_term \<Rightarrow> yxml_of_term \<Rightarrow> yxml_of_term"
    22 definition yot_append :: "yxml_of_term \<Rightarrow> yxml_of_term \<Rightarrow> yxml_of_term"
    23   where [code del]: "yot_append _ _ = YXML"
    23   where [code del]: "yot_append _ _ = YXML"
    24 definition yot_concat :: "yxml_of_term list \<Rightarrow> yxml_of_term"
    24 definition yot_concat :: "yxml_of_term list \<Rightarrow> yxml_of_term"
    25   where [code del]: "yot_concat _ = YXML"
    25   where [code del]: "yot_concat _ = YXML"
    26 
    26 
    27 text \<open>Serialise @{typ yxml_of_term} to native string of target language\<close>
    27 text \<open>Serialise \<^typ>\<open>yxml_of_term\<close> to native string of target language\<close>
    28 
    28 
    29 code_printing type_constructor yxml_of_term
    29 code_printing type_constructor yxml_of_term
    30   \<rightharpoonup>  (SML) "string"
    30   \<rightharpoonup>  (SML) "string"
    31   and (OCaml) "string"
    31   and (OCaml) "string"
    32   and (Haskell) "String"
    32   and (Haskell) "String"
    53   and (Scala) "_.mkString(\"\")"
    53   and (Scala) "_.mkString(\"\")"
    54 
    54 
    55 text \<open>
    55 text \<open>
    56   Stripped-down implementations of Isabelle's XML tree with YXML encoding as
    56   Stripped-down implementations of Isabelle's XML tree with YXML encoding as
    57   defined in \<^file>\<open>~~/src/Pure/PIDE/xml.ML\<close>, \<^file>\<open>~~/src/Pure/PIDE/yxml.ML\<close>
    57   defined in \<^file>\<open>~~/src/Pure/PIDE/xml.ML\<close>, \<^file>\<open>~~/src/Pure/PIDE/yxml.ML\<close>
    58   sufficient to encode @{typ "Code_Evaluation.term"} as in
    58   sufficient to encode \<^typ>\<open>Code_Evaluation.term\<close> as in
    59   \<^file>\<open>~~/src/Pure/term_xml.ML\<close>.
    59   \<^file>\<open>~~/src/Pure/term_xml.ML\<close>.
    60 \<close>
    60 \<close>
    61 
    61 
    62 datatype (plugins del: code size "quickcheck") xml_tree = XML_Tree
    62 datatype (plugins del: code size "quickcheck") xml_tree = XML_Tree
    63 
    63 
   112 
   112 
   113 definition yxml_string_of_body :: "xml.body \<Rightarrow> yxml_of_term"
   113 definition yxml_string_of_body :: "xml.body \<Rightarrow> yxml_of_term"
   114 where "yxml_string_of_body ts = foldr yxml_string_of_xml_tree ts yot_empty"
   114 where "yxml_string_of_body ts = foldr yxml_string_of_xml_tree ts yot_empty"
   115 
   115 
   116 text \<open>
   116 text \<open>
   117   Encoding @{typ Code_Evaluation.term} into XML trees as defined in
   117   Encoding \<^typ>\<open>Code_Evaluation.term\<close> into XML trees as defined in
   118   \<^file>\<open>~~/src/Pure/term_xml.ML\<close>.
   118   \<^file>\<open>~~/src/Pure/term_xml.ML\<close>.
   119 \<close>
   119 \<close>
   120 
   120 
   121 definition xml_of_typ :: "Typerep.typerep \<Rightarrow> xml.body"
   121 definition xml_of_typ :: "Typerep.typerep \<Rightarrow> xml.body"
   122 where [code del]: "xml_of_typ _ = [XML_Tree]"
   122 where [code del]: "xml_of_typ _ = [XML_Tree]"
   130 
   130 
   131 lemma xml_of_term_code [code]:
   131 lemma xml_of_term_code [code]:
   132   "xml_of_term (Code_Evaluation.Const x ty) = [xml.tagged (STR ''0'') (Some x) (xml_of_typ ty)]"
   132   "xml_of_term (Code_Evaluation.Const x ty) = [xml.tagged (STR ''0'') (Some x) (xml_of_typ ty)]"
   133   "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)]]"
   133   "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)]]"
   134   "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)]]"
   134   "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)]]"
   135   \<comment> \<open>FIXME: @{const Code_Evaluation.Free} is used only in @{theory "HOL.Quickcheck_Narrowing"} to represent
   135   \<comment> \<open>FIXME: \<^const>\<open>Code_Evaluation.Free\<close> is used only in \<^theory>\<open>HOL.Quickcheck_Narrowing\<close> to represent
   136     uninstantiated parameters in constructors. Here, we always translate them to @{ML Free} variables.\<close>
   136     uninstantiated parameters in constructors. Here, we always translate them to \<^ML>\<open>Free\<close> variables.\<close>
   137   "xml_of_term (Code_Evaluation.Free x ty)  = [xml.tagged (STR ''1'') (Some x) (xml_of_typ ty)]"
   137   "xml_of_term (Code_Evaluation.Free x ty)  = [xml.tagged (STR ''1'') (Some x) (xml_of_typ ty)]"
   138 by(simp_all add: xml_of_term_def xml_tree_anything)
   138 by(simp_all add: xml_of_term_def xml_tree_anything)
   139 
   139 
   140 definition yxml_string_of_term :: "Code_Evaluation.term \<Rightarrow> yxml_of_term"
   140 definition yxml_string_of_term :: "Code_Evaluation.term \<Rightarrow> yxml_of_term"
   141 where "yxml_string_of_term = yxml_string_of_body \<circ> xml_of_term"
   141 where "yxml_string_of_term = yxml_string_of_body \<circ> xml_of_term"