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