Skip to content

Commit 4d56eb1

Browse files
committed
Completed first version of polymorphic chantype command. Sort constraints don't work yet.
1 parent 5b054e7 commit 4d56eb1

File tree

3 files changed

+27
-67
lines changed

3 files changed

+27
-67
lines changed

Channel_Type.ML

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
structure Channel_Type =
22
struct
3+
4+
fun
5+
add_typerep_tfrees (Type (n, ts)) = Type (n, map add_typerep_tfrees ts) |
6+
add_typerep_tfrees (TFree (n, sorts)) = TFree (n, sorts @ @{sort typerep}) |
7+
add_typerep_tfrees (TVar (n, sorts)) = TVar (n, sorts @ @{sort typerep})
8+
39
fun prove_prism_goal thy =
410
let
511
open Simplifier; open Global_Theory; open Lens_Lib
@@ -76,7 +82,7 @@ fun chantyperep_def name raw_chans ct vmap ctx =
7682
open Syntax; open HOLogic; open Global_Theory; open Proof_Context
7783
val ty = read_type_name {proper = true, strict = false} ctx name;
7884
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)
85+
val chans = map (fn (n, t) => (n, mk_typerep (repl_tvars (read_typ ctx t)))) raw_chans
8086
val lhs = ct $ Free ("T", Term.itselfT ty);
8187
val rhs = mk_chantyperep chans ctx;
8288
val eq = check_term ctx (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs)));
@@ -105,12 +111,13 @@ fun chantyperep_instance raw_vars vars sorts name raw_chans thy =
105111
Class.prove_instantiation_exit (fn _ => NO_CONTEXT_TACTIC ctx5 (Method_Closure.apply_method ctx5 @{method chantyperep_inst} [] [] [] ctx5 [])) ctx5
106112
end;
107113

108-
fun make_chantype tvars vars sorts name chans thy =
114+
fun make_chantype tvars sorts name chans thy =
109115
let
110116
open BNF_FP_Def_Sugar; open BNF_FP_Rec_Sugar_Util; open BNF_LFP; open Ctr_Sugar
111117
open Prism_Lib; open Lens_Lib; open Local_Theory; open Specification; open Syntax; open HOLogic; open Global_Theory
112118
val ctx = Named_Target.theory_init thy;
113-
val ctrs = map (fn (n, t) => (((Binding.empty, Binding.name (n ^ ctor_suffix)), [(Binding.empty, t)]), Mixfix.NoSyn)) chans
119+
val ctrs = map (fn (n, t) => (((Binding.empty, Binding.name (n ^ ctor_suffix)), [(Binding.empty, add_typerep_tfrees (read_typ ctx t))]), Mixfix.NoSyn)) chans
120+
114121
val pnames = map fst chans
115122
val thypfx =
116123
case (Named_Target.locale_of ctx) of
@@ -119,12 +126,12 @@ fun make_chantype tvars vars sorts name chans thy =
119126
val prefix = thypfx ^ name ^ "."
120127
val attrs = @{attributes [simp, code_unfold]}
121128
val dummy_disc = absdummy dummyT @{term True}
122-
val ctx1 = co_datatype_cmd Least_FP construct_lfp
123-
((K Plugin_Name.default_filter, true),
124-
[(((((tvars, Binding.name name), Mixfix.NoSyn), ctrs), (Binding.empty, Binding.empty, Binding.empty)),[])]) ctx
129+
val cdtvars = map (fn (v, s) => (NONE, (TFree (v, []), s))) (ListPair.zip (tvars, sorts))
130+
val ctx1 = co_datatypes Least_FP construct_lfp
131+
((Plugin_Name.default_filter, true), [(((((cdtvars, Binding.name name), Mixfix.NoSyn), ctrs), (Binding.empty, Binding.empty, Binding.empty)),[])]) ctx
125132
val typ = Proof_Context.read_type_name {proper = true, strict = false} ctx1 name
126133
val tyco = fst (dest_Type typ);
127-
val typ' = Type (tyco, map TFree (ListPair.zip (vars, sorts)))
134+
val typ' = Type (tyco, map TFree (ListPair.zip (tvars, sorts)))
128135
val ptyp = prismT dummyT typ'
129136
in
130137
(
@@ -137,7 +144,7 @@ fun make_chantype tvars vars sorts name chans thy =
137144
(is_prefix ^ nth pnames 1 ^ ctor_suffix)
138145
(const @{const_name comp} $ @{term Not} $ const (prefix ^ is_prefix ^ nth pnames 0 ^ ctor_suffix))) false
139146
else I)
140-
(* The datatype also does not produce a discrimator when the length is 1 *)
147+
(* The datatype also does not produce a discriminator when the length is 1 *)
141148
#> (if (length pnames = 1)
142149
then abbreviation
143150
Syntax.mode_default (SOME (Binding.qualify false name (Binding.name (is_prefix ^ nth pnames 0 ^ ctor_suffix)), NONE, NoSyn)) []
@@ -163,11 +170,6 @@ fun prism_has_chanrep n ctx =
163170
Trueprop $ (const @{const_name has_chanrep} $ c)
164171
end
165172

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-
171173
fun prism_has_chanrep_proof ctx (n, t) =
172174
let open Simplifier; open Prism_Lib; open Syntax; open HOLogic
173175
val d = read_term ctx ("is_" ^ n ^ "_C")
@@ -202,18 +204,18 @@ fun prism_chanrep_proofs (name, chans) thy =
202204

203205

204206

205-
fun compile_chantype ((tvars, name), chans) thy =
207+
fun compile_chantype ((raw_tvars, name), raw_chans) thy =
206208
let
207-
open Syntax
209+
open Syntax;
208210
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+
val tvars = map (fn n => "'" ^ Char.toString (Char.chr n)) (97 upto (96 + length raw_tvars));
212+
val sorts = map (fn t => case snd (snd t) of SOME s => read_sort ctx s @ @{sort typerep} | NONE => @{sort typerep}) raw_tvars
211213
in
212-
(make_chantype tvars vars sorts name chans #>
214+
(make_chantype (map (fst o snd) raw_tvars) sorts name raw_chans #>
213215
(* Generate chantyperep instance *)
214-
chantyperep_instance (map (fst o snd) tvars) vars sorts name chans #>
216+
chantyperep_instance (map (fst o snd) raw_tvars) tvars sorts name raw_chans #>
215217
(* Generate representations for each prism (channel) *)
216-
prism_chanrep_proofs (name, chans)) thy
218+
prism_chanrep_proofs (name, raw_chans)) thy
217219
end
218220

219221
end;

Channel_Type.thy

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -220,51 +220,4 @@ subsection \<open> Channel Type Command \<close>
220220

221221
ML_file "Channel_Type.ML"
222222

223-
(*
224-
datatype 'a ty =
225-
x "'a"
226-
227-
ML \<open> Syntax.read_typ @{context} "'a ty"\<close>
228-
229-
ML \<open> Syntax.check_typ @{context} (Proof_Context.read_type_name {proper = true, strict = false} @{context} "ty") \<close>
230-
*)
231-
232-
declare [[show_sorts]]
233-
234-
ML \<open>
235-
open BNF_FP_Def_Sugar;
236-
open BNF_FP_Def_Sugar; open BNF_FP_Rec_Sugar_Util; open BNF_LFP; open Ctr_Sugar;
237-
238-
\<close>
239-
240-
ML \<open>
241-
val ctrs = [(((Binding.empty, Binding.name "MyCtr"), [(Binding.empty, @{typ int})]), NoSyn)];
242-
243-
(fn ctrs =>
244-
co_datatypes Least_FP construct_lfp
245-
((Plugin_Name.default_filter, true), [((((([], Binding.name "myt"), Mixfix.NoSyn), ctrs), (Binding.empty, Binding.empty, Binding.empty)),[])]))
246-
ctrs @{context}
247-
248-
; \<close>
249-
chantype 'a::typerep ty =
250-
x :: "'a list"
251-
y :: "nat"
252-
z :: bool
253-
254-
term x
255-
256-
257-
term x
258-
259-
term "CHANTYPEREP('v::typerep ty)"
260-
261-
lemma "has_chanrep x"
262-
apply (simp add: has_chanrep_def chantyperep_ty_def)
263-
apply (prism_has_chanrep "chanrep_of x")
264-
265-
declare [[show_sorts]]
266-
267-
term x
268-
269-
270223
end

Channel_Type_Example.thy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ chantype chbig =
4848
a8 :: unit
4949
a9 :: nat
5050

51+
chantype ('a, 'b) ty =
52+
p1 :: "'a list"
53+
p2 :: "nat"
54+
p3 :: bool
55+
5156
end

0 commit comments

Comments
 (0)