22525
|
1 |
(* Title: HOL/Library/Pure_term.thy
|
|
2 |
ID: $Id$
|
|
3 |
Author: Florian Haftmann, TU Muenchen
|
|
4 |
*)
|
|
5 |
|
|
6 |
header {* Embedding (a subset of) the Pure term algebra in HOL *}
|
|
7 |
|
|
8 |
theory Pure_term
|
|
9 |
imports MLString
|
|
10 |
begin
|
|
11 |
|
|
12 |
subsection {* Definitions *}
|
|
13 |
|
|
14 |
types vname = ml_string;
|
|
15 |
types "class" = ml_string;
|
|
16 |
types sort = "class list"
|
|
17 |
|
|
18 |
datatype "typ" =
|
|
19 |
Type ml_string "typ list" (infix "{\<struct>}" 120)
|
|
20 |
| TFix vname sort (infix "\<Colon>\<epsilon>" 117)
|
|
21 |
|
|
22 |
abbreviation
|
|
23 |
Fun :: "typ \<Rightarrow> typ \<Rightarrow> typ" (infixr "\<rightarrow>" 115) where
|
|
24 |
"ty1 \<rightarrow> ty2 \<equiv> Type (STR ''fun'') [ty1, ty2]"
|
|
25 |
abbreviation
|
|
26 |
Funs :: "typ list \<Rightarrow> typ \<Rightarrow> typ" (infixr "{\<rightarrow>}" 115) where
|
|
27 |
"tys {\<rightarrow>} ty \<equiv> foldr (op \<rightarrow>) tys ty"
|
|
28 |
|
|
29 |
datatype "term" =
|
|
30 |
Const ml_string "typ" (infix "\<Colon>\<subseteq>" 112)
|
|
31 |
| Fix vname "typ" (infix ":\<epsilon>" 112)
|
|
32 |
| App "term" "term" (infixl "\<bullet>" 110)
|
|
33 |
| Abs "vname \<times> typ" "term" (infixr "\<mapsto>" 111)
|
|
34 |
| Bnd nat
|
|
35 |
|
|
36 |
abbreviation
|
|
37 |
Apps :: "term \<Rightarrow> term list \<Rightarrow> term" (infixl "{\<bullet>}" 110) where
|
|
38 |
"t {\<bullet>} ts \<equiv> foldl (op \<bullet>) t ts"
|
|
39 |
abbreviation
|
|
40 |
Abss :: "(vname \<times> typ) list \<Rightarrow> term \<Rightarrow> term" (infixr "{\<mapsto>}" 111) where
|
|
41 |
"vs {\<mapsto>} t \<equiv> foldr (op \<mapsto>) vs t"
|
|
42 |
|
|
43 |
|
|
44 |
subsection {* ML interface *}
|
|
45 |
|
|
46 |
ML {*
|
|
47 |
structure Pure_term =
|
|
48 |
struct
|
|
49 |
|
|
50 |
val mk_sort = HOLogic.mk_list @{typ class} o map MLString.mk;
|
|
51 |
|
|
52 |
fun mk_typ f (Type (tyco, tys)) =
|
|
53 |
@{term Type} $ MLString.mk tyco
|
|
54 |
$ HOLogic.mk_list @{typ typ} (map (mk_typ f) tys)
|
|
55 |
| mk_typ f (TFree v) =
|
|
56 |
f v;
|
|
57 |
|
|
58 |
fun mk_term f g (Const (c, ty)) =
|
|
59 |
@{term Const} $ MLString.mk c $ g ty
|
|
60 |
| mk_term f g (t1 $ t2) =
|
|
61 |
@{term App} $ mk_term f g t1 $ mk_term f g t2
|
|
62 |
| mk_term f g (Free v) = f v;
|
|
63 |
|
|
64 |
end;
|
|
65 |
*}
|
|
66 |
|
|
67 |
|
|
68 |
subsection {* Code generator setup *}
|
|
69 |
|
22553
|
70 |
lemma [code func]:
|
|
71 |
"tyco1 {\<struct>} tys1 = tyco2 {\<struct>} tys2 \<longleftrightarrow> tyco1 = tyco2
|
|
72 |
\<and> list_all2 (op =) tys1 tys2"
|
|
73 |
by (auto simp add: list_all2_eq [symmetric])
|
|
74 |
|
22525
|
75 |
definition
|
|
76 |
Bound :: "int \<Rightarrow> term"
|
|
77 |
where
|
|
78 |
"Bound k = Bnd (nat k)"
|
|
79 |
|
|
80 |
lemma Bnd_Bound [code inline, code func]:
|
|
81 |
"Bnd n = Bound (int n)"
|
|
82 |
unfolding Bound_def by auto
|
|
83 |
|
|
84 |
definition
|
|
85 |
Absp :: "vname \<Rightarrow> typ \<Rightarrow> term \<Rightarrow> term"
|
|
86 |
where
|
|
87 |
"Absp v ty t = (v, ty) \<mapsto> t"
|
|
88 |
|
|
89 |
lemma Abs_Absp [code inline, code func]:
|
|
90 |
"(op \<mapsto>) (v, ty) = Absp v ty"
|
|
91 |
by rule (auto simp add: Absp_def)
|
|
92 |
|
|
93 |
definition
|
|
94 |
"term_case' f g h k l = term_case f g h (\<lambda>(v, ty). k v ty) (\<lambda>n. l (int n))"
|
|
95 |
|
|
96 |
lemma term_case' [code inline, code func]:
|
|
97 |
"term_case = (\<lambda>f g h k l. term_case' f g h (\<lambda>v ty. k (v, ty)) (\<lambda>v. l (nat v)))"
|
|
98 |
unfolding term_case'_def by auto
|
|
99 |
|
|
100 |
code_datatype Const App Fix Absp Bound
|
|
101 |
lemmas [code func] = Bnd_Bound Abs_Absp
|
|
102 |
|
|
103 |
code_type "typ" and "term"
|
|
104 |
(SML "Term.typ" and "Term.term")
|
|
105 |
|
|
106 |
code_const Type and TFix
|
|
107 |
(SML "Term.Type/ (_, _)" and "Term.TFree/ (_, _)")
|
|
108 |
|
|
109 |
code_const Const and App and Fix
|
|
110 |
and Absp and Bound
|
|
111 |
(SML "Term.Const/ (_, _)" and "Term.$/ (_, _)" and "Term.Free/ (_, _)"
|
|
112 |
and "Term.Abs/ (_, _, _)" and "Term.Bound/ (IntInf.toInt/ _)")
|
|
113 |
|
|
114 |
code_const term_rec and term_case and "size \<Colon> term \<Rightarrow> nat"
|
22553
|
115 |
and "op = \<Colon> term \<Rightarrow> term \<Rightarrow> bool"
|
22525
|
116 |
(SML "!(_; _; _; _; _; raise Fail \"term'_rec\")"
|
|
117 |
and "!(_; _; _; _; _; raise Fail \"term'_case\")"
|
22553
|
118 |
and "!(_; raise Fail \"size'_term\")"
|
|
119 |
and "!(_; _; raise Fail \"eq'_term\")")
|
|
120 |
(OCaml "!(_; _; _; _; _; failwith \"term'_rec\")"
|
|
121 |
and "!(_; _; _; _; _; failwith \"term'_case\")"
|
|
122 |
and "!(_; failwith \"size'_term\")"
|
|
123 |
and "!(_; _; failwith \"eq'_term\")")
|
|
124 |
(Haskell "error/ \"term'_rec\""
|
|
125 |
and "error/ \"term'_case\""
|
|
126 |
and "error/ \"size'_term\""
|
|
127 |
and "error/ \"eq'_term\"")
|
22525
|
128 |
|
|
129 |
code_reserved SML Term
|
|
130 |
|
|
131 |
end |