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