src/HOL/Codatatype/Tools/bnf_sugar.ML
author blanchet
Thu Aug 30 09:48:27 2012 +0200 (2012-08-30)
changeset 49025 7e89b0520e83
parent 49023 5afe918dd476
child 49027 fc3b9b49c92d
permissions -rw-r--r--
more work on sugar
blanchet@49017
     1
(*  Title:      HOL/Codatatype/Tools/bnf_sugar.ML
blanchet@49017
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@49017
     3
    Copyright   2012
blanchet@49017
     4
blanchet@49017
     5
Sugar on top of a BNF.
blanchet@49017
     6
*)
blanchet@49017
     7
blanchet@49017
     8
signature BNF_SUGAR =
blanchet@49017
     9
sig
blanchet@49017
    10
end;
blanchet@49017
    11
blanchet@49017
    12
structure BNF_Sugar : BNF_SUGAR =
blanchet@49017
    13
struct
blanchet@49017
    14
blanchet@49017
    15
open BNF_Util
blanchet@49019
    16
open BNF_FP_Util
blanchet@49020
    17
open BNF_Sugar_Tactics
blanchet@49017
    18
blanchet@49025
    19
val case_congN = "case_cong"
blanchet@49025
    20
val case_discsN = "case_discs"
blanchet@49025
    21
val casesN = "cases"
blanchet@49025
    22
val ctr_selsN = "ctr_sels"
blanchet@49025
    23
val disc_disjointN = "disc_disjoint"
blanchet@49025
    24
val distinctN = "distinct"
blanchet@49025
    25
val disc_exhaustN = "disc_exhaust"
blanchet@49025
    26
val selsN = "sels"
blanchet@49025
    27
val splitN = "split"
blanchet@49025
    28
val split_asmN = "split_asm"
blanchet@49025
    29
val weak_case_cong_thmsN = "weak_case_cong"
blanchet@49019
    30
blanchet@49025
    31
fun prepare_sugar prep_term (((raw_ctrs, raw_caseof), disc_names), sel_namess) no_defs_lthy =
blanchet@49017
    32
  let
blanchet@49019
    33
    (* TODO: sanity checks on arguments *)
blanchet@49017
    34
blanchet@49025
    35
    (* TODO: normalize types of constructors w.r.t. each other *)
blanchet@49025
    36
blanchet@49025
    37
    val ctrs0 = map (prep_term no_defs_lthy) raw_ctrs;
blanchet@49025
    38
    val caseof0 = prep_term no_defs_lthy raw_caseof;
blanchet@49017
    39
blanchet@49025
    40
    val n = length ctrs0;
blanchet@49025
    41
    val ks = 1 upto n;
blanchet@49022
    42
blanchet@49025
    43
    val (T_name, As0) = dest_Type (body_type (fastype_of (hd ctrs0)));
blanchet@49020
    44
    val b = Binding.qualified_name T_name;
blanchet@49020
    45
blanchet@49025
    46
    val (As, B) =
blanchet@49025
    47
      no_defs_lthy
blanchet@49025
    48
      |> mk_TFrees (length As0)
blanchet@49025
    49
      ||> the_single o fst o mk_TFrees 1;
blanchet@49025
    50
blanchet@49025
    51
    fun mk_undef T Ts = Const (@{const_name undefined}, Ts ---> T);
blanchet@49025
    52
blanchet@49025
    53
    fun mk_ctr Ts ctr =
blanchet@49025
    54
      let
blanchet@49025
    55
        val Ts0 = snd (dest_Type (body_type (fastype_of ctr)));
blanchet@49025
    56
      in
blanchet@49025
    57
        Term.subst_atomic_types (Ts0 ~~ Ts) ctr
blanchet@49025
    58
      end;
blanchet@49020
    59
blanchet@49022
    60
    fun mk_caseof T =
blanchet@49022
    61
      let
blanchet@49025
    62
        val (binders, body) = strip_type (fastype_of caseof0);
blanchet@49022
    63
      in
blanchet@49025
    64
        Term.subst_atomic_types ((body, T) :: (snd (dest_Type (List.last binders)) ~~ As)) caseof0
blanchet@49022
    65
      end;
blanchet@49022
    66
blanchet@49025
    67
    val T = Type (T_name, As);
blanchet@49025
    68
    val ctrs = map (mk_ctr As) ctrs0;
blanchet@49025
    69
    val ctr_Tss = map (binder_types o fastype_of) ctrs;
blanchet@49025
    70
blanchet@49025
    71
    val caseofB = mk_caseof B;
blanchet@49025
    72
    val caseofB_Ts = map (fn Ts => Ts ---> B) ctr_Tss;
blanchet@49025
    73
blanchet@49025
    74
    val (((((xss, yss), fs), (v, v')), p), _) = no_defs_lthy |>
blanchet@49025
    75
      mk_Freess "x" ctr_Tss
blanchet@49025
    76
      ||>> mk_Freess "y" ctr_Tss
blanchet@49025
    77
      ||>> mk_Frees "f" caseofB_Ts
blanchet@49020
    78
      ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "v") T
blanchet@49020
    79
      ||>> yield_singleton (mk_Frees "P") HOLogic.boolT;
blanchet@49020
    80
blanchet@49025
    81
    val xctrs = map2 (curry Term.list_comb) ctrs xss;
blanchet@49025
    82
    val yctrs = map2 (curry Term.list_comb) ctrs yss;
blanchet@49025
    83
    val eta_fs = map2 (fn f => fn xs => fold_rev Term.lambda xs (Term.list_comb (f, xs))) fs xss;
blanchet@49020
    84
blanchet@49025
    85
    val exist_xs_v_eq_ctrs =
blanchet@49025
    86
      map2 (fn xctr => fn xs => list_exists_free xs (HOLogic.mk_eq (v, xctr))) xctrs xss;
blanchet@49022
    87
blanchet@49025
    88
    fun mk_caseof_args k xs x T =
blanchet@49025
    89
      map2 (fn Ts => fn i => if i = k then fold_rev Term.lambda xs x else mk_undef T Ts) ctr_Tss ks;
blanchet@49025
    90
blanchet@49025
    91
    fun disc_spec b exist_xs_v_eq_ctr =
blanchet@49025
    92
      HOLogic.mk_Trueprop (HOLogic.mk_eq (Free (Binding.name_of b, T --> HOLogic.boolT) $ v,
blanchet@49025
    93
        exist_xs_v_eq_ctr));
blanchet@49025
    94
blanchet@49025
    95
    fun sel_spec b x xs xctr k =
blanchet@49025
    96
      let val T' = fastype_of x in
blanchet@49025
    97
        HOLogic.mk_Trueprop (HOLogic.mk_eq (Free (Binding.name_of b, T --> T') $ v,
blanchet@49025
    98
            Term.list_comb (mk_caseof T', mk_caseof_args k xs x T') $ v))
blanchet@49022
    99
      end;
blanchet@49022
   100
blanchet@49025
   101
    val (((discs0, (_, disc_defs0)), (selss0, (_, sel_defss0))), (lthy', lthy)) =
blanchet@49022
   102
      no_defs_lthy
blanchet@49025
   103
      |> apfst (apsnd split_list o split_list) o fold_map2 (fn b => fn exist_xs_v_eq_ctr =>
blanchet@49022
   104
        Specification.definition (SOME (b, NONE, NoSyn),
blanchet@49025
   105
          ((Thm.def_binding b, []), disc_spec b exist_xs_v_eq_ctr))) disc_names exist_xs_v_eq_ctrs
blanchet@49025
   106
      ||>> apfst (apsnd split_list o split_list) o fold_map4 (fn bs => fn xs => fn xctr => fn k =>
blanchet@49025
   107
        apfst (apsnd split_list o split_list) o fold_map2 (fn b => fn x =>
blanchet@49022
   108
          Specification.definition (SOME (b, NONE, NoSyn),
blanchet@49025
   109
            ((Thm.def_binding b, []), sel_spec b x xs xctr k))) bs xs) sel_namess xss xctrs
blanchet@49025
   110
            ks
blanchet@49022
   111
      ||> `Local_Theory.restore;
blanchet@49022
   112
blanchet@49025
   113
    (*transforms defined frees into consts (and more)*)
blanchet@49025
   114
    val phi = Proof_Context.export_morphism lthy lthy';
blanchet@49025
   115
blanchet@49025
   116
    val disc_defs = map (Morphism.thm phi) disc_defs0;
blanchet@49025
   117
    val sel_defss = map (map (Morphism.thm phi)) sel_defss0;
blanchet@49025
   118
blanchet@49025
   119
    val discs = map (Morphism.term phi) discs0;
blanchet@49025
   120
    val selss = map (map (Morphism.term phi)) selss0;
blanchet@49025
   121
blanchet@49020
   122
    val goal_exhaust =
blanchet@49020
   123
      let
blanchet@49020
   124
        fun mk_imp_p Q = Logic.list_implies (Q, HOLogic.mk_Trueprop p);
blanchet@49025
   125
        fun mk_prem xctr xs =
blanchet@49025
   126
          fold_rev Logic.all xs (mk_imp_p [HOLogic.mk_Trueprop (HOLogic.mk_eq (v, xctr))]);
blanchet@49020
   127
      in
blanchet@49025
   128
        mk_imp_p (map2 mk_prem xctrs xss)
blanchet@49020
   129
      end;
blanchet@49019
   130
blanchet@49019
   131
    val goal_injects =
blanchet@49017
   132
      let
blanchet@49020
   133
        fun mk_goal _ _ [] [] = NONE
blanchet@49025
   134
          | mk_goal xctr yctr xs ys =
blanchet@49019
   135
            SOME (HOLogic.mk_Trueprop (HOLogic.mk_eq
blanchet@49025
   136
              (HOLogic.mk_eq (xctr, yctr),
blanchet@49019
   137
               Library.foldr1 HOLogic.mk_conj (map2 (curry HOLogic.mk_eq) xs ys))));
blanchet@49017
   138
      in
blanchet@49025
   139
        map_filter I (map4 mk_goal xctrs yctrs xss yss)
blanchet@49017
   140
      end;
blanchet@49017
   141
blanchet@49019
   142
    val goal_half_distincts =
blanchet@49019
   143
      let
blanchet@49019
   144
        fun mk_goal t u = HOLogic.mk_Trueprop (HOLogic.mk_not (HOLogic.mk_eq (t, u)));
blanchet@49019
   145
        fun mk_goals [] = []
blanchet@49019
   146
          | mk_goals (t :: ts) = fold_rev (cons o mk_goal t) ts (mk_goals ts);
blanchet@49019
   147
      in
blanchet@49025
   148
        mk_goals xctrs
blanchet@49019
   149
      end;
blanchet@49019
   150
blanchet@49025
   151
    val goal_cases =
blanchet@49025
   152
      let
blanchet@49025
   153
        val lhs0 = Term.list_comb (caseofB, eta_fs);
blanchet@49025
   154
        fun mk_goal k xctr xs f =
blanchet@49025
   155
          HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs0 $ xctr, Term.list_comb (f, xs)))
blanchet@49025
   156
          |> tap (tracing o prefix "HERE: " o PolyML.makestring)(*###*);
blanchet@49025
   157
      in
blanchet@49025
   158
        map4 mk_goal ks xctrs xss fs
blanchet@49025
   159
      end;
blanchet@49025
   160
blanchet@49025
   161
    val goals = [[goal_exhaust], goal_injects, goal_half_distincts, goal_cases];
blanchet@49019
   162
blanchet@49019
   163
    fun after_qed thmss lthy =
blanchet@49019
   164
      let
blanchet@49025
   165
        val [[exhaust_thm], inject_thms, half_distinct_thms, case_thms] = thmss;
blanchet@49019
   166
blanchet@49019
   167
        val other_half_distinct_thms = map (fn thm => thm RS not_sym) half_distinct_thms;
blanchet@49019
   168
blanchet@49020
   169
        val nchotomy_thm =
blanchet@49020
   170
          let
blanchet@49020
   171
            val goal =
blanchet@49022
   172
              HOLogic.mk_Trueprop (HOLogic.mk_all (fst v', snd v',
blanchet@49025
   173
                   Library.foldr1 HOLogic.mk_disj exist_xs_v_eq_ctrs));
blanchet@49020
   174
          in
blanchet@49020
   175
            Skip_Proof.prove lthy [] [] goal (fn _ => mk_nchotomy_tac n exhaust_thm)
blanchet@49020
   176
          end;
blanchet@49020
   177
blanchet@49025
   178
        val sel_thms =
blanchet@49025
   179
          let
blanchet@49025
   180
            fun mk_thm k xs goal_case case_thm x sel sel_def =
blanchet@49025
   181
              let
blanchet@49025
   182
                val T = fastype_of x;
blanchet@49025
   183
                val cTs =
blanchet@49025
   184
                  map ((fn T' => certifyT lthy (if T' = B then T else T')) o TFree)
blanchet@49025
   185
                    (rev (Term.add_tfrees goal_case []));
blanchet@49025
   186
                val cxs = map (certify lthy) (mk_caseof_args k xs x T);
blanchet@49025
   187
              in
blanchet@49025
   188
                Local_Defs.fold lthy [sel_def]
blanchet@49025
   189
                  (Drule.instantiate' (map SOME cTs) (map SOME cxs) case_thm)
blanchet@49025
   190
              end;
blanchet@49025
   191
            fun mk_thms k xs goal_case case_thm sels sel_defs =
blanchet@49025
   192
              map3 (mk_thm k xs goal_case case_thm) xs sels sel_defs;
blanchet@49025
   193
          in
blanchet@49025
   194
            flat (map6 mk_thms ks xss goal_cases case_thms selss sel_defss)
blanchet@49025
   195
          end;
blanchet@49025
   196
blanchet@49025
   197
        val disc_thms = [];
blanchet@49025
   198
blanchet@49025
   199
        val disc_disjoint_thms = [];
blanchet@49025
   200
blanchet@49025
   201
        val disc_exhaust_thms = [];
blanchet@49025
   202
blanchet@49025
   203
        val ctr_sel_thms = [];
blanchet@49025
   204
blanchet@49025
   205
        val case_disc_thms = [];
blanchet@49025
   206
blanchet@49025
   207
        val case_cong_thm = TrueI;
blanchet@49025
   208
blanchet@49025
   209
        val weak_case_cong_thms = TrueI;
blanchet@49025
   210
blanchet@49025
   211
        val split_thms = [];
blanchet@49025
   212
blanchet@49025
   213
        val split_asm_thms = [];
blanchet@49025
   214
blanchet@49025
   215
        (* case syntax *)
blanchet@49025
   216
blanchet@49019
   217
        fun note thmN thms =
blanchet@49019
   218
          snd o Local_Theory.note
blanchet@49019
   219
            ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), thms);
blanchet@49019
   220
      in
blanchet@49019
   221
        lthy
blanchet@49025
   222
        |> note case_congN [case_cong_thm]
blanchet@49025
   223
        |> note case_discsN case_disc_thms
blanchet@49025
   224
        |> note casesN case_thms
blanchet@49025
   225
        |> note ctr_selsN ctr_sel_thms
blanchet@49025
   226
        |> note disc_disjointN disc_disjoint_thms
blanchet@49025
   227
        |> note disc_exhaustN disc_exhaust_thms
blanchet@49020
   228
        |> note distinctN (half_distinct_thms @ other_half_distinct_thms)
blanchet@49020
   229
        |> note exhaustN [exhaust_thm]
blanchet@49019
   230
        |> note injectN inject_thms
blanchet@49020
   231
        |> note nchotomyN [nchotomy_thm]
blanchet@49025
   232
        |> note selsN sel_thms
blanchet@49025
   233
        |> note splitN split_thms
blanchet@49025
   234
        |> note split_asmN split_asm_thms
blanchet@49025
   235
        |> note weak_case_cong_thmsN [weak_case_cong_thms]
blanchet@49019
   236
      end;
blanchet@49017
   237
  in
blanchet@49025
   238
    (goals, after_qed, lthy')
blanchet@49017
   239
  end;
blanchet@49017
   240
blanchet@49017
   241
val parse_binding_list = Parse.$$$ "[" |--  Parse.list Parse.binding --| Parse.$$$ "]";
blanchet@49017
   242
blanchet@49019
   243
val bnf_sugar_cmd = (fn (goalss, after_qed, lthy) =>
blanchet@49019
   244
  Proof.theorem NONE after_qed (map (map (rpair [])) goalss) lthy) oo
blanchet@49020
   245
  prepare_sugar Syntax.read_term;
blanchet@49017
   246
blanchet@49017
   247
val _ =
blanchet@49017
   248
  Outer_Syntax.local_theory_to_proof @{command_spec "bnf_sugar"} "adds sugar on top of a BNF"
blanchet@49023
   249
    (((Parse.$$$ "[" |-- Parse.list Parse.term --| Parse.$$$ "]") -- Parse.term --
blanchet@49023
   250
      parse_binding_list -- (Parse.$$$ "[" |-- Parse.list parse_binding_list --| Parse.$$$ "]"))
blanchet@49023
   251
      >> bnf_sugar_cmd);
blanchet@49017
   252
blanchet@49017
   253
end;