src/HOL/ex/CodeEmbed.thy
author haftmann
Wed, 27 Dec 2006 19:10:03 +0100
changeset 21912 ff45788e7bf9
parent 21757 093ca3efb132
permissions -rw-r--r--
dropped section header
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
subsection {* Definitions *}
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    12
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    13
types vname = ml_string;
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    14
types "class" = ml_string;
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    15
types sort = "class list"
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    16
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    17
datatype "typ" =
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    18
    Type ml_string "typ list" (infix "{\<struct>}" 120)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    19
  | TFix vname sort (infix "\<Colon>\<epsilon>" 117)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    20
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    21
abbreviation
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21113
diff changeset
    22
  Fun :: "typ \<Rightarrow> typ \<Rightarrow> typ" (infixr "\<rightarrow>" 115) where
20400
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    23
  "ty1 \<rightarrow> ty2 \<equiv> Type (STR ''fun'') [ty1, ty2]"
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21113
diff changeset
    24
abbreviation
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21113
diff changeset
    25
  Funs :: "typ list \<Rightarrow> typ \<Rightarrow> typ" (infixr "{\<rightarrow>}" 115) where
20400
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    26
  "tys {\<rightarrow>} ty \<equiv> foldr (op \<rightarrow>) tys ty"
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    27
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    28
datatype "term" =
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    29
    Const ml_string "typ" (infix "\<Colon>\<subseteq>" 112)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    30
  | Fix   vname "typ" (infix ":\<epsilon>" 112)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    31
  | App   "term" "term" (infixl "\<bullet>" 110)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    32
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    33
abbreviation
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21113
diff changeset
    34
  Apps :: "term \<Rightarrow> term list \<Rightarrow> term"  (infixl "{\<bullet>}" 110) where
20400
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    35
  "t {\<bullet>} ts \<equiv> foldl (op \<bullet>) t ts"
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    36
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    37
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    38
subsection {* ML interface *}
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    39
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    40
ML {*
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    41
structure Embed =
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    42
struct
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    43
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    44
local
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    45
  val thy = the_context ();
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    46
  val const_Type = Sign.intern_const thy "Type";
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    47
  val const_TFix = Sign.intern_const thy "TFix";
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    48
  val const_Const = Sign.intern_const thy "Const";
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    49
  val const_App = Sign.intern_const thy "App";
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    50
  val const_Fix = Sign.intern_const thy "Fix";
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    51
in
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    52
  val typ_vname = Type (Sign.intern_type thy "vname", []);
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    53
  val typ_class = Type (Sign.intern_type thy "class", []);
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    54
  val typ_sort = Type (Sign.intern_type thy "sort", []);
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    55
  val typ_typ = Type (Sign.intern_type thy "typ", []);
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    56
  val typ_term = Type (Sign.intern_type thy "term", []);
21757
093ca3efb132 HOLogic cleanup;
wenzelm
parents: 21455
diff changeset
    57
  val term_sort = HOLogic.mk_list typ_class o map MLString.term_ml_string;
20400
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    58
  fun term_typ f (Type (tyco, tys)) =
21455
b6be1d1b66c5 incorporated structure HOList into HOLogic
haftmann
parents: 21404
diff changeset
    59
        Const (const_Type, MLString.typ_ml_string --> HOLogic.listT typ_typ --> typ_typ)
21757
093ca3efb132 HOLogic cleanup;
wenzelm
parents: 21455
diff changeset
    60
          $ MLString.term_ml_string tyco $ HOLogic.mk_list typ_typ (map (term_typ f) tys)
20400
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    61
    | term_typ f (TFree v) =
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    62
        f v;
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    63
  fun term_term f g (Const (c, ty)) =
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    64
        Const (const_Const, MLString.typ_ml_string --> typ_typ --> typ_term)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    65
          $ MLString.term_ml_string c $ g ty
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    66
    | term_term f g (t1 $ t2) =
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    67
        Const (const_App, typ_term --> typ_term --> typ_term)
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    68
          $ term_term f g t1 $ term_term f g t2
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    69
    | term_term f g (Free v) = f v;
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    70
end;
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    71
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
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    75
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    76
subsection {* Code serialization setup *}
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    77
20453
855f07fabd76 final syntax for some Isar code generator keywords
haftmann
parents: 20400
diff changeset
    78
code_type "typ" and "term"
21113
5b76e541cc0a adapted to new serializer syntax
haftmann
parents: 21079
diff changeset
    79
  (SML "Term.typ" and "Term.term")
20400
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    80
20453
855f07fabd76 final syntax for some Isar code generator keywords
haftmann
parents: 20400
diff changeset
    81
code_const Type and TFix
855f07fabd76 final syntax for some Isar code generator keywords
haftmann
parents: 20400
diff changeset
    82
  and Const and App and Fix
855f07fabd76 final syntax for some Isar code generator keywords
haftmann
parents: 20400
diff changeset
    83
  (SML "Term.Type (_, _)" and "Term.TFree (_, _)"
855f07fabd76 final syntax for some Isar code generator keywords
haftmann
parents: 20400
diff changeset
    84
    and "Term.Const (_, _)" and "Term.$ (_, _)" and "Term.Free (_, _)")
20400
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    85
21079
747d716e98d0 added reserved words for Haskell
haftmann
parents: 20453
diff changeset
    86
code_reserved SML Term
747d716e98d0 added reserved words for Haskell
haftmann
parents: 20453
diff changeset
    87
20400
0ad2f3bbd4f0 added some codegen examples/applications
haftmann
parents:
diff changeset
    88
end