@@ -57,18 +57,50 @@ fun defs qual inds f ctx =
5757 in (thms @ [thm], ctx') end ) inds ([], ctx)
5858
5959fun mk_chantyperep chans ctx =
60- let open HOLogic; open Syntax
61- fun mk_chanrep (n, t) = check_term ctx (const @{const_name Chanrep} $ mk_literal n $ mk_literal t $ parse_term ctx (is_prefix ^ n ^ ctor_suffix))
60+ let open HOLogic; open Syntax; open Proof_Context
61+ fun mk_chanrep (n, t) =
62+ let val c =
63+ case read_const {proper = false , strict = false } ctx (is_prefix ^ n ^ ctor_suffix)
64+ of Free (c', _) => free c' | Const (c', _) => const c' | _ => raise Match;
65+ in
66+ (* check_term ctx *)
67+ (const @{const_name Chanrep}
68+ $ mk_literal n $ t
69+ $ c)
70+ end
6271 in
63- @{print} ( mk_list dummyT (map mk_chanrep chans) )
72+ mk_list dummyT (map mk_chanrep chans)
6473 end
6574
66- fun compile_chantype (name, chans) ctx =
75+ fun chantyperep_instance name raw_chans thy =
76+ let
77+ open Syntax; open HOLogic; open Global_Theory;
78+ val ty = Syntax.read_typ (Named_Target.theory_init thy) name;
79+ val tyco = fst (dest_Type ty);
80+ val disc_intro_thms = if length raw_chans = 1 then [] else get_thms thy (tyco ^ " .disc" );
81+ val disc_elim_thms = if length raw_chans = 1 then [] else get_thms thy (tyco ^ " .exhaust_disc" );
82+ val ctx0 = Class.instantiation ([tyco], [], \<^sort>\<open >chantyperep\<close>) thy;
83+ val chans = map (fn (n, t) => (n, mk_typerep (read_typ ctx0 t))) raw_chans
84+ val lhs = \<^Const>\<open >chantyperep ty\<close> $ Free (" T" , Term.itselfT ty);
85+ val rhs = mk_chantyperep chans ctx0;
86+ val ctx1 = snd (Local_Theory.begin_nested ctx0)
87+ val eq = check_term ctx1 (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs)));
88+ val (_, ctx2) = Specification.definition NONE [] [] ((Binding.empty, @{attributes [chantyperep_defs]}), eq) ctx1;
89+ val ctx3 = Local_Theory.end_nested ctx2;
90+ val ctx4 = fold (Context.proof_map o Named_Theorems.add_thm " Channel_Type.datatype_disc_intros" ) disc_intro_thms ctx3;
91+ val ctx5 = fold (Context.proof_map o Named_Theorems.add_thm " Channel_Type.datatype_disc_elims" ) disc_elim_thms ctx4;
92+ in
93+ Class.prove_instantiation_exit (fn _ => NO_CONTEXT_TACTIC ctx5 (Method_Closure.apply_method ctx5 @{method chantyperep_inst} [] [] [] ctx5 [])) ctx5
94+ end ;
95+
96+ fun compile_chantype (name, chans) thy =
6797 let
6898 open BNF_FP_Def_Sugar; open BNF_FP_Rec_Sugar_Util; open BNF_LFP; open Ctr_Sugar
69- open Prism_Lib; open Lens_Lib; open Local_Theory; open Specification; open Syntax
99+ open Prism_Lib; open Lens_Lib; open Local_Theory; open Specification; open Syntax; open HOLogic
100+ val ctx = Named_Target.theory_init thy;
70101 val ctrs = map (fn (n, t) => (((Binding.empty, Binding.name (n ^ ctor_suffix)), [(Binding.empty, t)]), Mixfix.NoSyn)) chans
71102 val pnames = map fst chans
103+ val tyreps = map (mk_typerep o read_typ ctx o snd) chans
72104 val thypfx =
73105 case (Named_Target.locale_of ctx) of
74106 SOME loc => loc ^ " ." |
@@ -107,7 +139,9 @@ fun compile_chantype (name, chans) ctx =
107139 (fold (fn x => fn thy => snd (note ((Binding.qualify false name (Binding.name (x ^ wb_prism_suffix)), attrs), [wb_prism_proof x thms thy]) thy)) pnames
108140 #> (snd o note ((Binding.qualify false name (Binding.name codepsN), attrs), map (codep_proof thms ctx) (pairings pnames)))
109141 ) ctx)
110- (* #> (fn ctx => snd ((def name ("ctrep", mk_chantyperep chans ctx1)) ctx)) *) )
142+ #> Local_Theory.exit_global
143+ (* Generate chantyperep instance *)
144+ #> chantyperep_instance name chans)
111145 ctx1
112146 end ;
113147
@@ -117,5 +151,5 @@ let open Parse; open Parse_Spec; open Scan in
117151 Outer_Syntax.command @{command_keyword chantype} " define a channel datatype"
118152 ((name --
119153 (@{keyword " =" } |-- repeat1 (name -- ($$$ " ::" |-- !!! typ))))
120- >> (fn x => Toplevel.local_theory NONE NONE (Channel_Type.compile_chantype x)))
154+ >> (fn x => Toplevel.theory (Channel_Type.compile_chantype x)))
121155 end ;
0 commit comments