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