src/HOL/ex/CodeEmbed.thy
author haftmann
Mon, 21 Aug 2006 11:02:39 +0200
changeset 20400 0ad2f3bbd4f0
child 20453 855f07fabd76
permissions -rw-r--r--
added some codegen examples/applications
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20400
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
     1
(*  ID:         $Id$
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
     3
*)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
     4
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
     5
header {* Embedding (a subset of) the Pure term algebra in HOL *}
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
     6
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
     7
theory CodeEmbed
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
     8
imports Main MLString
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
     9
begin
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    10
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    11
section {* Embedding (a subset of) the Pure term algebra in HOL *}
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    12
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    13
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    14
subsection {* Definitions *}
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    15
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    16
types vname = ml_string;
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    17
types "class" = ml_string;
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    18
types sort = "class list"
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    19
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    20
datatype "typ" =
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    21
    Type ml_string "typ list" (infix "{\<struct>}" 120)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    22
  | TFix vname sort (infix "\<Colon>\<epsilon>" 117)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    23
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    24
abbreviation
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    25
  Fun :: "typ \<Rightarrow> typ \<Rightarrow> typ" (infixr "\<rightarrow>" 115)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    26
  "ty1 \<rightarrow> ty2 \<equiv> Type (STR ''fun'') [ty1, ty2]"
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    27
  Funs :: "typ list \<Rightarrow> typ \<Rightarrow> typ" (infixr "{\<rightarrow>}" 115)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    28
  "tys {\<rightarrow>} ty \<equiv> foldr (op \<rightarrow>) tys ty"
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    29
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    30
datatype "term" =
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    31
    Const ml_string "typ" (infix "\<Colon>\<subseteq>" 112)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    32
  | Fix   vname "typ" (infix ":\<epsilon>" 112)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    33
  | App   "term" "term" (infixl "\<bullet>" 110)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    34
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    35
abbreviation
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    36
  Apps :: "term \<Rightarrow> term list \<Rightarrow> term"  (infixl "{\<bullet>}" 110)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    37
  "t {\<bullet>} ts \<equiv> foldl (op \<bullet>) t ts"
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    38
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    39
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    40
subsection {* ML interface *}
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    41
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    42
ML {*
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    43
structure Embed =
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    44
struct
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    45
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    46
local
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    47
  val thy = the_context ();
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    48
  val const_Type = Sign.intern_const thy "Type";
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    49
  val const_TFix = Sign.intern_const thy "TFix";
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    50
  val const_Const = Sign.intern_const thy "Const";
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    51
  val const_App = Sign.intern_const thy "App";
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    52
  val const_Fix = Sign.intern_const thy "Fix";
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    53
in
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    54
  val typ_vname = Type (Sign.intern_type thy "vname", []);
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    55
  val typ_class = Type (Sign.intern_type thy "class", []);
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    56
  val typ_sort = Type (Sign.intern_type thy "sort", []);
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    57
  val typ_typ = Type (Sign.intern_type thy "typ", []);
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    58
  val typ_term = Type (Sign.intern_type thy "term", []);
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    59
  val term_sort = HOList.term_list typ_class MLString.term_ml_string;
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    60
  fun term_typ f (Type (tyco, tys)) =
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    61
        Const (const_Type, MLString.typ_ml_string --> HOList.typ_list typ_typ --> typ_typ)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    62
          $ MLString.term_ml_string tyco $ HOList.term_list typ_typ (term_typ f) tys
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    63
    | term_typ f (TFree v) =
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    64
        f v;
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    65
  fun term_term f g (Const (c, ty)) =
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    66
        Const (const_Const, MLString.typ_ml_string --> typ_typ --> typ_term)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    67
          $ MLString.term_ml_string c $ g ty
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    68
    | term_term f g (t1 $ t2) =
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    69
        Const (const_App, typ_term --> typ_term --> typ_term)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    70
          $ term_term f g t1 $ term_term f g t2
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    71
    | term_term f g (Free v) = f v;
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    72
end;
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    73
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    74
end;
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    75
*}
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    76
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    77
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    78
subsection {* Code serialization setup *}
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    79
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    80
code_typapp
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    81
  "typ"  ml (target_atom "Term.typ")
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    82
  "term" ml (target_atom "Term.term")
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    83
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    84
code_constapp
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    85
  Type  ml ("Term.Type (_, _)")
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    86
  TFix  ml ("Term.TFree (_, _)")
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    87
  Const ml ("Term.Const (_, _)")
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    88
  App   ml ("Term.$ (_, _)")
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    89
  Fix   ml ("Term.Free (_, _)")
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    90
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    91
end