src/HOL/Tools/BNF/bnf_axiomatization.ML
author wenzelm
Fri, 09 Aug 2019 17:14:49 +0200
changeset 70494 41108e3e9ca5
parent 69593 3dda49e08b9d
permissions -rw-r--r--
formal position for PThm nodes;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62691
9bfcbab7cd99 put all 'bnf_*.ML' files together, irrespective of bootstrapping/dependency constraints
blanchet
parents: 62531
diff changeset
     1
(*  Title:      HOL/Tools/BNF/bnf_axiomatization.ML
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
     2
    Author:     Dmitriy Traytel, TU Muenchen
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
     3
    Copyright   2013
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
     4
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
     5
Axiomatic declaration of bounded natural functors.
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
     6
*)
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
     7
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
     8
signature BNF_AXIOMATIZATION =
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
     9
sig
58189
9d714be4f028 added 'plugins' option to control which hooks are enabled
blanchet
parents: 58188
diff changeset
    10
  val bnf_axiomatization: (string -> bool) -> (binding option * (typ * sort)) list -> binding ->
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 61259
diff changeset
    11
    mixfix -> binding -> binding -> binding -> typ list -> local_theory ->
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 61259
diff changeset
    12
    BNF_Def.bnf * local_theory
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    13
end
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    14
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    15
structure BNF_Axiomatization : BNF_AXIOMATIZATION =
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    16
struct
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    17
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    18
open BNF_Util
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    19
open BNF_Def
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    20
58659
6c9821c32dd5 Local_Interpretation is superseded by Plugin with formal Plugin_Name management, avoiding undeclared strings;
wenzelm
parents: 58189
diff changeset
    21
fun prepare_decl prepare_plugins prepare_constraint prepare_typ
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 61259
diff changeset
    22
    raw_plugins raw_vars b mx user_mapb user_relb user_predb user_witTs lthy =
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    23
  let
58659
6c9821c32dd5 Local_Interpretation is superseded by Plugin with formal Plugin_Name management, avoiding undeclared strings;
wenzelm
parents: 58189
diff changeset
    24
   val plugins = prepare_plugins lthy raw_plugins;
6c9821c32dd5 Local_Interpretation is superseded by Plugin with formal Plugin_Name management, avoiding undeclared strings;
wenzelm
parents: 58189
diff changeset
    25
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    26
   fun prepare_type_arg (set_opt, (ty, c)) =
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    27
      let val s = fst (dest_TFree (prepare_typ lthy ty)) in
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    28
        (set_opt, (s, prepare_constraint lthy c))
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    29
      end;
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    30
    val ((user_setbs, vars), raw_vars') =
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    31
      map prepare_type_arg raw_vars
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    32
      |> `split_list
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    33
      |>> apfst (map_filter I);
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    34
    val deads = map_filter (fn (NONE, x) => SOME x | _ => NONE) raw_vars';
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    35
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    36
    fun mk_b name user_b =
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    37
      (if Binding.is_empty user_b then Binding.prefix_name (name ^ "_") b else user_b)
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    38
      |> Binding.qualify false (Binding.name_of b);
61259
6dc3d5d50e57 tuned signature;
wenzelm
parents: 61246
diff changeset
    39
    val (Tname, lthy) = Typedecl.basic_typedecl {final = true} (b, length vars, mx) lthy;
6dc3d5d50e57 tuned signature;
wenzelm
parents: 61246
diff changeset
    40
    val (bd_type_Tname, lthy) = lthy
6dc3d5d50e57 tuned signature;
wenzelm
parents: 61246
diff changeset
    41
      |> Typedecl.basic_typedecl {final = true} (mk_b "bd_type" Binding.empty, length deads, NoSyn);
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    42
    val T = Type (Tname, map TFree vars);
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    43
    val bd_type_T = Type (bd_type_Tname, map TFree deads);
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    44
    val lives = map TFree (filter_out (member (op =) deads) vars);
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    45
    val live = length lives;
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    46
    val _ = "Trying to declare a BNF with no live variables" |> null lives ? error;
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    47
    val (lives', _) = BNF_Util.mk_TFrees (length lives)
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    48
      (fold Variable.declare_typ (map TFree vars) lthy);
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    49
    val T' = Term.typ_subst_atomic (lives ~~ lives') T;
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    50
    val mapT = map2 (curry op -->) lives lives' ---> T --> T';
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    51
    val setTs = map (fn U => T --> HOLogic.mk_setT U) lives;
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    52
    val bdT = BNF_Util.mk_relT (bd_type_T, bd_type_T);
58189
9d714be4f028 added 'plugins' option to control which hooks are enabled
blanchet
parents: 58188
diff changeset
    53
    val mapb = mk_b mapN user_mapb;
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    54
    val bdb = mk_b "bd" Binding.empty;
58189
9d714be4f028 added 'plugins' option to control which hooks are enabled
blanchet
parents: 58188
diff changeset
    55
    val setbs = map2 (fn b => fn i => mk_b (mk_setN i) b) user_setbs
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    56
      (if live = 1 then [0] else 1 upto live);
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    57
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    58
    val witTs = map (prepare_typ lthy) user_witTs;
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    59
    val nwits = length witTs;
58189
9d714be4f028 added 'plugins' option to control which hooks are enabled
blanchet
parents: 58188
diff changeset
    60
    val witbs = map (fn i => mk_b (mk_witN i) Binding.empty)
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    61
      (if nwits = 1 then [0] else 1 upto nwits);
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    62
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    63
    val lthy = Local_Theory.background_theory
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    64
      (Sign.add_consts ((mapb, mapT, NoSyn) :: (bdb, bdT, NoSyn) ::
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    65
        map2 (fn b => fn T => (b, T, NoSyn)) setbs setTs @
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    66
        map2 (fn b => fn T => (b, T, NoSyn)) witbs witTs))
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    67
      lthy;
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    68
    val Fmap = Const (Local_Theory.full_name lthy mapb, mapT);
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    69
    val Fsets = map2 (fn setb => fn setT =>
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    70
      Const (Local_Theory.full_name lthy setb, setT)) setbs setTs;
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    71
    val Fbd = Const (Local_Theory.full_name lthy bdb, bdT);
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    72
    val Fwits = map2 (fn witb => fn witT =>
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    73
      Const (Local_Theory.full_name lthy witb, witT)) witbs witTs;
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    74
    val (key, goals, (triv_tac_opt, wit_goalss), after_qed, lthy, _) =
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    75
      prepare_def Do_Inline (user_policy Note_Some) false I (K I) (K I) (SOME (map TFree deads))
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 61259
diff changeset
    76
      user_mapb user_relb user_predb user_setbs
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 61259
diff changeset
    77
      (((((((Binding.empty, T), Fmap), Fsets), Fbd), Fwits), NONE), NONE)
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    78
      lthy;
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    79
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    80
    fun mk_wits_tac ctxt set_maps = TRYALL Goal.conjunction_tac THEN the triv_tac_opt ctxt set_maps;
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    81
    val wit_goals = map Logic.mk_conjunction_balanced wit_goalss;
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    82
    val all_goalss = map single goals @ (if nwits > 0 then wit_goalss else []);
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    83
63178
b9e1d53124f5 clarified 'axiomatization';
wenzelm
parents: 63064
diff changeset
    84
    val (((_, raw_thms)), lthy) = Local_Theory.background_theory_result
b9e1d53124f5 clarified 'axiomatization';
wenzelm
parents: 63064
diff changeset
    85
      (Specification.axiomatization [] [] []
b9e1d53124f5 clarified 'axiomatization';
wenzelm
parents: 63064
diff changeset
    86
        (map_index (fn (i, ax) =>
b9e1d53124f5 clarified 'axiomatization';
wenzelm
parents: 63064
diff changeset
    87
          ((mk_b ("axiom" ^ string_of_int (i + 1)) Binding.empty, []), ax)) (flat all_goalss))) lthy;
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    88
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    89
    fun mk_wit_thms set_maps =
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    90
      Goal.prove_sorry lthy [] [] (Logic.mk_conjunction_balanced wit_goals)
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    91
        (fn {context = ctxt, prems = _} => mk_wits_tac ctxt set_maps)
70494
41108e3e9ca5 formal position for PThm nodes;
wenzelm
parents: 69593
diff changeset
    92
      |> Thm.close_derivation \<^here>
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    93
      |> Conjunction.elim_balanced (length wit_goals)
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    94
      |> map2 (Conjunction.elim_balanced o length) wit_goalss
61116
6189d179c2b5 close derivation *before* splitting conjuncts, like Goal.prove_common (see also 757cad5a3fe9) -- potential improvement of performance;
wenzelm
parents: 59936
diff changeset
    95
      |> (map o map) (Thm.forall_elim_vars 0);
62531
b5d656bf0441 less resetting of local theories
traytel
parents: 62514
diff changeset
    96
    val phi = Local_Theory.target_morphism lthy;
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    97
    val thms = unflat all_goalss (Morphism.fact phi raw_thms);
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    98
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
    99
    val (bnf, lthy') = after_qed mk_wit_thms thms lthy
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   100
  in
58189
9d714be4f028 added 'plugins' option to control which hooks are enabled
blanchet
parents: 58188
diff changeset
   101
    (bnf, register_bnf plugins key bnf lthy')
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   102
  end;
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   103
58659
6c9821c32dd5 Local_Interpretation is superseded by Plugin with formal Plugin_Name management, avoiding undeclared strings;
wenzelm
parents: 58189
diff changeset
   104
val bnf_axiomatization = prepare_decl (K I) (K I) (K I);
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   105
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63178
diff changeset
   106
fun read_constraint _ NONE = \<^sort>\<open>type\<close>
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   107
  | read_constraint ctxt (SOME s) = Syntax.read_sort ctxt s;
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   108
58659
6c9821c32dd5 Local_Interpretation is superseded by Plugin with formal Plugin_Name management, avoiding undeclared strings;
wenzelm
parents: 58189
diff changeset
   109
val bnf_axiomatization_cmd = prepare_decl Plugin_Name.make_filter read_constraint Syntax.read_typ;
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   110
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   111
val parse_witTs =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63178
diff changeset
   112
  \<^keyword>\<open>[\<close> |-- (Parse.name --| \<^keyword>\<open>:\<close> -- Scan.repeat Parse.typ
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   113
    >> (fn ("wits", Ts) => Ts
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   114
         | (s, _) => error ("Unknown label " ^ quote s ^ " (expected \"wits\")"))) --|
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63178
diff changeset
   115
  \<^keyword>\<open>]\<close> || Scan.succeed [];
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   116
58189
9d714be4f028 added 'plugins' option to control which hooks are enabled
blanchet
parents: 58188
diff changeset
   117
val parse_bnf_axiomatization_options =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63178
diff changeset
   118
  Scan.optional (\<^keyword>\<open>(\<close> |-- Plugin_Name.parse_filter --| \<^keyword>\<open>)\<close>) (K (K true));
58189
9d714be4f028 added 'plugins' option to control which hooks are enabled
blanchet
parents: 58188
diff changeset
   119
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   120
val parse_bnf_axiomatization =
58189
9d714be4f028 added 'plugins' option to control which hooks are enabled
blanchet
parents: 58188
diff changeset
   121
  parse_bnf_axiomatization_options -- parse_type_args_named_constrained -- Parse.binding --
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 61259
diff changeset
   122
  parse_witTs -- Parse.opt_mixfix -- parse_map_rel_pred_bindings;
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   123
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   124
val _ =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63178
diff changeset
   125
  Outer_Syntax.local_theory \<^command_keyword>\<open>bnf_axiomatization\<close> "bnf declaration"
62698
9d706e37ddab tuned whitespace
blanchet
parents: 62691
diff changeset
   126
    (parse_bnf_axiomatization >>
62324
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 61259
diff changeset
   127
      (fn (((((plugins, bsTs), b), witTs), mx), (mapb, relb, predb)) =>
ae44f16dcea5 make predicator a first-class bnf citizen
traytel
parents: 61259
diff changeset
   128
         bnf_axiomatization_cmd plugins bsTs b mx mapb relb predb witTs #> snd));
56942
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   129
5fff4dc31d34 bnf_decl -> bnf_axiomatization
traytel
parents:
diff changeset
   130
end;