QuotMain.thy
changeset 26 68d64432623e
parent 24 6885fa184e89
child 27 160f287ebb75
equal deleted inserted replaced
24:6885fa184e89 26:68d64432623e
   150     (HOLogic.mk_exists
   150     (HOLogic.mk_exists
   151        ("x", ty, HOLogic.mk_eq (c, (rel $ x))))
   151        ("x", ty, HOLogic.mk_eq (c, (rel $ x))))
   152 end
   152 end
   153 *}
   153 *}
   154 
   154 
   155 (*
   155 ML {*
   156 ML {*
   156 (* makes the new type definitions and proves non-emptyness*)
   157  typedef_term @{term R} @{typ "nat"} @{context}
       
   158   |> Syntax.string_of_term @{context}
       
   159   |> writeln
       
   160 *}*)
       
   161 
       
   162 ML {*
       
   163 val typedef_tac =
       
   164   EVERY1 [rewrite_goal_tac @{thms mem_def},
       
   165           rtac @{thm exI}, rtac @{thm exI}, rtac @{thm refl}]
       
   166 *}
       
   167 
       
   168 ML {*
       
   169 (* makes the new type definitions *)
       
   170 fun typedef_make (qty_name, rel, ty) lthy =
   157 fun typedef_make (qty_name, rel, ty) lthy =
       
   158 let  
       
   159   val typedef_tac =
       
   160      EVERY1 [rewrite_goal_tac @{thms mem_def},
       
   161              rtac @{thm exI}, 
       
   162              rtac @{thm exI}, 
       
   163              rtac @{thm refl}]
       
   164 in
   171   LocalTheory.theory_result
   165   LocalTheory.theory_result
   172   (Typedef.add_typedef false NONE
   166     (Typedef.add_typedef false NONE
   173      (qty_name, map fst (Term.add_tfreesT ty []), NoSyn)
   167        (qty_name, map fst (Term.add_tfreesT ty []), NoSyn)
   174        (typedef_term rel ty lthy)
   168          (typedef_term rel ty lthy)
   175          NONE typedef_tac) lthy
   169            NONE typedef_tac) lthy
   176 *}
   170 end
   177 
   171 *}
   178 text {* proves the QUOT_TYPE theorem for the new type *}
   172 
   179 ML {*
   173 ML {*
       
   174 (* proves the QUOT_TYPE theorem for the new type *)
   180 fun typedef_quot_type_tac equiv_thm (typedef_info: Typedef.info) =
   175 fun typedef_quot_type_tac equiv_thm (typedef_info: Typedef.info) =
   181 let
   176 let
   182   val rep_thm = #Rep typedef_info
   177   val rep_thm = #Rep typedef_info
   183   val rep_inv = #Rep_inverse typedef_info
   178   val rep_inv = #Rep_inverse typedef_info
   184   val abs_inv = #Abs_inverse typedef_info
   179   val abs_inv = #Abs_inverse typedef_info
   193           rtac rep_thm_simpd,
   188           rtac rep_thm_simpd,
   194           rtac rep_inv,
   189           rtac rep_inv,
   195           rtac abs_inv_simpd, rtac @{thm exI}, rtac @{thm refl},
   190           rtac abs_inv_simpd, rtac @{thm exI}, rtac @{thm refl},
   196           rtac rep_inj]
   191           rtac rep_inj]
   197 end
   192 end
   198 *}
   193 
   199 
       
   200 term QUOT_TYPE
       
   201 ML {* HOLogic.mk_Trueprop *}
       
   202 ML {* Goal.prove *}
       
   203 ML {* Syntax.check_term *}
       
   204 
       
   205 ML {*
       
   206 fun typedef_quot_type_thm (rel, abs, rep, equiv_thm, typedef_info) lthy =
   194 fun typedef_quot_type_thm (rel, abs, rep, equiv_thm, typedef_info) lthy =
   207 let
   195 let
   208   val quot_type_const = Const (@{const_name "QUOT_TYPE"}, dummyT)
   196   val quot_type_const = Const (@{const_name "QUOT_TYPE"}, dummyT)
   209   val goal = HOLogic.mk_Trueprop (quot_type_const $ rel $ abs $ rep)
   197   val goal = HOLogic.mk_Trueprop (quot_type_const $ rel $ abs $ rep)
   210              |> Syntax.check_term lthy
   198              |> Syntax.check_term lthy
   211 in
   199 in
   212   Goal.prove lthy [] [] goal
   200   Goal.prove lthy [] [] goal
   213     (fn _ => typedef_quot_type_tac equiv_thm typedef_info)
   201     (K (typedef_quot_type_tac equiv_thm typedef_info))
   214 end
   202 end
   215 *}
       
   216 
       
   217 ML {*
       
   218 fun typedef_quotient_thm_tac defs quot_type_thm =
       
   219   EVERY1 [K (rewrite_goals_tac defs),
       
   220           rtac @{thm QUOT_TYPE.QUOTIENT},
       
   221           rtac quot_type_thm]
       
   222 *}
   203 *}
   223 
   204 
   224 ML {*
   205 ML {*
   225 fun typedef_quotient_thm (rel, abs, rep, abs_def, rep_def, quot_type_thm) lthy =
   206 fun typedef_quotient_thm (rel, abs, rep, abs_def, rep_def, quot_type_thm) lthy =
   226 let
   207 let
   227   val quotient_const = Const (@{const_name "QUOTIENT"}, dummyT)
   208   val quotient_const = Const (@{const_name "QUOTIENT"}, dummyT)
   228   val goal = HOLogic.mk_Trueprop (quotient_const $ rel $ abs $ rep)
   209   val goal = HOLogic.mk_Trueprop (quotient_const $ rel $ abs $ rep)
   229              |> Syntax.check_term lthy
   210              |> Syntax.check_term lthy
   230 in
   211 
   231   Goal.prove lthy [] [] goal
   212   val typedef_quotient_thm_tac =
   232     (fn _ => typedef_quotient_thm_tac [abs_def, rep_def] quot_type_thm)
   213     EVERY1 [K (rewrite_goals_tac [abs_def, rep_def]),
       
   214             rtac @{thm QUOT_TYPE.QUOTIENT},
       
   215             rtac quot_type_thm]
       
   216 in
       
   217   Goal.prove lthy [] [] goal 
       
   218     (K typedef_quotient_thm_tac)
   233 end
   219 end
   234 *}
   220 *}
   235 
   221 
   236 text {* two wrappers for define and note *}
   222 text {* two wrappers for define and note *}
   237 ML {*
   223 ML {*
   243   ((rhs, thm), lthy')
   229   ((rhs, thm), lthy')
   244 end
   230 end
   245 *}
   231 *}
   246 
   232 
   247 ML {*
   233 ML {*
   248 fun reg_thm (name, thm) lthy =
   234 fun note_thm (name, thm) lthy =
   249 let
   235 let
   250   val ((_,[thm']), lthy') = LocalTheory.note Thm.theoremK ((name, []), [thm]) lthy
   236   val ((_,[thm']), lthy') = LocalTheory.note Thm.theoremK ((name, []), [thm]) lthy
   251 in
   237 in
   252   (thm', lthy')
   238   (thm', lthy')
   253 end
   239 end
   263 
   249 
   264 ML {*
   250 ML {*
   265 fun typedef_main (qty_name, rel, ty, equiv_thm) lthy =
   251 fun typedef_main (qty_name, rel, ty, equiv_thm) lthy =
   266 let
   252 let
   267   (* generates typedef *)
   253   (* generates typedef *)
   268   val ((_, typedef_info), lthy') = typedef_make (qty_name, rel, ty) lthy
   254   val ((_, typedef_info), lthy1) = typedef_make (qty_name, rel, ty) lthy
   269 
   255 
   270   (* abs and rep functions *)
   256   (* abs and rep functions *)
   271   val abs_ty = #abs_type typedef_info
   257   val abs_ty = #abs_type typedef_info
   272   val rep_ty = #rep_type typedef_info
   258   val rep_ty = #rep_type typedef_info
   273   val abs_name = #Abs_name typedef_info
   259   val abs_name = #Abs_name typedef_info
   276   val rep = Const (rep_name, abs_ty --> rep_ty)
   262   val rep = Const (rep_name, abs_ty --> rep_ty)
   277 
   263 
   278   (* ABS and REP definitions *)
   264   (* ABS and REP definitions *)
   279   val ABS_const = Const (@{const_name "QUOT_TYPE.ABS"}, dummyT )
   265   val ABS_const = Const (@{const_name "QUOT_TYPE.ABS"}, dummyT )
   280   val REP_const = Const (@{const_name "QUOT_TYPE.REP"}, dummyT )
   266   val REP_const = Const (@{const_name "QUOT_TYPE.REP"}, dummyT )
   281   val ABS_trm = Syntax.check_term lthy' (ABS_const $ rel $ abs)
   267   val ABS_trm = Syntax.check_term lthy1 (ABS_const $ rel $ abs)
   282   val REP_trm = Syntax.check_term lthy' (REP_const $ rep)
   268   val REP_trm = Syntax.check_term lthy1 (REP_const $ rep)
   283   val ABS_name = Binding.prefix_name "ABS_" qty_name
   269   val ABS_name = Binding.prefix_name "ABS_" qty_name
   284   val REP_name = Binding.prefix_name "REP_" qty_name
   270   val REP_name = Binding.prefix_name "REP_" qty_name
   285   val (((ABS, ABS_def), (REP, REP_def)), lthy'') =
   271   val (((ABS, ABS_def), (REP, REP_def)), lthy2) =
   286          lthy' |> make_def (ABS_name, NoSyn, ABS_trm)
   272          lthy1 |> make_def (ABS_name, NoSyn, ABS_trm)
   287                ||>> make_def (REP_name, NoSyn, REP_trm)
   273                ||>> make_def (REP_name, NoSyn, REP_trm)
   288 
   274 
   289   (* quot_type theorem *)
   275   (* quot_type theorem *)
   290   val quot_thm = typedef_quot_type_thm (rel, abs, rep, equiv_thm, typedef_info) lthy''
   276   val quot_thm = typedef_quot_type_thm (rel, abs, rep, equiv_thm, typedef_info) lthy2
   291   val quot_thm_name = Binding.prefix_name "QUOT_TYPE_" qty_name
   277   val quot_thm_name = Binding.prefix_name "QUOT_TYPE_" qty_name
   292 
   278 
   293   (* quotient theorem *)
   279   (* quotient theorem *)
   294   val quotient_thm = typedef_quotient_thm (rel, ABS, REP, ABS_def, REP_def, quot_thm) lthy''
   280   val quotient_thm = typedef_quotient_thm (rel, ABS, REP, ABS_def, REP_def, quot_thm) lthy2
   295   val quotient_thm_name = Binding.prefix_name "QUOTIENT_" qty_name
   281   val quotient_thm_name = Binding.prefix_name "QUOTIENT_" qty_name
   296 
   282 
   297   (* interpretation *)
   283   (* interpretation *)
   298   val bindd = ((Binding.make ("", Position.none)), ([]: Attrib.src list))
   284   val bindd = ((Binding.make ("", Position.none)), ([]: Attrib.src list))
   299   val ((_, [eqn1pre]), lthy''') = Variable.import true [ABS_def] lthy'';
   285   val ((_, [eqn1pre]), lthy3) = Variable.import true [ABS_def] lthy2;
   300   val eqn1i = Thm.prop_of (symmetric eqn1pre)
   286   val eqn1i = Thm.prop_of (symmetric eqn1pre)
   301   val ((_, [eqn2pre]), lthy'''') = Variable.import true [REP_def] lthy''';
   287   val ((_, [eqn2pre]), lthy4) = Variable.import true [REP_def] lthy3;
   302   val eqn2i = Thm.prop_of (symmetric eqn2pre)
   288   val eqn2i = Thm.prop_of (symmetric eqn2pre)
   303 
   289 
   304   val exp_morphism = ProofContext.export_morphism lthy'''' (ProofContext.init (ProofContext.theory_of lthy''''));
   290   val exp_morphism = ProofContext.export_morphism lthy4 (ProofContext.init (ProofContext.theory_of lthy4));
   305   val exp_term = Morphism.term exp_morphism;
   291   val exp_term = Morphism.term exp_morphism;
   306   val exp = Morphism.thm exp_morphism;
   292   val exp = Morphism.thm exp_morphism;
   307 
   293 
   308   val mthd = Method.SIMPLE_METHOD ((rtac quot_thm 1) THEN
   294   val mthd = Method.SIMPLE_METHOD ((rtac quot_thm 1) THEN
   309     ALLGOALS (simp_tac (HOL_basic_ss addsimps [(symmetric (exp ABS_def)), (symmetric (exp REP_def))])))
   295     ALLGOALS (simp_tac (HOL_basic_ss addsimps [(symmetric (exp ABS_def)), (symmetric (exp REP_def))])))
   314      ("R", rel),
   300      ("R", rel),
   315      ("Abs", abs),
   301      ("Abs", abs),
   316      ("Rep", rep)
   302      ("Rep", rep)
   317     ]))]
   303     ]))]
   318 in
   304 in
   319   lthy''''
   305   lthy4
   320   |> reg_thm (quot_thm_name, quot_thm)
   306   |> note_thm (quot_thm_name, quot_thm)
   321   ||>> reg_thm (quotient_thm_name, quotient_thm)
   307   ||>> note_thm (quotient_thm_name, quotient_thm)
   322   ||> LocalTheory.theory (fn thy =>
   308   ||> LocalTheory.theory (fn thy =>
   323       let
   309       let
   324         val global_eqns = map exp_term [eqn2i, eqn1i];
   310         val global_eqns = map exp_term [eqn2i, eqn1i];
   325         (* Not sure if the following context should not be used *)
   311         (* Not sure if the following context should not be used *)
   326         val (global_eqns2, lthy''''') = Variable.import_terms true global_eqns lthy'''';
   312         val (global_eqns2, lthy5) = Variable.import_terms true global_eqns lthy4;
   327         val global_eqns3 = map (fn t => (bindd, t)) global_eqns2;
   313         val global_eqns3 = map (fn t => (bindd, t)) global_eqns2;
   328       in ProofContext.theory_of (bymt (Expression.interpretation (exp_i, []) global_eqns3 thy)) end)
   314       in ProofContext.theory_of (bymt (Expression.interpretation (exp_i, []) global_eqns3 thy)) end)
   329 end
   315 end
   330 *}
   316 *}
   331 
   317 
   333 datatype trm =
   319 datatype trm =
   334   var  "nat"
   320   var  "nat"
   335 | app  "trm" "trm"
   321 | app  "trm" "trm"
   336 | lam  "nat" "trm"
   322 | lam  "nat" "trm"
   337 
   323 
   338 axiomatization RR :: "trm \<Rightarrow> trm \<Rightarrow> bool" where
   324 axiomatization 
       
   325   RR :: "trm \<Rightarrow> trm \<Rightarrow> bool" 
       
   326 where
   339   r_eq: "EQUIV RR"
   327   r_eq: "EQUIV RR"
   340 
       
   341 ML {*
       
   342   typedef_main
       
   343 *}
       
   344 
   328 
   345 local_setup {*
   329 local_setup {*
   346   typedef_main (@{binding "qtrm"}, @{term "RR"}, @{typ trm}, @{thm r_eq}) #> snd
   330   typedef_main (@{binding "qtrm"}, @{term "RR"}, @{typ trm}, @{thm r_eq}) #> snd
   347 *}
   331 *}
   348 
   332 
   353 thm QUOT_TYPE_qtrm
   337 thm QUOT_TYPE_qtrm
   354 thm QUOTIENT_qtrm
   338 thm QUOTIENT_qtrm
   355 
   339 
   356 (* Test interpretation *)
   340 (* Test interpretation *)
   357 thm QUOT_TYPE_I_qtrm.thm11
   341 thm QUOT_TYPE_I_qtrm.thm11
       
   342 thm QUOT_TYPE.thm11
   358 
   343 
   359 print_theorems
   344 print_theorems
   360 
   345 
   361 thm Rep_qtrm
   346 thm Rep_qtrm
   362 
   347