src/HOL/add_ind_def.ML
author wenzelm
Thu Jan 23 14:19:16 1997 +0100 (1997-01-23)
changeset 2545 d10abc8c11fb
parent 2270 d7513875b2b8
child 2859 7d640451ae7d
permissions -rw-r--r--
added AxClasses test;
clasohm@1465
     1
(*  Title:      HOL/add_ind_def.ML
clasohm@923
     2
    ID:         $Id$
clasohm@1465
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@923
     4
    Copyright   1994  University of Cambridge
clasohm@923
     5
clasohm@923
     6
Fixedpoint definition module -- for Inductive/Coinductive Definitions
clasohm@923
     7
clasohm@923
     8
Features:
clasohm@923
     9
* least or greatest fixedpoints
clasohm@923
    10
* user-specified product and sum constructions
clasohm@923
    11
* mutually recursive definitions
clasohm@923
    12
* definitions involving arbitrary monotone operators
clasohm@923
    13
* automatically proves introduction and elimination rules
clasohm@923
    14
clasohm@923
    15
The recursive sets must *already* be declared as constants in parent theory!
clasohm@923
    16
clasohm@923
    17
  Introduction rules have the form
clasohm@923
    18
  [| ti:M(Sj), ..., P(x), ... |] ==> t: Sk |]
clasohm@923
    19
  where M is some monotone operator (usually the identity)
clasohm@923
    20
  P(x) is any (non-conjunctive) side condition on the free variables
clasohm@923
    21
  ti, t are any terms
clasohm@923
    22
  Sj, Sk are two of the sets being defined in mutual recursion
clasohm@923
    23
clasohm@923
    24
Sums are used only for mutual recursion;
clasohm@923
    25
Products are used only to derive "streamlined" induction rules for relations
clasohm@923
    26
clasohm@923
    27
Nestings of disjoint sum types:
clasohm@923
    28
   (a+(b+c)) for 3,  ((a+b)+(c+d)) for 4,  ((a+b)+(c+(d+e))) for 5,
clasohm@923
    29
   ((a+(b+c))+(d+(e+f))) for 6
clasohm@923
    30
*)
clasohm@923
    31
clasohm@1465
    32
signature FP =          (** Description of a fixed point operator **)
clasohm@923
    33
  sig
clasohm@1465
    34
  val oper      : string * typ * term -> term   (*fixed point operator*)
clasohm@1465
    35
  val Tarski    : thm                   (*Tarski's fixed point theorem*)
clasohm@1465
    36
  val induct    : thm                   (*induction/coinduction rule*)
clasohm@923
    37
  end;
clasohm@923
    38
clasohm@923
    39
clasohm@923
    40
signature ADD_INDUCTIVE_DEF =
clasohm@923
    41
  sig 
clasohm@923
    42
  val add_fp_def_i : term list * term list -> theory -> theory
clasohm@923
    43
  end;
clasohm@923
    44
clasohm@923
    45
clasohm@923
    46
clasohm@923
    47
(*Declares functions to add fixedpoint/constructor defs to a theory*)
clasohm@923
    48
functor Add_inductive_def_Fun (Fp: FP) : ADD_INDUCTIVE_DEF =
clasohm@923
    49
struct
paulson@1397
    50
open Ind_Syntax;
clasohm@923
    51
clasohm@923
    52
(*internal version*)
clasohm@923
    53
fun add_fp_def_i (rec_tms, intr_tms) thy = 
clasohm@923
    54
  let
clasohm@923
    55
    val sign = sign_of thy;
clasohm@923
    56
lcp@1189
    57
    (*rec_params should agree for all mutually recursive components*)
clasohm@923
    58
    val rec_hds = map head_of rec_tms;
clasohm@923
    59
clasohm@923
    60
    val _ = assert_all is_Const rec_hds
clasohm@1465
    61
            (fn t => "Recursive set not previously declared as constant: " ^ 
clasohm@1465
    62
                     Sign.string_of_term sign t);
clasohm@923
    63
clasohm@923
    64
    (*Now we know they are all Consts, so get their names, type and params*)
clasohm@923
    65
    val rec_names = map (#1 o dest_Const) rec_hds
clasohm@923
    66
    and (Const(_,recT),rec_params) = strip_comb (hd rec_tms);
clasohm@923
    67
clasohm@923
    68
    val _ = assert_all Syntax.is_identifier rec_names
clasohm@923
    69
       (fn a => "Name of recursive set not an identifier: " ^ a);
clasohm@923
    70
clasohm@923
    71
    local (*Checking the introduction rules*)
clasohm@923
    72
      val intr_sets = map (#2 o rule_concl_msg sign) intr_tms;
clasohm@923
    73
      fun intr_ok set =
clasohm@1465
    74
          case head_of set of Const(a,_) => a mem rec_names | _ => false;
clasohm@923
    75
    in
clasohm@923
    76
      val _ =  assert_all intr_ok intr_sets
clasohm@1465
    77
         (fn t => "Conclusion of rule does not name a recursive set: " ^ 
clasohm@1465
    78
                  Sign.string_of_term sign t);
clasohm@923
    79
    end;
clasohm@923
    80
clasohm@923
    81
    val _ = assert_all is_Free rec_params
clasohm@1465
    82
        (fn t => "Param in recursion term not a free variable: " ^
clasohm@1465
    83
                 Sign.string_of_term sign t);
clasohm@923
    84
clasohm@923
    85
    (*** Construct the lfp definition ***)
clasohm@923
    86
    val mk_variant = variant (foldr add_term_names (intr_tms,[]));
clasohm@923
    87
clasohm@923
    88
    val z = mk_variant"z" and X = mk_variant"X" and w = mk_variant"w";
clasohm@923
    89
lcp@1189
    90
    (*Mutual recursion ?? *)
lcp@1189
    91
    val domTs = summands (dest_setT (body_type recT));
clasohm@1465
    92
                (*alternative defn: map (dest_setT o fastype_of) rec_tms *)
clasohm@923
    93
    val dom_sumT = fold_bal mk_sum domTs;
paulson@1397
    94
    val dom_set  = mk_setT dom_sumT;
clasohm@923
    95
clasohm@923
    96
    val freez   = Free(z, dom_sumT)
clasohm@923
    97
    and freeX   = Free(X, dom_set);
clasohm@923
    98
    (*type of w may be any of the domTs*)
clasohm@923
    99
clasohm@923
   100
    fun dest_tprop (Const("Trueprop",_) $ P) = P
clasohm@923
   101
      | dest_tprop Q = error ("Ill-formed premise of introduction rule: " ^ 
clasohm@1465
   102
                              Sign.string_of_term sign Q);
clasohm@923
   103
clasohm@923
   104
    (*Makes a disjunct from an introduction rule*)
clasohm@923
   105
    fun lfp_part intr = (*quantify over rule's free vars except parameters*)
paulson@1397
   106
      let val prems = map dest_tprop (Logic.strip_imp_prems intr)
clasohm@1465
   107
          val _ = seq (fn rec_hd => seq (chk_prem rec_hd) prems) rec_hds
clasohm@1465
   108
          val exfrees = term_frees intr \\ rec_params
clasohm@1465
   109
          val zeq = eq_const dom_sumT $ freez $ (#1 (rule_concl intr))
clasohm@923
   110
      in foldr mk_exists (exfrees, fold_bal (app conj) (zeq::prems)) end;
clasohm@923
   111
clasohm@923
   112
    (*The Part(A,h) terms -- compose injections to make h*)
clasohm@1465
   113
    fun mk_Part (Bound 0, _) = freeX    (*no mutual rec, no Part needed*)
clasohm@923
   114
      | mk_Part (h, domT)    = 
clasohm@1465
   115
          let val goodh = mend_sum_types (h, dom_sumT)
clasohm@923
   116
              and Part_const = 
clasohm@1465
   117
                  Const("Part", [dom_set, domT-->dom_sumT]---> dom_set)
clasohm@923
   118
          in  Part_const $ freeX $ Abs(w,domT,goodh)  end;
clasohm@923
   119
clasohm@923
   120
    (*Access to balanced disjoint sums via injections*)
paulson@2270
   121
    val parts = ListPair.map mk_Part
paulson@2270
   122
                (accesses_bal (ap Inl, ap Inr, Bound 0) (length domTs),
clasohm@1465
   123
                 domTs);
clasohm@923
   124
clasohm@923
   125
    (*replace each set by the corresponding Part(A,h)*)
clasohm@923
   126
    val part_intrs = map (subst_free (rec_tms ~~ parts) o lfp_part) intr_tms;
clasohm@923
   127
clasohm@923
   128
    val lfp_rhs = Fp.oper(X, dom_sumT, 
clasohm@1465
   129
                          mk_Collect(z, dom_sumT, 
clasohm@1465
   130
                                     fold_bal (app disj) part_intrs))
clasohm@923
   131
clasohm@923
   132
clasohm@923
   133
    (*** Make the new theory ***)
clasohm@923
   134
clasohm@923
   135
    (*A key definition:
clasohm@923
   136
      If no mutual recursion then it equals the one recursive set.
clasohm@923
   137
      If mutual recursion then it differs from all the recursive sets. *)
clasohm@923
   138
    val big_rec_name = space_implode "_" rec_names;
clasohm@923
   139
clasohm@923
   140
    (*Big_rec... is the union of the mutually recursive sets*)
clasohm@923
   141
    val big_rec_tm = list_comb(Const(big_rec_name,recT), rec_params);
clasohm@923
   142
clasohm@923
   143
    (*The individual sets must already be declared*)
clasohm@923
   144
    val axpairs = map mk_defpair 
clasohm@1465
   145
          ((big_rec_tm, lfp_rhs) ::
clasohm@1465
   146
           (case parts of 
clasohm@1465
   147
               [_] => []                        (*no mutual recursion*)
clasohm@1465
   148
             | _ => rec_tms ~~          (*define the sets as Parts*)
clasohm@1465
   149
                    map (subst_atomic [(freeX, big_rec_tm)]) parts));
clasohm@923
   150
clasohm@923
   151
    val _ = seq (writeln o Sign.string_of_term sign o #2) axpairs
clasohm@923
   152
  
paulson@1397
   153
    (*Detect occurrences of operator, even with other types!*)
paulson@1397
   154
    val _ = (case rec_names inter (add_term_names (lfp_rhs,[])) of
clasohm@1465
   155
               [] => ()
clasohm@1465
   156
             | x::_ => error ("Illegal occurrence of recursion op: " ^ x ^
paulson@1397
   157
                               "\n\t*Consider adding type constraints*"))
paulson@1397
   158
clasohm@923
   159
  in  thy |> add_defs_i axpairs  end
clasohm@923
   160
clasohm@923
   161
clasohm@923
   162
(****************************************************************OMITTED
clasohm@923
   163
clasohm@923
   164
(*Expects the recursive sets to have been defined already.
clasohm@923
   165
  con_ty_lists specifies the constructors in the form (name,prems,mixfix) *)
clasohm@923
   166
fun add_constructs_def (rec_names, con_ty_lists) thy = 
clasohm@923
   167
* let
clasohm@923
   168
*   val _ = writeln"  Defining the constructor functions...";
clasohm@1465
   169
*   val case_name = "f";                (*name for case variables*)
clasohm@923
   170
clasohm@923
   171
*   (** Define the constructors **)
clasohm@923
   172
clasohm@923
   173
*   (*The empty tuple is 0*)
clasohm@923
   174
*   fun mk_tuple [] = Const("0",iT)
clasohm@923
   175
*     | mk_tuple args = foldr1 mk_Pair args;
clasohm@923
   176
clasohm@923
   177
*   fun mk_inject n k u = access_bal(ap Inl, ap Inr, u) n k;
clasohm@923
   178
clasohm@1465
   179
*   val npart = length rec_names;       (*total # of mutually recursive parts*)
clasohm@923
   180
clasohm@923
   181
*   (*Make constructor definition; kpart is # of this mutually recursive part*)
clasohm@923
   182
*   fun mk_con_defs (kpart, con_ty_list) = 
clasohm@1465
   183
*     let val ncon = length con_ty_list    (*number of constructors*)
clasohm@1465
   184
          fun mk_def (((id,T,syn), name, args, prems), kcon) =
clasohm@1465
   185
                (*kcon is index of constructor*)
clasohm@1465
   186
              mk_defpair (list_comb (Const(name,T), args),
clasohm@1465
   187
                          mk_inject npart kpart
clasohm@1465
   188
                          (mk_inject ncon kcon (mk_tuple args)))
paulson@2270
   189
*     in  ListPair.map mk_def (con_ty_list, (1 upto ncon))  end;
clasohm@923
   190
clasohm@923
   191
*   (** Define the case operator **)
clasohm@923
   192
clasohm@923
   193
*   (*Combine split terms using case; yields the case operator for one part*)
clasohm@923
   194
*   fun call_case case_list = 
clasohm@923
   195
*     let fun call_f (free,args) = 
clasohm@1465
   196
              ap_split T free (map (#2 o dest_Free) args)
clasohm@923
   197
*     in  fold_bal (app sum_case) (map call_f case_list)  end;
clasohm@923
   198
clasohm@923
   199
*   (** Generating function variables for the case definition
clasohm@1465
   200
        Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **)
clasohm@923
   201
clasohm@923
   202
*   (*Treatment of a single constructor*)
clasohm@923
   203
*   fun add_case (((id,T,syn), name, args, prems), (opno,cases)) =
clasohm@1465
   204
        if Syntax.is_identifier id
clasohm@1465
   205
        then (opno,   
clasohm@1465
   206
              (Free(case_name ^ "_" ^ id, T), args) :: cases)
clasohm@1465
   207
        else (opno+1, 
clasohm@1465
   208
              (Free(case_name ^ "_op_" ^ string_of_int opno, T), args) :: 
clasohm@1465
   209
              cases)
clasohm@923
   210
clasohm@923
   211
*   (*Treatment of a list of constructors, for one part*)
clasohm@923
   212
*   fun add_case_list (con_ty_list, (opno,case_lists)) =
clasohm@1465
   213
        let val (opno',case_list) = foldr add_case (con_ty_list, (opno,[]))
clasohm@1465
   214
        in (opno', case_list :: case_lists) end;
clasohm@923
   215
clasohm@923
   216
*   (*Treatment of all parts*)
clasohm@923
   217
*   val (_, case_lists) = foldr add_case_list (con_ty_lists, (1,[]));
clasohm@923
   218
clasohm@923
   219
*   val big_case_typ = flat (map (map (#2 o #1)) con_ty_lists) ---> (iT-->iT);
clasohm@923
   220
clasohm@923
   221
*   val big_rec_name = space_implode "_" rec_names;
clasohm@923
   222
clasohm@923
   223
*   val big_case_name = big_rec_name ^ "_case";
clasohm@923
   224
clasohm@923
   225
*   (*The list of all the function variables*)
clasohm@923
   226
*   val big_case_args = flat (map (map #1) case_lists);
clasohm@923
   227
clasohm@923
   228
*   val big_case_tm = 
clasohm@1465
   229
        list_comb (Const(big_case_name, big_case_typ), big_case_args); 
clasohm@923
   230
clasohm@923
   231
*   val big_case_def = mk_defpair  
clasohm@1465
   232
        (big_case_tm, fold_bal (app sum_case) (map call_case case_lists)); 
clasohm@923
   233
clasohm@923
   234
*   (** Build the new theory **)
clasohm@923
   235
clasohm@923
   236
*   val const_decs =
clasohm@1465
   237
        (big_case_name, big_case_typ, NoSyn) :: map #1 (flat con_ty_lists);
clasohm@923
   238
clasohm@923
   239
*   val axpairs =
paulson@2270
   240
        big_case_def :: flat (ListPair.map mk_con_defs ((1 upto npart), con_ty_lists))
clasohm@923
   241
clasohm@923
   242
*   in  thy |> add_consts_i const_decs |> add_defs_i axpairs  end;
clasohm@923
   243
****************************************************************)
clasohm@923
   244
end;
clasohm@923
   245
clasohm@923
   246
clasohm@923
   247
clasohm@923
   248