@@ -44,15 +44,15 @@ fun mk_def ty x v = Const ("Pure.eq", ty --> ty --> Term.propT) $ Free (x, ty) $
44
44
val is_prefix = " is_" ;
45
45
val un_prefix = " un_" ;
46
46
47
- fun def qual (x, tm) ctx =
47
+ fun def typ qual (x, tm) ctx =
48
48
let open Specification; open Syntax
49
- val ((_, (_, thm)), d) = definition (SOME (Binding.qualify false qual (Binding.name x), NONE , NoSyn)) [] [] ((Binding.empty, @{attributes [lens_defs, chan_defs]}), mk_def dummyT x tm) ctx
49
+ val ((_, (_, thm)), d) = definition (SOME (Binding.qualify false qual (Binding.name x), SOME typ , NoSyn)) [] [] ((Binding.empty, @{attributes [lens_defs, chan_defs]}), mk_def typ x tm) ctx
50
50
in (thm, d)
51
51
end
52
52
53
- fun defs qual inds f ctx =
53
+ fun defs typ qual inds f ctx =
54
54
fold (fn i => fn (thms, ctx) =>
55
- let val (thm, ctx') = def qual (i, f i) ctx
55
+ let val (thm, ctx') = def typ qual (i, f i) ctx
56
56
in (thms @ [thm], ctx') end ) inds ([], ctx)
57
57
58
58
fun mk_chantyperep chans ctx =
@@ -71,25 +71,29 @@ fun mk_chantyperep chans ctx =
71
71
mk_list dummyT (map mk_chanrep chans)
72
72
end
73
73
74
- fun chantyperep_def name raw_chans ct ctx =
74
+ fun chantyperep_def name raw_chans ct vmap ctx =
75
75
let
76
- open Syntax; open HOLogic; open Global_Theory;
77
- val ty = read_typ ctx name;
78
- val chans = map (fn (n, t) => (n, mk_typerep (read_typ ctx t))) raw_chans
76
+ open Syntax; open HOLogic; open Global_Theory; open Proof_Context
77
+ val ty = read_type_name {proper = true , strict = false } ctx name;
78
+ fun repl_tvars ty = map_type_tfree (fn (n, s) => TFree (the (AList.lookup (op =) vmap n), s)) ty
79
+ val chans = @{print} (map (fn (n, t) => (n, mk_typerep (repl_tvars (read_typ ctx t)))) raw_chans)
79
80
val lhs = ct $ Free (" T" , Term.itselfT ty);
80
81
val rhs = mk_chantyperep chans ctx;
81
82
val eq = check_term ctx (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs)));
82
83
in snd (Specification.definition NONE [] [] ((Binding.empty, @{attributes [chantyperep_defs]}), eq) ctx)
83
84
end ;
84
85
85
- fun chantyperep_instance name raw_chans thy =
86
+ fun chantyperep_instance raw_vars vars sorts name raw_chans thy =
86
87
let
87
88
open Syntax; open HOLogic; open Global_Theory;
88
- val ty = read_typ (Named_Target.theory_init thy) name;
89
+ val vmap = ListPair.zip (raw_vars, vars)
90
+ val tvars = ListPair.zip (vars, sorts)
91
+ val ty = Proof_Context.read_type_name {proper = true , strict = false } (Named_Target.theory_init thy) name;
89
92
val tyco = fst (dest_Type ty);
90
- val ctx0 = Class.instantiation ([tyco], [] , \<^sort>\<open >chantyperep\<close>) thy;
93
+ val ctx0 = Class.instantiation ([tyco], tvars , \<^sort>\<open >chantyperep\<close>) thy;
91
94
val ctx1 = snd (Local_Theory.begin_nested ctx0)
92
- val ctx2 = chantyperep_def name raw_chans \<^Const>\<open >chantyperep ty\<close> ctx1
95
+ val ty' = Type (tyco, map (read_typ ctx1) vars)
96
+ val ctx2 = chantyperep_def name raw_chans \<^Const>\<open >chantyperep ty'\<close> vmap ctx1
93
97
val ctx3 = Local_Theory.end_nested ctx2;
94
98
val disc_thms = if length raw_chans = 1 then [] else get_thms thy (tyco ^ " .discI" )
95
99
val exhaust_disc_thms = if length raw_chans = 1 then [] else get_thms thy (tyco ^ " .exhaust_disc" )
@@ -101,7 +105,7 @@ fun chantyperep_instance name raw_chans thy =
101
105
Class.prove_instantiation_exit (fn _ => NO_CONTEXT_TACTIC ctx5 (Method_Closure.apply_method ctx5 @{method chantyperep_inst} [] [] [] ctx5 [])) ctx5
102
106
end ;
103
107
104
- fun make_chantype ( name, chans) thy =
108
+ fun make_chantype tvars vars sorts name chans thy =
105
109
let
106
110
open BNF_FP_Def_Sugar; open BNF_FP_Rec_Sugar_Util; open BNF_LFP; open Ctr_Sugar
107
111
open Prism_Lib; open Lens_Lib; open Local_Theory; open Specification; open Syntax; open HOLogic; open Global_Theory
@@ -117,8 +121,11 @@ fun make_chantype (name, chans) thy =
117
121
val dummy_disc = absdummy dummyT @{term True}
118
122
val ctx1 = co_datatype_cmd Least_FP construct_lfp
119
123
((K Plugin_Name.default_filter, true ),
120
- [((((([],Binding.name name), Mixfix.NoSyn), ctrs), (Binding.empty, Binding.empty, Binding.empty)),[])]) ctx
121
- val typ = read_typ ctx1 name
124
+ [(((((tvars, Binding.name name), Mixfix.NoSyn), ctrs), (Binding.empty, Binding.empty, Binding.empty)),[])]) ctx
125
+ val typ = Proof_Context.read_type_name {proper = true , strict = false } ctx1 name
126
+ val tyco = fst (dest_Type typ);
127
+ val typ' = Type (tyco, map TFree (ListPair.zip (vars, sorts)))
128
+ val ptyp = prismT dummyT typ'
122
129
in
123
130
(
124
131
(* The datatype package does not produce a discriminator for the second constructor when
@@ -138,7 +145,7 @@ fun make_chantype (name, chans) thy =
138
145
(is_prefix ^ nth pnames 0 ^ ctor_suffix)
139
146
(Abs (" x" , typ, @{term " True" }))) false
140
147
else I)
141
- #> defs name pnames (fn x => (const @{const_name ctor_prism}
148
+ #> defs ptyp name pnames (fn x => (const @{const_name ctor_prism}
142
149
$ const (prefix ^ x ^ ctor_suffix)
143
150
$ (if (length pnames = 1 ) then dummy_disc else const (prefix ^ is_prefix ^ x ^ ctor_suffix))
144
151
$ const (prefix ^ un_prefix ^ x ^ ctor_suffix)))
@@ -152,14 +159,19 @@ fun make_chantype (name, chans) thy =
152
159
153
160
fun prism_has_chanrep n ctx =
154
161
let open Syntax; open HOLogic
155
- val c = read_term ctx n in
156
- Trueprop $ (const @{const_name has_chanrep} $ c)
162
+ val c = parse_term ctx n in
163
+ Trueprop $ (const @{const_name has_chanrep} $ c)
157
164
end
158
165
166
+ fun
167
+ add_typerep_tfrees (Type (n, ts)) = Type (n, map add_typerep_tfrees ts) |
168
+ add_typerep_tfrees (TFree (n, sorts)) = TFree (n, sorts @ @{sort typerep}) |
169
+ add_typerep_tfrees (TVar (n, sorts)) = TVar (n, sorts @ @{sort typerep})
170
+
159
171
fun prism_has_chanrep_proof ctx (n, t) =
160
172
let open Simplifier; open Prism_Lib; open Syntax; open HOLogic
161
173
val d = read_term ctx (" is_" ^ n ^ " _C" )
162
- val ct = Syntax.check_term ctx ((const @{const_name Chanrep} $ mk_literal n $ mk_typerep (read_typ ctx t)) $ d)
174
+ val ct = Syntax.check_term ctx ((const @{const_name Chanrep} $ mk_literal n $ mk_typerep (add_typerep_tfrees ( read_typ ctx t) )) $ d)
163
175
in
164
176
Goal.prove ctx [] []
165
177
(Syntax.check_term ctx (prism_has_chanrep n ctx))
@@ -169,7 +181,7 @@ fun prism_has_chanrep_proof ctx (n, t) =
169
181
fun prism_chanrep n t ctx =
170
182
let open Syntax; open HOLogic
171
183
val c = read_term ctx n; val d = read_term ctx (" is_" ^ n ^ " _C" )
172
- val ct = ((const @{const_name Chanrep} $ mk_literal n $ mk_typerep t ) $ d) in
184
+ val ct = ((const @{const_name Chanrep} $ mk_literal n $ mk_typerep (add_typerep_tfrees t) ) $ d) in
173
185
Trueprop $ (eq_const dummyT $ (const @{const_name chanrep_of} $ c) $ ct)
174
186
end
175
187
@@ -190,18 +202,25 @@ fun prism_chanrep_proofs (name, chans) thy =
190
202
191
203
192
204
193
- fun compile_chantype (name, chans) =
194
- make_chantype (name, chans) #>
205
+ fun compile_chantype ((tvars, name), chans) thy =
206
+ let
207
+ open Syntax
208
+ val ctx = Named_Target.theory_init thy;
209
+ val vars = map (fn n => " '" ^ Char.toString (Char.chr n)) (97 upto (96 + length tvars));
210
+ val sorts = map (fn t => case snd (snd t) of SOME s => read_sort ctx s @ @{sort typerep} | NONE => @{sort typerep}) tvars
211
+ in
212
+ (make_chantype tvars vars sorts name chans #>
195
213
(* Generate chantyperep instance *)
196
- chantyperep_instance name chans #>
214
+ chantyperep_instance (map (fst o snd) tvars) vars sorts name chans #>
197
215
(* Generate representations for each prism (channel) *)
198
- prism_chanrep_proofs (name, chans)
216
+ prism_chanrep_proofs (name, chans)) thy
217
+ end
199
218
200
219
end ;
201
220
202
221
let open Parse; open Parse_Spec; open Scan in
203
222
Outer_Syntax.command @{command_keyword chantype} " define a channel datatype"
204
- ((name --
223
+ ((( BNF_Util.parse_type_args_named_constrained -- name) --
205
224
(@{keyword " =" } |-- repeat1 (name -- ($$$ " ::" |-- !!! typ))))
206
225
>> (fn x => Toplevel.theory (Channel_Type.compile_chantype x)))
207
226
end ;
0 commit comments