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