src/HOL/Tools/BNF/bnf_def.ML
changeset 56016 8875cdcfc85b
parent 55945 e96383acecf9
child 56346 42533f8f4729
equal deleted inserted replaced
56015:57e2cfba9c6e 56016:8875cdcfc85b
    10 sig
    10 sig
    11   type bnf
    11   type bnf
    12   type nonemptiness_witness = {I: int list, wit: term, prop: thm list}
    12   type nonemptiness_witness = {I: int list, wit: term, prop: thm list}
    13 
    13 
    14   val morph_bnf: morphism -> bnf -> bnf
    14   val morph_bnf: morphism -> bnf -> bnf
       
    15   val morph_bnf_defs: morphism -> bnf -> bnf
    15   val bnf_of: Proof.context -> string -> bnf option
    16   val bnf_of: Proof.context -> string -> bnf option
    16   val register_bnf: string -> (bnf * local_theory) -> (bnf * local_theory)
    17   val register_bnf: string -> (bnf * local_theory) -> (bnf * local_theory)
    17 
    18 
    18   val name_of_bnf: bnf -> binding
    19   val name_of_bnf: bnf -> binding
    19   val T_of_bnf: bnf -> typ
    20   val T_of_bnf: bnf -> typ
    98   val user_policy: fact_policy -> Proof.context -> fact_policy
    99   val user_policy: fact_policy -> Proof.context -> fact_policy
    99   val note_bnf_thms: fact_policy -> (binding -> binding) -> binding -> bnf -> Proof.context ->
   100   val note_bnf_thms: fact_policy -> (binding -> binding) -> binding -> bnf -> Proof.context ->
   100     Proof.context
   101     Proof.context
   101 
   102 
   102   val print_bnfs: Proof.context -> unit
   103   val print_bnfs: Proof.context -> unit
   103   val prepare_def: inline_policy -> (Proof.context -> fact_policy) -> (binding -> binding) ->
   104   val prepare_def: inline_policy -> (Proof.context -> fact_policy) -> bool ->
   104     (Proof.context -> 'a -> typ) -> (Proof.context -> 'b -> term) -> typ list option ->
   105     (binding -> binding) -> (Proof.context -> 'a -> typ) -> (Proof.context -> 'b -> term) ->
   105     binding -> binding -> binding list ->
   106     typ list option -> binding -> binding -> binding list ->
   106     (((((binding * 'a) * 'b) * 'b list) * 'b) * 'b list) * 'b option -> Proof.context ->
   107     (((((binding * 'a) * 'b) * 'b list) * 'b) * 'b list) * 'b option -> Proof.context ->
   107     string * term list *
   108     string * term list *
   108     ((Proof.context -> thm list -> tactic) option * term list list) *
   109     ((Proof.context -> thm list -> tactic) option * term list list) *
   109     ((thm list -> thm list list) -> thm list list -> Proof.context -> bnf * local_theory) *
   110     ((thm list -> thm list list) -> thm list list -> Proof.context -> bnf * local_theory) *
   110     local_theory * thm list
   111     local_theory * thm list
   111 
   112 
   112   val define_bnf_consts: inline_policy -> fact_policy -> typ list option ->
   113   val define_bnf_consts: inline_policy -> fact_policy -> bool -> typ list option ->
   113     binding -> binding -> binding list ->
   114     binding -> binding -> binding list ->
   114     (((((binding * typ) * term) * term list) * term) * term list) * term option -> local_theory ->
   115     (((((binding * typ) * term) * term list) * term) * term list) * term option -> local_theory ->
   115       ((typ list * typ list * typ list * typ) *
   116       ((typ list * typ list * typ list * typ) *
   116        (term * term list * term * (int list * term) list * term) *
   117        (term * term list * term * (int list * term) list * term) *
   117        (thm * thm list * thm * thm list * thm) *
   118        (thm * thm list * thm * thm list * thm) *
   119         (typ list -> typ list -> term -> term) *
   120         (typ list -> typ list -> term -> term) *
   120         (typ list -> typ list -> typ -> typ) *
   121         (typ list -> typ list -> typ -> typ) *
   121         (typ list -> typ list -> typ list -> term) *
   122         (typ list -> typ list -> typ list -> term) *
   122         (typ list -> typ list -> typ list -> term))) * local_theory
   123         (typ list -> typ list -> typ list -> term))) * local_theory
   123 
   124 
   124   val bnf_def: inline_policy -> (Proof.context -> fact_policy) -> (binding -> binding) ->
   125   val bnf_def: inline_policy -> (Proof.context -> fact_policy) -> bool -> (binding -> binding) ->
   125     (Proof.context -> tactic) list ->
   126     (Proof.context -> tactic) list ->
   126     (Proof.context -> tactic) -> typ list option -> binding ->
   127     (Proof.context -> tactic) -> typ list option -> binding ->
   127     binding -> binding list ->
   128     binding -> binding list ->
   128     (((((binding * typ) * term) * term list) * term) * term list) * term option ->
   129     (((((binding * typ) * term) * term list) * term) * term list) * term option ->
   129     local_theory -> bnf * local_theory
   130     local_theory -> bnf * local_theory
   432        live = live, lives = lives, lives' = lives', dead = dead, deads = deads,
   433        live = live, lives = lives, lives' = lives', dead = dead, deads = deads,
   433        map = map, sets = sets, bd = bd,
   434        map = map, sets = sets, bd = bd,
   434        axioms = axioms, defs = defs, facts = facts,
   435        axioms = axioms, defs = defs, facts = facts,
   435        nwits = length wits, wits = wits, rel = rel};
   436        nwits = length wits, wits = wits, rel = rel};
   436 
   437 
   437 fun morph_bnf phi (BNF {name = name, T = T, live = live, lives = lives, lives' = lives',
   438 fun map_bnf f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16
       
   439   (BNF {name = name, T = T, live = live, lives = lives, lives' = lives',
   438   dead = dead, deads = deads, map = map, sets = sets, bd = bd,
   440   dead = dead, deads = deads, map = map, sets = sets, bd = bd,
   439   axioms = axioms, defs = defs, facts = facts,
   441   axioms = axioms, defs = defs, facts = facts,
   440   nwits = nwits, wits = wits, rel = rel}) =
   442   nwits = nwits, wits = wits, rel = rel}) =
   441   BNF {name = Morphism.binding phi name, T = Morphism.typ phi T,
   443   BNF {name = f1 name, T = f2 T,
   442     live = live, lives = List.map (Morphism.typ phi) lives,
   444        live = f3 live, lives = f4 lives, lives' = f5 lives', dead = f6 dead, deads = f7 deads,
   443     lives' = List.map (Morphism.typ phi) lives',
   445        map = f8 map, sets = f9 sets, bd = f10 bd,
   444     dead = dead, deads = List.map (Morphism.typ phi) deads,
   446        axioms = f11 axioms, defs = f12 defs, facts = f13 facts,
   445     map = Morphism.term phi map, sets = List.map (Morphism.term phi) sets,
   447        nwits = f14 nwits, wits = f15 wits, rel = f16 rel};
   446     bd = Morphism.term phi bd,
   448 
   447     axioms = morph_axioms phi axioms,
   449 fun morph_bnf phi =
   448     defs = morph_defs phi defs,
   450   let
   449     facts = morph_facts phi facts,
   451     val Tphi = Morphism.typ phi;
   450     nwits = nwits,
   452     val tphi = Morphism.term phi;
   451     wits = List.map (morph_witness phi) wits,
   453   in
   452     rel = Morphism.term phi rel};
   454     map_bnf (Morphism.binding phi) Tphi I (map Tphi) (map Tphi) I (map Tphi) tphi (map tphi) tphi
       
   455       (morph_axioms phi) (morph_defs phi) (morph_facts phi) I (map (morph_witness phi)) tphi
       
   456   end;
       
   457 
       
   458 fun morph_bnf_defs phi = map_bnf I I I I I I I I I I I (morph_defs phi) I I I I;
   453 
   459 
   454 structure Data = Generic_Data
   460 structure Data = Generic_Data
   455 (
   461 (
   456   type T = bnf Symtab.table;
   462   type T = bnf Symtab.table;
   457   val empty = Symtab.empty;
   463   val empty = Symtab.empty;
   658   end;
   664   end;
   659 
   665 
   660 
   666 
   661 (* Define new BNFs *)
   667 (* Define new BNFs *)
   662 
   668 
   663 fun define_bnf_consts const_policy fact_policy Ds_opt map_b rel_b set_bs
   669 fun define_bnf_consts const_policy fact_policy internal Ds_opt map_b rel_b set_bs
   664   ((((((bnf_b, T_rhs), map_rhs), set_rhss), bd_rhs), wit_rhss), rel_rhs_opt) no_defs_lthy =
   670   ((((((bnf_b, T_rhs), map_rhs), set_rhss), bd_rhs), wit_rhss), rel_rhs_opt) no_defs_lthy =
   665   let
   671   let
   666     val live = length set_rhss;
   672     val live = length set_rhss;
   667 
   673 
   668     val def_qualify = Binding.conceal o Binding.qualify false (Binding.name_of bnf_b);
   674     val def_qualify = Binding.conceal o Binding.qualify false (Binding.name_of bnf_b);
   681       in
   687       in
   682         if inline then
   688         if inline then
   683           ((rhs, Drule.reflexive_thm), lthy)
   689           ((rhs, Drule.reflexive_thm), lthy)
   684         else
   690         else
   685           let val b = b () in
   691           let val b = b () in
   686             apfst (apsnd snd) (Local_Theory.define ((b, NoSyn), ((Thm.def_binding b, []), rhs))
   692             apfst (apsnd snd)
   687               lthy)
   693               ((if internal then Local_Theory.define_internal else Local_Theory.define)
       
   694                 ((b, NoSyn), ((Thm.def_binding b, []), rhs)) lthy)
   688           end
   695           end
   689       end;
   696       end;
   690 
   697 
   691     fun maybe_restore lthy_old lthy =
   698     fun maybe_restore lthy_old lthy =
   692       lthy |> not (pointer_eq (lthy_old, lthy)) ? Local_Theory.restore;
   699       lthy |> not (pointer_eq (lthy_old, lthy)) ? Local_Theory.restore;
   828      (bnf_map, bnf_sets, bnf_bd, bnf_wits, bnf_rel),
   835      (bnf_map, bnf_sets, bnf_bd, bnf_wits, bnf_rel),
   829      (bnf_map_def, bnf_set_defs, bnf_bd_def, bnf_wit_defs, bnf_rel_def),
   836      (bnf_map_def, bnf_set_defs, bnf_bd_def, bnf_wit_defs, bnf_rel_def),
   830      (mk_bnf_map, mk_bnf_t, mk_bnf_T, mk_bnf_rel, mk_OO_Grp)), lthy)
   837      (mk_bnf_map, mk_bnf_t, mk_bnf_T, mk_bnf_rel, mk_OO_Grp)), lthy)
   831   end;
   838   end;
   832 
   839 
   833 fun prepare_def const_policy mk_fact_policy qualify prep_typ prep_term Ds_opt map_b rel_b set_bs
   840 fun prepare_def const_policy mk_fact_policy internal qualify prep_typ prep_term Ds_opt map_b rel_b
   834   ((((((raw_bnf_b, raw_bnf_T), raw_map), raw_sets), raw_bd), raw_wits), raw_rel_opt)
   841   set_bs ((((((raw_bnf_b, raw_bnf_T), raw_map), raw_sets), raw_bd), raw_wits), raw_rel_opt)
   835   no_defs_lthy =
   842   no_defs_lthy =
   836   let
   843   let
   837     val fact_policy = mk_fact_policy no_defs_lthy;
   844     val fact_policy = mk_fact_policy no_defs_lthy;
   838     val bnf_b = qualify raw_bnf_b;
   845     val bnf_b = qualify raw_bnf_b;
   839     val live = length raw_sets;
   846     val live = length raw_sets;
   859 
   866 
   860     val (((alphas, betas, deads, Calpha),
   867     val (((alphas, betas, deads, Calpha),
   861      (bnf_map, bnf_sets, bnf_bd, bnf_wits, bnf_rel),
   868      (bnf_map, bnf_sets, bnf_bd, bnf_wits, bnf_rel),
   862      (bnf_map_def, bnf_set_defs, bnf_bd_def, bnf_wit_defs, bnf_rel_def),
   869      (bnf_map_def, bnf_set_defs, bnf_bd_def, bnf_wit_defs, bnf_rel_def),
   863      (mk_bnf_map_Ds, mk_bnf_t_Ds, mk_bnf_T_Ds, _, mk_OO_Grp)), lthy) =
   870      (mk_bnf_map_Ds, mk_bnf_t_Ds, mk_bnf_T_Ds, _, mk_OO_Grp)), lthy) =
   864        define_bnf_consts const_policy fact_policy Ds_opt map_b rel_b set_bs
   871        define_bnf_consts const_policy fact_policy internal Ds_opt map_b rel_b set_bs
   865          ((((((bnf_b, T_rhs), map_rhs), set_rhss), bd_rhs), wit_rhss), rel_rhs_opt) no_defs_lthy;
   872          ((((((bnf_b, T_rhs), map_rhs), set_rhss), bd_rhs), wit_rhss), rel_rhs_opt) no_defs_lthy;
   866 
   873 
   867     val dead = length deads;
   874     val dead = length deads;
   868 
   875 
   869     val ((((((As', Bs'), Cs), Ds), B1Ts), B2Ts), (Ts, T)) = lthy
   876     val ((((((As', Bs'), Cs), Ds), B1Ts), B2Ts), (Ts, T)) = lthy
  1302 
  1309 
  1303 fun register_bnf key (bnf, lthy) =
  1310 fun register_bnf key (bnf, lthy) =
  1304   (bnf, Local_Theory.declaration {syntax = false, pervasive = true}
  1311   (bnf, Local_Theory.declaration {syntax = false, pervasive = true}
  1305     (fn phi => Data.map (Symtab.update (key, morph_bnf phi bnf))) lthy);
  1312     (fn phi => Data.map (Symtab.update (key, morph_bnf phi bnf))) lthy);
  1306 
  1313 
  1307 fun bnf_def const_policy fact_policy qualify tacs wit_tac Ds map_b rel_b set_bs =
  1314 fun bnf_def const_policy fact_policy internal qualify tacs wit_tac Ds map_b rel_b set_bs =
  1308   (fn (_, goals, (triv_tac_opt, wit_goalss), after_qed, lthy, one_step_defs) =>
  1315   (fn (_, goals, (triv_tac_opt, wit_goalss), after_qed, lthy, one_step_defs) =>
  1309   let
  1316   let
  1310     fun mk_wits_tac ctxt set_maps =
  1317     fun mk_wits_tac ctxt set_maps =
  1311       TRYALL Goal.conjunction_tac THEN
  1318       TRYALL Goal.conjunction_tac THEN
  1312       (case triv_tac_opt of
  1319       (case triv_tac_opt of
  1322   in
  1329   in
  1323     map2 (Thm.close_derivation oo Goal.prove_sorry lthy [] [])
  1330     map2 (Thm.close_derivation oo Goal.prove_sorry lthy [] [])
  1324       goals (map (fn tac => fn {context = ctxt, prems = _} =>
  1331       goals (map (fn tac => fn {context = ctxt, prems = _} =>
  1325         unfold_thms_tac ctxt one_step_defs THEN tac ctxt) tacs)
  1332         unfold_thms_tac ctxt one_step_defs THEN tac ctxt) tacs)
  1326     |> (fn thms => after_qed mk_wit_thms (map single thms) lthy)
  1333     |> (fn thms => after_qed mk_wit_thms (map single thms) lthy)
  1327   end) oo prepare_def const_policy fact_policy qualify (K I) (K I) Ds map_b rel_b set_bs;
  1334   end) oo prepare_def const_policy fact_policy internal qualify (K I) (K I) Ds map_b rel_b set_bs;
  1328 
  1335 
  1329 val bnf_cmd = (fn (key, goals, (triv_tac_opt, wit_goalss), after_qed, lthy, defs) =>
  1336 val bnf_cmd = (fn (key, goals, (triv_tac_opt, wit_goalss), after_qed, lthy, defs) =>
  1330   let
  1337   let
  1331     val wit_goals = map Logic.mk_conjunction_balanced wit_goalss;
  1338     val wit_goals = map Logic.mk_conjunction_balanced wit_goalss;
  1332     fun mk_triv_wit_thms tac set_maps =
  1339     fun mk_triv_wit_thms tac set_maps =
  1341       | SOME tac => (mk_triv_wit_thms tac, []));
  1348       | SOME tac => (mk_triv_wit_thms tac, []));
  1342   in
  1349   in
  1343     Proof.unfolding ([[(defs, [])]])
  1350     Proof.unfolding ([[(defs, [])]])
  1344       (Proof.theorem NONE (snd o register_bnf key oo after_qed mk_wit_thms)
  1351       (Proof.theorem NONE (snd o register_bnf key oo after_qed mk_wit_thms)
  1345         (map (single o rpair []) goals @ nontriv_wit_goals) lthy)
  1352         (map (single o rpair []) goals @ nontriv_wit_goals) lthy)
  1346   end) oo prepare_def Do_Inline (user_policy Note_Some) I Syntax.read_typ Syntax.read_term NONE
  1353   end) oo prepare_def Do_Inline (user_policy Note_Some) false I Syntax.read_typ Syntax.read_term
  1347     Binding.empty Binding.empty [];
  1354     NONE Binding.empty Binding.empty [];
  1348 
  1355 
  1349 fun print_bnfs ctxt =
  1356 fun print_bnfs ctxt =
  1350   let
  1357   let
  1351     fun pretty_set sets i = Pretty.block
  1358     fun pretty_set sets i = Pretty.block
  1352       [Pretty.str (mk_setN (i + 1) ^ ":"), Pretty.brk 1,
  1359       [Pretty.str (mk_setN (i + 1) ^ ":"), Pretty.brk 1,