src/ZF/ind_syntax.ML
author paulson
Fri Apr 10 13:15:28 1998 +0200 (1998-04-10)
changeset 4804 02b7c759159b
parent 4352 7ac9f3e8a97d
child 4972 7fe1d30c1374
permissions -rw-r--r--
Fixed bug in inductive sections to allow disjunctive premises;
added tracing flag trace_induct
clasohm@1461
     1
(*  Title:      ZF/ind-syntax.ML
clasohm@0
     2
    ID:         $Id$
clasohm@1461
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
clasohm@0
     6
Abstract Syntax functions for Inductive Definitions
clasohm@0
     7
*)
clasohm@0
     8
lcp@516
     9
(*The structure protects these items from redeclaration (somewhat!).  The 
lcp@516
    10
  datatype definitions in theory files refer to these items by name!
lcp@516
    11
*)
lcp@516
    12
structure Ind_Syntax =
lcp@516
    13
struct
clasohm@0
    14
paulson@4804
    15
(*Print tracing messages during processing of "inductive" theory sections*)
paulson@4804
    16
val trace = ref false;
paulson@4804
    17
paulson@4352
    18
(** Abstract syntax definitions for ZF **)
clasohm@0
    19
paulson@4352
    20
val iT = Type("i",[]);
clasohm@0
    21
paulson@4352
    22
val mem_const = Const("op :", [iT,iT]--->FOLogic.oT);
clasohm@0
    23
clasohm@0
    24
(*Creates All(%v.v:A --> P(v)) rather than Ball(A,P) *)
clasohm@0
    25
fun mk_all_imp (A,P) = 
paulson@4352
    26
    FOLogic.all_const iT $ 
paulson@4352
    27
      Abs("v", iT, FOLogic.imp $ (mem_const $ Bound 0 $ A) $ (P $ Bound 0));
clasohm@0
    28
clasohm@0
    29
val Part_const = Const("Part", [iT,iT-->iT]--->iT);
clasohm@0
    30
paulson@4352
    31
val Collect_const = Const("Collect", [iT, iT-->FOLogic.oT] ---> iT);
clasohm@0
    32
fun mk_Collect (a,D,t) = Collect_const $ D $ absfree(a, iT, t);
clasohm@0
    33
lcp@516
    34
(*simple error-checking in the premises of an inductive definition*)
lcp@516
    35
fun chk_prem rec_hd (Const("op &",_) $ _ $ _) =
clasohm@1461
    36
        error"Premises may not be conjuctive"
lcp@516
    37
  | chk_prem rec_hd (Const("op :",_) $ t $ X) = 
clasohm@1461
    38
        deny (Logic.occs(rec_hd,t)) "Recursion term on left of member symbol"
lcp@516
    39
  | chk_prem rec_hd t = 
clasohm@1461
    40
        deny (Logic.occs(rec_hd,t)) "Recursion term in side formula";
lcp@516
    41
lcp@14
    42
(*Return the conclusion of a rule, of the form t:X*)
clasohm@0
    43
fun rule_concl rl = 
lcp@435
    44
    let val Const("Trueprop",_) $ (Const("op :",_) $ t $ X) = 
clasohm@1461
    45
                Logic.strip_imp_concl rl
lcp@435
    46
    in  (t,X)  end;
lcp@435
    47
lcp@435
    48
(*As above, but return error message if bad*)
lcp@435
    49
fun rule_concl_msg sign rl = rule_concl rl
lcp@435
    50
    handle Bind => error ("Ill-formed conclusion of introduction rule: " ^ 
clasohm@1461
    51
                          Sign.string_of_term sign rl);
clasohm@0
    52
clasohm@0
    53
(*For deriving cases rules.  CollectD2 discards the domain, which is redundant;
clasohm@0
    54
  read_instantiate replaces a propositional variable by a formula variable*)
clasohm@0
    55
val equals_CollectD = 
clasohm@0
    56
    read_instantiate [("W","?Q")]
clasohm@0
    57
        (make_elim (equalityD1 RS subsetD RS CollectD2));
clasohm@0
    58
clasohm@0
    59
lcp@516
    60
(** For datatype definitions **)
lcp@516
    61
lcp@516
    62
fun dest_mem (Const("op :",_) $ x $ A) = (x,A)
lcp@516
    63
  | dest_mem _ = error "Constructor specifications must have the form x:A";
lcp@516
    64
lcp@516
    65
(*read a constructor specification*)
lcp@516
    66
fun read_construct sign (id, sprems, syn) =
paulson@4352
    67
    let val prems = map (readtm sign FOLogic.oT) sprems
clasohm@1461
    68
        val args = map (#1 o dest_mem) prems
clasohm@1461
    69
        val T = (map (#2 o dest_Free) args) ---> iT
clasohm@1461
    70
                handle TERM _ => error 
clasohm@1461
    71
                    "Bad variable in constructor specification"
wenzelm@568
    72
        val name = Syntax.const_name id syn  (*handle infix constructors*)
lcp@516
    73
    in ((id,T,syn), name, args, prems) end;
lcp@516
    74
lcp@516
    75
val read_constructs = map o map o read_construct;
clasohm@0
    76
lcp@516
    77
(*convert constructor specifications into introduction rules*)
wenzelm@3925
    78
fun mk_intr_tms sg (rec_tm, constructs) =
wenzelm@3925
    79
  let
wenzelm@3925
    80
    fun mk_intr ((id,T,syn), name, args, prems) =
wenzelm@3925
    81
      Logic.list_implies
paulson@4352
    82
        (map FOLogic.mk_Trueprop prems,
paulson@4352
    83
	 FOLogic.mk_Trueprop
paulson@4352
    84
	    (mem_const $ list_comb (Const (Sign.full_name sg name, T), args)
paulson@4352
    85
	               $ rec_tm))
lcp@516
    86
  in  map mk_intr constructs  end;
lcp@516
    87
wenzelm@3925
    88
fun mk_all_intr_tms sg arg = List.concat (ListPair.map (mk_intr_tms sg) arg);
clasohm@0
    89
clasohm@1461
    90
val Un          = Const("op Un", [iT,iT]--->iT)
clasohm@1461
    91
and empty       = Const("0", iT)
clasohm@1461
    92
and univ        = Const("univ", iT-->iT)
clasohm@1461
    93
and quniv       = Const("quniv", iT-->iT);
clasohm@0
    94
lcp@516
    95
(*Make a datatype's domain: form the union of its set parameters*)
lcp@516
    96
fun union_params rec_tm =
lcp@516
    97
  let val (_,args) = strip_comb rec_tm
lcp@516
    98
  in  case (filter (fn arg => type_of arg = iT) args) of
lcp@516
    99
         []    => empty
lcp@516
   100
       | iargs => fold_bal (app Un) iargs
lcp@516
   101
  end;
lcp@516
   102
lcp@742
   103
(*Previously these both did    replicate (length rec_tms);  however now
lcp@742
   104
  [q]univ itself constitutes the sum domain for mutual recursion!*)
lcp@742
   105
fun data_domain rec_tms = univ $ union_params (hd rec_tms);
lcp@742
   106
fun Codata_domain rec_tms = quniv $ union_params (hd rec_tms);
clasohm@0
   107
clasohm@0
   108
(*Could go to FOL, but it's hardly general*)
lcp@516
   109
val def_swap_iff = prove_goal IFOL.thy "a==b ==> a=c <-> c=b"
lcp@516
   110
 (fn [def] => [(rewtac def), (rtac iffI 1), (REPEAT (etac sym 1))]);
clasohm@0
   111
clasohm@0
   112
val def_trans = prove_goal IFOL.thy "[| f==g;  g(a)=b |] ==> f(a)=b"
clasohm@0
   113
  (fn [rew,prem] => [ rewtac rew, rtac prem 1 ]);
clasohm@0
   114
lcp@55
   115
(*Delete needless equality assumptions*)
lcp@55
   116
val refl_thin = prove_goal IFOL.thy "!!P. [| a=a;  P |] ==> P"
lcp@55
   117
     (fn _ => [assume_tac 1]);
clasohm@0
   118
paulson@1418
   119
(*Includes rules for succ and Pair since they are common constructions*)
paulson@1418
   120
val elim_rls = [asm_rl, FalseE, succ_neq_0, sym RS succ_neq_0, 
clasohm@1461
   121
                Pair_neq_0, sym RS Pair_neq_0, Pair_inject,
clasohm@1461
   122
                make_elim succ_inject, 
clasohm@1461
   123
                refl_thin, conjE, exE, disjE];
paulson@1418
   124
paulson@1418
   125
(*Turns iff rules into safe elimination rules*)
paulson@1418
   126
fun mk_free_SEs iffs = map (gen_make_elim [conjE,FalseE]) (iffs RL [iffD1]);
paulson@1418
   127
lcp@516
   128
end;
lcp@516
   129
paulson@4804
   130
paulson@4804
   131
val trace_induct = Ind_Syntax.trace;