src/HOL/Codatatype/Tools/bnf_sugar.ML
author blanchet
Thu Aug 30 09:47:46 2012 +0200 (2012-08-30)
changeset 49022 005ce926a932
parent 49020 f379cf5d71bd
child 49023 5afe918dd476
permissions -rw-r--r--
define selectors and discriminators
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@49019
    19
val distinctN = "distinct";
blanchet@49019
    20
blanchet@49022
    21
fun prepare_sugar prep_term (((raw_ctors, dtor_names), stor_namess), raw_caseof) no_defs_lthy =
blanchet@49017
    22
  let
blanchet@49019
    23
    (* TODO: sanity checks on arguments *)
blanchet@49017
    24
blanchet@49022
    25
    val ctors = map (prep_term no_defs_lthy) raw_ctors;
blanchet@49017
    26
    val ctor_Tss = map (binder_types o fastype_of) ctors;
blanchet@49017
    27
blanchet@49022
    28
    val caseof = prep_term no_defs_lthy raw_caseof;
blanchet@49022
    29
blanchet@49022
    30
    val T as Type (T_name, As) = body_type (fastype_of (hd ctors));
blanchet@49020
    31
    val b = Binding.qualified_name T_name;
blanchet@49020
    32
blanchet@49020
    33
    val n = length ctors;
blanchet@49022
    34
    val ks = 1 upto n;
blanchet@49020
    35
blanchet@49022
    36
    fun mk_caseof T =
blanchet@49022
    37
      let
blanchet@49022
    38
        val (binders, body) = strip_type (fastype_of caseof);
blanchet@49022
    39
      in
blanchet@49022
    40
        Term.subst_atomic_types ((body, T) :: (snd (dest_Type (List.last binders)) ~~ As)) caseof
blanchet@49022
    41
      end;
blanchet@49022
    42
blanchet@49022
    43
    val ((((xss, yss), (v, v')), p), no_defs_lthy') = no_defs_lthy |>
blanchet@49019
    44
      mk_Freess "x" ctor_Tss
blanchet@49020
    45
      ||>> mk_Freess "y" ctor_Tss
blanchet@49020
    46
      ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "v") T
blanchet@49020
    47
      ||>> yield_singleton (mk_Frees "P") HOLogic.boolT;
blanchet@49020
    48
blanchet@49020
    49
    val xs_ctors = map2 (curry Term.list_comb) ctors xss;
blanchet@49020
    50
    val ys_ctors = map2 (curry Term.list_comb) ctors yss;
blanchet@49020
    51
blanchet@49022
    52
    val exist_xs_v_eq_ctors =
blanchet@49022
    53
      map2 (fn xs_ctor => fn xs => list_exists_free xs (HOLogic.mk_eq (v, xs_ctor))) xs_ctors xss;
blanchet@49022
    54
blanchet@49022
    55
    fun dtor_spec b exist_xs_v_eq_ctor =
blanchet@49022
    56
      HOLogic.mk_Trueprop
blanchet@49022
    57
        (HOLogic.mk_eq (Free (Binding.name_of b, T --> HOLogic.boolT) $ v, exist_xs_v_eq_ctor));
blanchet@49022
    58
blanchet@49022
    59
    fun stor_spec b x xs xs_ctor k =
blanchet@49022
    60
      let
blanchet@49022
    61
        val T' = fastype_of x;
blanchet@49022
    62
      in
blanchet@49022
    63
        HOLogic.mk_Trueprop
blanchet@49022
    64
          (HOLogic.mk_eq (Free (Binding.name_of b, T --> T') $ v,
blanchet@49022
    65
            Term.list_comb (mk_caseof T', map2 (fn Ts => fn i =>
blanchet@49022
    66
              if i = k then fold_rev lambda xs x else Const (@{const_name undefined}, Ts ---> T'))
blanchet@49022
    67
              ctor_Tss ks) $ v))
blanchet@49022
    68
      end;
blanchet@49022
    69
blanchet@49022
    70
    val ((dtor_defs, stor_defss), (lthy', lthy)) =
blanchet@49022
    71
      no_defs_lthy
blanchet@49022
    72
      |> fold_map2 (fn b => fn exist_xs_v_eq_ctor =>
blanchet@49022
    73
        Specification.definition (SOME (b, NONE, NoSyn),
blanchet@49022
    74
          ((Thm.def_binding b, []), dtor_spec b exist_xs_v_eq_ctor))) dtor_names exist_xs_v_eq_ctors
blanchet@49022
    75
      ||>> fold_map4 (fn bs => fn xs => fn xs_ctor => fn k =>
blanchet@49022
    76
        fold_map2 (fn b => fn x =>
blanchet@49022
    77
          Specification.definition (SOME (b, NONE, NoSyn),
blanchet@49022
    78
            ((Thm.def_binding b, []), stor_spec b x xs xs_ctor k))) bs xs) stor_namess xss xs_ctors
blanchet@49022
    79
          ks
blanchet@49022
    80
      ||> `Local_Theory.restore;
blanchet@49022
    81
blanchet@49020
    82
    val goal_exhaust =
blanchet@49020
    83
      let
blanchet@49020
    84
        fun mk_imp_p Q = Logic.list_implies (Q, HOLogic.mk_Trueprop p);
blanchet@49020
    85
        fun mk_prem xs_ctor xs =
blanchet@49022
    86
          fold_rev Logic.all xs (mk_imp_p [HOLogic.mk_Trueprop (HOLogic.mk_eq (v, xs_ctor))]);
blanchet@49020
    87
      in
blanchet@49020
    88
        mk_imp_p (map2 mk_prem xs_ctors xss)
blanchet@49020
    89
      end;
blanchet@49019
    90
blanchet@49019
    91
    val goal_injects =
blanchet@49017
    92
      let
blanchet@49020
    93
        fun mk_goal _ _ [] [] = NONE
blanchet@49020
    94
          | mk_goal xs_ctor ys_ctor xs ys =
blanchet@49019
    95
            SOME (HOLogic.mk_Trueprop (HOLogic.mk_eq
blanchet@49020
    96
              (HOLogic.mk_eq (xs_ctor, ys_ctor),
blanchet@49019
    97
               Library.foldr1 HOLogic.mk_conj (map2 (curry HOLogic.mk_eq) xs ys))));
blanchet@49017
    98
      in
blanchet@49020
    99
        map_filter I (map4 mk_goal xs_ctors ys_ctors xss yss)
blanchet@49017
   100
      end;
blanchet@49017
   101
blanchet@49019
   102
    val goal_half_distincts =
blanchet@49019
   103
      let
blanchet@49019
   104
        fun mk_goal t u = HOLogic.mk_Trueprop (HOLogic.mk_not (HOLogic.mk_eq (t, u)));
blanchet@49019
   105
        fun mk_goals [] = []
blanchet@49019
   106
          | mk_goals (t :: ts) = fold_rev (cons o mk_goal t) ts (mk_goals ts);
blanchet@49019
   107
      in
blanchet@49020
   108
        mk_goals xs_ctors
blanchet@49019
   109
      end;
blanchet@49019
   110
blanchet@49020
   111
    val goals = [[goal_exhaust], goal_injects, goal_half_distincts];
blanchet@49019
   112
blanchet@49019
   113
    fun after_qed thmss lthy =
blanchet@49019
   114
      let
blanchet@49020
   115
        val [[exhaust_thm], inject_thms, half_distinct_thms] = thmss;
blanchet@49019
   116
blanchet@49019
   117
        val other_half_distinct_thms = map (fn thm => thm RS not_sym) half_distinct_thms;
blanchet@49019
   118
blanchet@49020
   119
        val nchotomy_thm =
blanchet@49020
   120
          let
blanchet@49020
   121
            val goal =
blanchet@49022
   122
              HOLogic.mk_Trueprop (HOLogic.mk_all (fst v', snd v',
blanchet@49022
   123
                   Library.foldr1 HOLogic.mk_disj exist_xs_v_eq_ctors));
blanchet@49020
   124
          in
blanchet@49020
   125
            Skip_Proof.prove lthy [] [] goal (fn _ => mk_nchotomy_tac n exhaust_thm)
blanchet@49020
   126
          end;
blanchet@49020
   127
blanchet@49019
   128
        fun note thmN thms =
blanchet@49019
   129
          snd o Local_Theory.note
blanchet@49019
   130
            ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), thms);
blanchet@49019
   131
      in
blanchet@49019
   132
        lthy
blanchet@49020
   133
        |> note distinctN (half_distinct_thms @ other_half_distinct_thms)
blanchet@49020
   134
        |> note exhaustN [exhaust_thm]
blanchet@49019
   135
        |> note injectN inject_thms
blanchet@49020
   136
        |> note nchotomyN [nchotomy_thm]
blanchet@49019
   137
      end;
blanchet@49017
   138
  in
blanchet@49019
   139
    (goals, after_qed, lthy)
blanchet@49017
   140
  end;
blanchet@49017
   141
blanchet@49017
   142
val parse_binding_list = Parse.$$$ "[" |--  Parse.list Parse.binding --| Parse.$$$ "]";
blanchet@49017
   143
blanchet@49019
   144
val bnf_sugar_cmd = (fn (goalss, after_qed, lthy) =>
blanchet@49019
   145
  Proof.theorem NONE after_qed (map (map (rpair [])) goalss) lthy) oo
blanchet@49020
   146
  prepare_sugar Syntax.read_term;
blanchet@49017
   147
blanchet@49017
   148
val _ =
blanchet@49017
   149
  Outer_Syntax.local_theory_to_proof @{command_spec "bnf_sugar"} "adds sugar on top of a BNF"
blanchet@49020
   150
    (((Parse.$$$ "[" |-- Parse.list Parse.term --| Parse.$$$ "]") --
blanchet@49017
   151
      parse_binding_list -- (Parse.$$$ "[" |-- Parse.list parse_binding_list --| Parse.$$$ "]") --
blanchet@49017
   152
      Parse.term) >> bnf_sugar_cmd);
blanchet@49017
   153
blanchet@49017
   154
end;