src/ZF/Tools/cartprod.ML
author wenzelm
Mon, 08 Aug 2011 17:23:15 +0200
changeset 44058 ae85c5d64913
parent 43333 2bdec7f430d3
child 54742 7a86358a3c0b
permissions -rw-r--r--
misc tuning -- eliminated old-fashioned rep_thm;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24584
01e83ffa6c54 fixed title
haftmann
parents: 22596
diff changeset
     1
(*  Title:      ZF/Tools/cartprod.ML
6049
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
     3
    Copyright   1996  University of Cambridge
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
     4
35762
af3ff2ba4c54 removed old CVS Ids;
wenzelm
parents: 33957
diff changeset
     5
Signatures for inductive definitions.
6049
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
     6
35762
af3ff2ba4c54 removed old CVS Ids;
wenzelm
parents: 33957
diff changeset
     7
Syntactic operations for Cartesian Products.
6049
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
     8
*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
     9
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    10
signature FP =          (** Description of a fixed point operator **)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    11
  sig
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    12
  val oper      : term                  (*fixed point operator*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    13
  val bnd_mono  : term                  (*monotonicity predicate*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    14
  val bnd_monoI : thm                   (*intro rule for bnd_mono*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    15
  val subs      : thm                   (*subset theorem for fp*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    16
  val Tarski    : thm                   (*Tarski's fixed point theorem*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    17
  val induct    : thm                   (*induction/coinduction rule*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    18
  end;
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    19
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    20
signature SU =                  (** Description of a disjoint sum **)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    21
  sig
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    22
  val sum       : term                  (*disjoint sum operator*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    23
  val inl       : term                  (*left injection*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    24
  val inr       : term                  (*right injection*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    25
  val elim      : term                  (*case operator*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    26
  val case_inl  : thm                   (*inl equality rule for case*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    27
  val case_inr  : thm                   (*inr equality rule for case*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    28
  val inl_iff   : thm                   (*injectivity of inl, using <->*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    29
  val inr_iff   : thm                   (*injectivity of inr, using <->*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    30
  val distinct  : thm                   (*distinctness of inl, inr using <->*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    31
  val distinct' : thm                   (*distinctness of inr, inl using <->*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    32
  val free_SEs  : thm list              (*elim rules for SU, and pair_iff!*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    33
  end;
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    34
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    35
signature PR =                  (** Description of a Cartesian product **)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    36
  sig
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    37
  val sigma     : term                  (*Cartesian product operator*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    38
  val pair      : term                  (*pairing operator*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    39
  val split_name : string               (*name of polymorphic split*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    40
  val pair_iff  : thm                   (*injectivity of pairing, using <->*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    41
  val split_eq  : thm                   (*equality rule for split*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    42
  val fsplitI   : thm                   (*intro rule for fsplit*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    43
  val fsplitD   : thm                   (*destruct rule for fsplit*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    44
  val fsplitE   : thm                   (*elim rule; apparently never used*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    45
  end;
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    46
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    47
signature CARTPROD =            (** Derived syntactic functions for produts **)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    48
  sig
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    49
  val ap_split : typ -> typ -> term -> term
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    50
  val factors : typ -> typ list
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    51
  val mk_prod : typ * typ -> typ
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    52
  val mk_tuple : term -> typ -> term list -> term
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    53
  val pseudo_type : term -> typ
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    54
  val remove_split : thm -> thm
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    55
  val split_const : typ -> term
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    56
  val split_rule_var : term * typ * thm -> thm
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    57
  end;
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    58
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    59
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    60
functor CartProd_Fun (Pr: PR) : CARTPROD =
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    61
struct
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    62
15674
4a1d07bb53e2 reverted renaming of Some/None in comments and strings;
wenzelm
parents: 15570
diff changeset
    63
(* Some of these functions expect "pseudo-types" containing products,
6049
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    64
   as in HOL; the true ZF types would just be "i" *)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    65
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    66
fun mk_prod (T1,T2) = Type("*", [T1,T2]);
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    67
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    68
(*Bogus product type underlying a (possibly nested) Sigma.  
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    69
  Lets us share HOL code*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    70
fun pseudo_type (t $ A $ Abs(_,_,B)) = 
26189
9808cca5c54d misc cleanup of embedded ML code;
wenzelm
parents: 24584
diff changeset
    71
      if t = Pr.sigma
9808cca5c54d misc cleanup of embedded ML code;
wenzelm
parents: 24584
diff changeset
    72
      then mk_prod(pseudo_type A, pseudo_type B)
9808cca5c54d misc cleanup of embedded ML code;
wenzelm
parents: 24584
diff changeset
    73
      else @{typ i}
9808cca5c54d misc cleanup of embedded ML code;
wenzelm
parents: 24584
diff changeset
    74
  | pseudo_type _ = @{typ i};
6049
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    75
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    76
(*Maps the type T1*...*Tn to [T1,...,Tn], however nested*)
26189
9808cca5c54d misc cleanup of embedded ML code;
wenzelm
parents: 24584
diff changeset
    77
fun factors (Type("*", [T1, T2])) = factors T1 @ factors T2
9808cca5c54d misc cleanup of embedded ML code;
wenzelm
parents: 24584
diff changeset
    78
  | factors T                     = [T];
6049
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    79
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    80
(*Make a well-typed instance of "split"*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    81
fun split_const T = Const(Pr.split_name, 
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    82
                          [[Ind_Syntax.iT, Ind_Syntax.iT]--->T, 
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    83
                           Ind_Syntax.iT] ---> T);
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    84
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    85
(*In ap_split S T u, term u expects separate arguments for the factors of S,
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    86
  with result type T.  The call creates a new term expecting one argument
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    87
  of type S.*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    88
fun ap_split (Type("*", [T1,T2])) T3 u   = 
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    89
       split_const T3 $ 
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    90
       Abs("v", Ind_Syntax.iT, (*Not T1, as it involves pseudo-product types*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    91
           ap_split T2 T3
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    92
           ((ap_split T1 (factors T2 ---> T3) (incr_boundvars 1 u)) $ 
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    93
            Bound 0))
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    94
  | ap_split T T3 u = u;
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    95
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    96
(*Makes a nested tuple from a list, following the product type structure*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
    97
fun mk_tuple pair (Type("*", [T1,T2])) tms = 
33957
e9afca2118d4 normalized uncurry take/drop
haftmann
parents: 33955
diff changeset
    98
        pair $ mk_tuple pair T1 tms
e9afca2118d4 normalized uncurry take/drop
haftmann
parents: 33955
diff changeset
    99
             $ mk_tuple pair T2 (drop (length (factors T1)) tms)
6049
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   100
  | mk_tuple pair T (t::_) = t;
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   101
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   102
(*Attempts to remove occurrences of split, and pair-valued parameters*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   103
val remove_split = rewrite_rule [Pr.split_eq];
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   104
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   105
(*Uncurries any Var according to its "pseudo-product type" T1 in the rule*)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   106
fun split_rule_var (Var(v,_), Type("fun",[T1,T2]), rl) =
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   107
      let val T' = factors T1 ---> T2
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   108
          val newt = ap_split T1 T2 (Var(v,T'))
22596
d0d2af4db18f rep_thm/cterm/ctyp: removed obsolete sign field;
wenzelm
parents: 15674
diff changeset
   109
          val cterm = Thm.cterm_of (Thm.theory_of_thm rl)
6049
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   110
      in
43333
2bdec7f430d3 renamed Drule.instantiate to Drule.instantiate_normalize to emphasize its meaning as opposed to plain Thm.instantiate;
wenzelm
parents: 35762
diff changeset
   111
          remove_split (Drule.instantiate_normalize ([], [(cterm (Var(v, Ind_Syntax.iT-->T2)), 
6049
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   112
                                           cterm newt)]) rl)
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   113
      end
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   114
  | split_rule_var (t,T,rl) = rl;
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   115
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   116
end;
7fef0169ab5e moved from ZF to new subdirectory Tools
paulson
parents:
diff changeset
   117