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