src/HOL/Codegenerator_Test/Code_Test.thy
author blanchet
Thu, 11 Sep 2014 19:32:36 +0200
changeset 58310 91ea607a34d8
parent 58249 180f1b3508ed
child 58348 2d47c7d10b62
permissions -rw-r--r--
updated news
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58039
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
     1
(*  Title:      Code_Test.thy
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
     2
    Author:     Andreas Lochbihler, ETH Zurich
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
     3
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
     4
Test infrastructure for the code generator
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
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
     9
keywords "test_code" "eval_term" :: diag
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
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    12
subsection {* YXML encoding for @{typ Code_Evaluation.term} *}
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    13
58310
91ea607a34d8 updated news
blanchet
parents: 58249
diff changeset
    14
datatype 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
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    27
text {* Serialise @{typ yxml_of_term} to native string of target language *}
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
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    55
text {*
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    56
  Stripped-down implementations of Isabelle's XML tree with YXML encoding
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    57
  as defined in @{file "~~/src/Pure/PIDE/xml.ML"}, @{file "~~/src/Pure/PIDE/yxml.ML"}
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    58
  sufficient to encode @{typ "Code_Evaluation.term"} as in @{file "~~/src/Pure/term_xml.ML"}.
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    59
*}
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    60
58310
91ea607a34d8 updated news
blanchet
parents: 58249
diff changeset
    61
datatype xml_tree = XML_Tree
58039
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    62
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    63
lemma xml_tree_anything: "x = (y :: xml_tree)"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    64
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
    65
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    66
context begin
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    67
local_setup {* Local_Theory.map_naming (Name_Space.mandatory_path "xml") *}
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    68
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    69
type_synonym attributes = "(String.literal \<times> String.literal) list"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    70
type_synonym body = "xml_tree list"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    71
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    72
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
    73
where [code del]: "Elem _ _ _ = XML_Tree"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    74
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    75
definition Text :: "String.literal \<Rightarrow> xml_tree"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    76
where [code del]: "Text _ = XML_Tree"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    77
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    78
definition node :: "xml_tree list \<Rightarrow> xml_tree"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    79
where "node ts = Elem (STR '':'') [] ts"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    80
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    81
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
    82
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
    83
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    84
definition list where "list f xs = map (node \<circ> f) xs"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    85
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    86
definition X :: yxml_of_term where "X = yot_literal (STR [Char Nibble0 Nibble5])"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    87
definition Y :: yxml_of_term where "Y = yot_literal (STR [Char Nibble0 Nibble6])"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    88
definition XY :: yxml_of_term where "XY = yot_append X Y"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    89
definition XYX :: yxml_of_term where "XYX = yot_append XY X"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    90
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    91
end
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    92
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    93
code_datatype xml.Elem xml.Text
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    94
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    95
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
    96
where [code del]: "yxml_string_of_xml_tree _ _ = YXML"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    97
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    98
lemma yxml_string_of_xml_tree_code [code]:
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
    99
  "yxml_string_of_xml_tree (xml.Elem name atts ts) rest =
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   100
   yot_append xml.XY (
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   101
   yot_append (yot_literal name) (
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   102
   foldr (\<lambda>(a, x) rest. 
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   103
     yot_append xml.Y (
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   104
     yot_append (yot_literal a) (
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   105
     yot_append (yot_literal (STR ''='')) (
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   106
     yot_append (yot_literal x) rest)))) atts (
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   107
   foldr yxml_string_of_xml_tree ts (
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   108
   yot_append xml.XYX rest))))"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   109
  "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
   110
by(rule yot_anything)+
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   111
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   112
definition yxml_string_of_body :: "xml.body \<Rightarrow> yxml_of_term"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   113
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
   114
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   115
text {*
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   116
  Encoding @{typ Code_Evaluation.term} into XML trees
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   117
  as defined in @{file "~~/src/Pure/term_xml.ML"}
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   118
*}
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   119
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   120
definition xml_of_typ :: "Typerep.typerep \<Rightarrow> xml.body"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   121
where [code del]: "xml_of_typ _ = [XML_Tree]"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   122
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   123
definition xml_of_term :: "Code_Evaluation.term \<Rightarrow> xml.body"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   124
where [code del]: "xml_of_term _ = [XML_Tree]"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   125
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   126
lemma xml_of_typ_code [code]:
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   127
  "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
   128
by(simp add: xml_of_typ_def xml_tree_anything)
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   129
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   130
lemma xml_of_term_code [code]:
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   131
  "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
   132
  "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
   133
  "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)]]"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   134
  -- {*
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   135
    FIXME: @{const Code_Evaluation.Free} is used only in Quickcheck_Narrowing to represent
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   136
    uninstantiated parameters in constructors. Here, we always translate them to @{ML Free} variables.
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   137
  *}
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   138
  "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
   139
by(simp_all add: xml_of_term_def xml_tree_anything)
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   140
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   141
definition yxml_string_of_term :: "Code_Evaluation.term \<Rightarrow> yxml_of_term"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   142
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
   143
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   144
subsection {* Test engine and drivers *}
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   145
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   146
ML_file "code_test.ML"
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   147
469a375212c1 add testing framework for generated code
Andreas Lochbihler
parents:
diff changeset
   148
end