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