src/HOL/Tools/datatype_codegen.ML
changeset 21546 268b6bed0cc8
parent 21516 c2a116a2c4fd
child 21565 bd28361f4c5b
equal deleted inserted replaced
21545:54cc492d80a9 21546:268b6bed0cc8
   404 end;
   404 end;
   405 
   405 
   406 local
   406 local
   407   val not_sym = thm "HOL.not_sym";
   407   val not_sym = thm "HOL.not_sym";
   408   val not_false_true = iffD2 OF [nth (thms "HOL.simp_thms") 7, TrueI];
   408   val not_false_true = iffD2 OF [nth (thms "HOL.simp_thms") 7, TrueI];
       
   409   val refl = thm "refl";
       
   410   val eqTrueI = thm "eqTrueI";
   409 in
   411 in
   410 
   412 
   411 fun get_eq_datatype thy dtco =
   413 fun get_eq_datatype thy dtco =
   412   let
   414   let
   413     val SOME (vs, cs) = DatatypePackage.get_datatype_spec thy dtco;
   415     val SOME (vs, cs) = DatatypePackage.get_datatype_spec thy dtco;
   414     fun mk_triv_inject co =
   416     fun mk_triv_inject co =
   415       let
   417       let
   416         val ct' = Thm.cterm_of thy
   418         val ct' = Thm.cterm_of thy
   417           (Const (co, Type (dtco, map (fn (v, sort) => TVar ((v, 0), sort)) vs)))
   419           (Const (co, Type (dtco, map (fn (v, sort) => TVar ((v, 0), sort)) vs)))
   418         val cty' = Thm.ctyp_of_term ct';
   420         val cty' = Thm.ctyp_of_term ct';
   419         val refl = Thm.prop_of HOL.refl;
       
   420         val SOME (ct, cty) = fold_aterms (fn Var (v, ty) =>
   421         val SOME (ct, cty) = fold_aterms (fn Var (v, ty) =>
   421           (K o SOME) (Thm.cterm_of thy (Var (v, Thm.typ_of cty')), Thm.ctyp_of thy ty) | _ => I)
   422           (K o SOME) (Thm.cterm_of thy (Var (v, Thm.typ_of cty')), Thm.ctyp_of thy ty) | _ => I)
   422           refl NONE;
   423           (Thm.prop_of refl) NONE;
   423       in eqTrueI OF [Thm.instantiate ([(cty, cty')], [(ct, ct')]) HOL.refl] end;
   424       in eqTrueI OF [Thm.instantiate ([(cty, cty')], [(ct, ct')]) refl] end;
   424     val inject1 = map_filter (fn (co, []) => SOME (mk_triv_inject co) | _ => NONE) cs
   425     val inject1 = map_filter (fn (co, []) => SOME (mk_triv_inject co) | _ => NONE) cs
   425     val inject2 = (#inject o DatatypePackage.the_datatype thy) dtco;
   426     val inject2 = (#inject o DatatypePackage.the_datatype thy) dtco;
   426     val ctxt = ProofContext.init thy;
   427     val ctxt = ProofContext.init thy;
   427     val simpset = Simplifier.context ctxt
   428     val simpset = Simplifier.context ctxt
   428       (MetaSimplifier.empty_ss addsimprocs [distinct_simproc]);
   429       (MetaSimplifier.empty_ss addsimprocs [distinct_simproc]);
   496 
   497 
   497 local
   498 local
   498   fun get_eq_thms thy tyco = case DatatypePackage.get_datatype thy tyco
   499   fun get_eq_thms thy tyco = case DatatypePackage.get_datatype thy tyco
   499    of SOME _ => get_eq_datatype thy tyco
   500    of SOME _ => get_eq_datatype thy tyco
   500     | NONE => [TypecopyPackage.get_eq thy tyco];
   501     | NONE => [TypecopyPackage.get_eq thy tyco];
       
   502   fun constrain_op_eq_thms thy thms =
       
   503     let
       
   504       fun add_eq (Const ("op =", ty)) =
       
   505             fold (insert (eq_fst (op =)))
       
   506               (Term.add_tvarsT ty [])
       
   507         | add_eq _ =
       
   508             I
       
   509       val eqs = fold (fold_aterms add_eq o Thm.prop_of) thms [];
       
   510       val instT = map (fn (v_i, sort) =>
       
   511         (Thm.ctyp_of thy (TVar (v_i, sort)),
       
   512            Thm.ctyp_of thy (TVar (v_i, Sorts.inter_sort (Sign.classes_of thy) (sort, [HOLogic.class_eq]))))) eqs;
       
   513     in
       
   514       thms
       
   515       |> map (Thm.instantiate (instT, []))
       
   516     end;
   501 in
   517 in
   502   fun get_eq thy tyco =
   518   fun get_eq thy tyco =
   503     get_eq_thms thy tyco
   519     get_eq_thms thy tyco
   504     |> maps ((#mk o #mk_rews o snd o MetaSimplifier.rep_ss o Simplifier.simpset_of) thy)
   520     |> maps ((#mk o #mk_rews o snd o MetaSimplifier.rep_ss o Simplifier.simpset_of) thy)
   505     |> HOL.constrain_op_eq_thms thy
   521     |> constrain_op_eq_thms thy
   506 end;
   522 end;
   507 
   523 
   508 type hook = (string * (bool * ((string * sort) list * (string * typ list) list))) list
   524 type hook = (string * (bool * ((string * sort) list * (string * typ list) list))) list
   509   -> theory -> theory;
   525   -> theory -> theory;
   510 
   526