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