src/HOL/Library/bnf_axiomatization.ML
author blanchet
Wed Sep 24 15:45:55 2014 +0200 (2014-09-24)
changeset 58425 246985c6b20b
parent 58189 9d714be4f028
child 58659 6c9821c32dd5
permissions -rw-r--r--
simpler proof
traytel@56942
     1
(*  Title:      HOL/Library/bnf_axiomatization.ML
traytel@56942
     2
    Author:     Dmitriy Traytel, TU Muenchen
traytel@56942
     3
    Copyright   2013
traytel@56942
     4
traytel@56942
     5
Axiomatic declaration of bounded natural functors.
traytel@56942
     6
*)
traytel@56942
     7
traytel@56942
     8
signature BNF_AXIOMATIZATION =
traytel@56942
     9
sig
blanchet@58189
    10
  val bnf_axiomatization: (string -> bool) -> (binding option * (typ * sort)) list -> binding ->
blanchet@58189
    11
    mixfix -> binding -> binding -> typ list -> local_theory -> BNF_Def.bnf * local_theory
traytel@56942
    12
end
traytel@56942
    13
traytel@56942
    14
structure BNF_Axiomatization : BNF_AXIOMATIZATION =
traytel@56942
    15
struct
traytel@56942
    16
traytel@56942
    17
open BNF_Util
traytel@56942
    18
open BNF_Def
traytel@56942
    19
blanchet@58189
    20
fun prepare_decl prepare_constraint prepare_typ plugins raw_vars b mx user_mapb user_relb user_witTs
blanchet@58189
    21
  lthy =
traytel@56942
    22
  let
traytel@56942
    23
   fun prepare_type_arg (set_opt, (ty, c)) =
traytel@56942
    24
      let val s = fst (dest_TFree (prepare_typ lthy ty)) in
traytel@56942
    25
        (set_opt, (s, prepare_constraint lthy c))
traytel@56942
    26
      end;
traytel@56942
    27
    val ((user_setbs, vars), raw_vars') =
traytel@56942
    28
      map prepare_type_arg raw_vars
traytel@56942
    29
      |> `split_list
traytel@56942
    30
      |>> apfst (map_filter I);
traytel@56942
    31
    val deads = map_filter (fn (NONE, x) => SOME x | _ => NONE) raw_vars';
traytel@56942
    32
traytel@56942
    33
    fun mk_b name user_b =
traytel@56942
    34
      (if Binding.is_empty user_b then Binding.prefix_name (name ^ "_") b else user_b)
traytel@56942
    35
      |> Binding.qualify false (Binding.name_of b);
traytel@56942
    36
    val (Tname, lthy) = Typedecl.basic_typedecl (b, length vars, mx) lthy;
traytel@56942
    37
    val (bd_type_Tname, lthy) =
traytel@56942
    38
      Typedecl.basic_typedecl (mk_b "bd_type" Binding.empty, length deads, NoSyn) lthy;
traytel@56942
    39
    val T = Type (Tname, map TFree vars);
traytel@56942
    40
    val bd_type_T = Type (bd_type_Tname, map TFree deads);
traytel@56942
    41
    val lives = map TFree (filter_out (member (op =) deads) vars);
traytel@56942
    42
    val live = length lives;
traytel@56942
    43
    val _ = "Trying to declare a BNF with no live variables" |> null lives ? error;
traytel@56942
    44
    val (lives', _) = BNF_Util.mk_TFrees (length lives)
traytel@56942
    45
      (fold Variable.declare_typ (map TFree vars) lthy);
traytel@56942
    46
    val T' = Term.typ_subst_atomic (lives ~~ lives') T;
traytel@56942
    47
    val mapT = map2 (curry op -->) lives lives' ---> T --> T';
traytel@56942
    48
    val setTs = map (fn U => T --> HOLogic.mk_setT U) lives;
traytel@56942
    49
    val bdT = BNF_Util.mk_relT (bd_type_T, bd_type_T);
blanchet@58189
    50
    val mapb = mk_b mapN user_mapb;
traytel@56942
    51
    val bdb = mk_b "bd" Binding.empty;
blanchet@58189
    52
    val setbs = map2 (fn b => fn i => mk_b (mk_setN i) b) user_setbs
traytel@56942
    53
      (if live = 1 then [0] else 1 upto live);
traytel@56942
    54
traytel@56942
    55
    val witTs = map (prepare_typ lthy) user_witTs;
traytel@56942
    56
    val nwits = length witTs;
blanchet@58189
    57
    val witbs = map (fn i => mk_b (mk_witN i) Binding.empty)
traytel@56942
    58
      (if nwits = 1 then [0] else 1 upto nwits);
traytel@56942
    59
traytel@56942
    60
    val lthy = Local_Theory.background_theory
traytel@56942
    61
      (Sign.add_consts ((mapb, mapT, NoSyn) :: (bdb, bdT, NoSyn) ::
traytel@56942
    62
        map2 (fn b => fn T => (b, T, NoSyn)) setbs setTs @
traytel@56942
    63
        map2 (fn b => fn T => (b, T, NoSyn)) witbs witTs))
traytel@56942
    64
      lthy;
traytel@56942
    65
    val Fmap = Const (Local_Theory.full_name lthy mapb, mapT);
traytel@56942
    66
    val Fsets = map2 (fn setb => fn setT =>
traytel@56942
    67
      Const (Local_Theory.full_name lthy setb, setT)) setbs setTs;
traytel@56942
    68
    val Fbd = Const (Local_Theory.full_name lthy bdb, bdT);
traytel@56942
    69
    val Fwits = map2 (fn witb => fn witT =>
traytel@56942
    70
      Const (Local_Theory.full_name lthy witb, witT)) witbs witTs;
traytel@56942
    71
    val (key, goals, (triv_tac_opt, wit_goalss), after_qed, lthy, _) =
traytel@56942
    72
      prepare_def Do_Inline (user_policy Note_Some) false I (K I) (K I) (SOME (map TFree deads))
traytel@56942
    73
      user_mapb user_relb user_setbs ((((((Binding.empty, T), Fmap), Fsets), Fbd), Fwits), NONE)
traytel@56942
    74
      lthy;
traytel@56942
    75
traytel@56942
    76
    fun mk_wits_tac ctxt set_maps = TRYALL Goal.conjunction_tac THEN the triv_tac_opt ctxt set_maps;
traytel@56942
    77
    val wit_goals = map Logic.mk_conjunction_balanced wit_goalss;
traytel@56942
    78
    val all_goalss = map single goals @ (if nwits > 0 then wit_goalss else []);
traytel@56942
    79
traytel@56942
    80
    val (((_, [raw_thms])), (lthy_old, lthy)) = Local_Theory.background_theory_result
traytel@56942
    81
      (Specification.axiomatization [] [((mk_b "axioms" Binding.empty, []), flat all_goalss)]) lthy
traytel@56942
    82
      ||> `Local_Theory.restore;
traytel@56942
    83
traytel@56942
    84
    fun mk_wit_thms set_maps =
traytel@56942
    85
      Goal.prove_sorry lthy [] [] (Logic.mk_conjunction_balanced wit_goals)
traytel@56942
    86
        (fn {context = ctxt, prems = _} => mk_wits_tac ctxt set_maps)
traytel@56942
    87
      |> Conjunction.elim_balanced (length wit_goals)
traytel@56942
    88
      |> map2 (Conjunction.elim_balanced o length) wit_goalss
traytel@56942
    89
      |> map (map (Thm.close_derivation o Thm.forall_elim_vars 0));
traytel@56942
    90
    val phi = Proof_Context.export_morphism lthy_old lthy;
traytel@56942
    91
    val thms = unflat all_goalss (Morphism.fact phi raw_thms);
traytel@56942
    92
traytel@56942
    93
    val (bnf, lthy') = after_qed mk_wit_thms thms lthy
traytel@56942
    94
  in
blanchet@58189
    95
    (bnf, register_bnf plugins key bnf lthy')
traytel@56942
    96
  end;
traytel@56942
    97
traytel@56942
    98
val bnf_axiomatization = prepare_decl (K I) (K I);
traytel@56942
    99
traytel@56942
   100
fun read_constraint _ NONE = @{sort type}
traytel@56942
   101
  | read_constraint ctxt (SOME s) = Syntax.read_sort ctxt s;
traytel@56942
   102
traytel@56942
   103
val bnf_axiomatization_cmd = prepare_decl read_constraint Syntax.read_typ;
traytel@56942
   104
traytel@56942
   105
val parse_witTs =
traytel@56942
   106
  @{keyword "["} |-- (Parse.short_ident --| @{keyword ":"} -- Scan.repeat Parse.typ
traytel@56942
   107
    >> (fn ("wits", Ts) => Ts
traytel@56942
   108
         | (s, _) => error ("Unknown label " ^ quote s ^ " (expected \"wits\")"))) --|
traytel@56942
   109
  @{keyword "]"} || Scan.succeed [];
traytel@56942
   110
blanchet@58189
   111
val parse_bnf_axiomatization_options =
blanchet@58189
   112
  Scan.optional (@{keyword "("} |-- parse_plugins --| @{keyword ")"}) (K true);
blanchet@58189
   113
traytel@56942
   114
val parse_bnf_axiomatization =
blanchet@58189
   115
  parse_bnf_axiomatization_options -- parse_type_args_named_constrained -- Parse.binding --
blanchet@58189
   116
  parse_witTs -- Parse.opt_mixfix -- parse_map_rel_bindings;
traytel@56942
   117
traytel@56942
   118
val _ =
traytel@56942
   119
  Outer_Syntax.local_theory @{command_spec "bnf_axiomatization"} "bnf declaration"
traytel@56942
   120
    (parse_bnf_axiomatization >> 
blanchet@58189
   121
      (fn (((((plugins, bsTs), b), witTs), mx), (mapb, relb)) =>
blanchet@58189
   122
         bnf_axiomatization_cmd plugins bsTs b mx mapb relb witTs #> snd));
traytel@56942
   123
traytel@56942
   124
end;