author | haftmann |
Wed, 27 Dec 2006 19:10:03 +0100 | |
changeset 21912 | ff45788e7bf9 |
parent 21757 | 093ca3efb132 |
permissions | -rw-r--r-- |
20400 | 1 |
(* ID: $Id$ |
2 |
Author: Florian Haftmann, TU Muenchen |
|
3 |
*) |
|
4 |
||
5 |
header {* Embedding (a subset of) the Pure term algebra in HOL *} |
|
6 |
||
7 |
theory CodeEmbed |
|
8 |
imports Main MLString |
|
9 |
begin |
|
10 |
||
11 |
subsection {* Definitions *} |
|
12 |
||
13 |
types vname = ml_string; |
|
14 |
types "class" = ml_string; |
|
15 |
types sort = "class list" |
|
16 |
||
17 |
datatype "typ" = |
|
18 |
Type ml_string "typ list" (infix "{\<struct>}" 120) |
|
19 |
| TFix vname sort (infix "\<Colon>\<epsilon>" 117) |
|
20 |
||
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 | 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 | 26 |
"tys {\<rightarrow>} ty \<equiv> foldr (op \<rightarrow>) tys ty" |
27 |
||
28 |
datatype "term" = |
|
29 |
Const ml_string "typ" (infix "\<Colon>\<subseteq>" 112) |
|
30 |
| Fix vname "typ" (infix ":\<epsilon>" 112) |
|
31 |
| App "term" "term" (infixl "\<bullet>" 110) |
|
32 |
||
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 | 35 |
"t {\<bullet>} ts \<equiv> foldl (op \<bullet>) t ts" |
36 |
||
37 |
||
38 |
subsection {* ML interface *} |
|
39 |
||
40 |
ML {* |
|
41 |
structure Embed = |
|
42 |
struct |
|
43 |
||
44 |
local |
|
45 |
val thy = the_context (); |
|
46 |
val const_Type = Sign.intern_const thy "Type"; |
|
47 |
val const_TFix = Sign.intern_const thy "TFix"; |
|
48 |
val const_Const = Sign.intern_const thy "Const"; |
|
49 |
val const_App = Sign.intern_const thy "App"; |
|
50 |
val const_Fix = Sign.intern_const thy "Fix"; |
|
51 |
in |
|
52 |
val typ_vname = Type (Sign.intern_type thy "vname", []); |
|
53 |
val typ_class = Type (Sign.intern_type thy "class", []); |
|
54 |
val typ_sort = Type (Sign.intern_type thy "sort", []); |
|
55 |
val typ_typ = Type (Sign.intern_type thy "typ", []); |
|
56 |
val typ_term = Type (Sign.intern_type thy "term", []); |
|
21757 | 57 |
val term_sort = HOLogic.mk_list typ_class o map MLString.term_ml_string; |
20400 | 58 |
fun term_typ f (Type (tyco, tys)) = |
21455 | 59 |
Const (const_Type, MLString.typ_ml_string --> HOLogic.listT typ_typ --> typ_typ) |
21757 | 60 |
$ MLString.term_ml_string tyco $ HOLogic.mk_list typ_typ (map (term_typ f) tys) |
20400 | 61 |
| term_typ f (TFree v) = |
62 |
f v; |
|
63 |
fun term_term f g (Const (c, ty)) = |
|
64 |
Const (const_Const, MLString.typ_ml_string --> typ_typ --> typ_term) |
|
65 |
$ MLString.term_ml_string c $ g ty |
|
66 |
| term_term f g (t1 $ t2) = |
|
67 |
Const (const_App, typ_term --> typ_term --> typ_term) |
|
68 |
$ term_term f g t1 $ term_term f g t2 |
|
69 |
| term_term f g (Free v) = f v; |
|
70 |
end; |
|
71 |
||
72 |
end; |
|
73 |
*} |
|
74 |
||
75 |
||
76 |
subsection {* Code serialization setup *} |
|
77 |
||
20453
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20400
diff
changeset
|
78 |
code_type "typ" and "term" |
21113 | 79 |
(SML "Term.typ" and "Term.term") |
20400 | 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 | 85 |
|
21079 | 86 |
code_reserved SML Term |
87 |
||
20400 | 88 |
end |