src/ZF/ind_syntax.ML
author wenzelm
Wed Jun 18 18:55:10 2008 +0200 (2008-06-18)
changeset 27261 5b3101338f42
parent 27239 f2f42f9fa09d
child 28965 1de908189869
permissions -rw-r--r--
eliminated old Sign.read_term/Thm.read_cterm etc.;
wenzelm@12243
     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
wenzelm@12243
     6
Abstract Syntax functions for Inductive Definitions.
clasohm@0
     7
*)
clasohm@0
     8
lcp@516
     9
structure Ind_Syntax =
lcp@516
    10
struct
clasohm@0
    11
paulson@4804
    12
(*Print tracing messages during processing of "inductive" theory sections*)
paulson@4804
    13
val trace = ref false;
paulson@4804
    14
wenzelm@26189
    15
fun traceIt msg thy t =
wenzelm@26939
    16
  if !trace then (tracing (msg ^ Syntax.string_of_term_global thy t); t)
wenzelm@17988
    17
  else t;
wenzelm@17988
    18
paulson@6053
    19
paulson@4352
    20
(** Abstract syntax definitions for ZF **)
clasohm@0
    21
paulson@4352
    22
val iT = Type("i",[]);
clasohm@0
    23
clasohm@0
    24
(*Creates All(%v.v:A --> P(v)) rather than Ball(A,P) *)
wenzelm@26189
    25
fun mk_all_imp (A,P) =
wenzelm@26189
    26
    FOLogic.all_const iT $
wenzelm@26189
    27
      Abs("v", iT, FOLogic.imp $ (@{const mem} $ Bound 0 $ A) $
wenzelm@18176
    28
	           Term.betapply(P, Bound 0));
clasohm@0
    29
wenzelm@26189
    30
fun mk_Collect (a, D, t) = @{const Collect} $ D $ absfree (a, iT, t);
clasohm@0
    31
lcp@516
    32
(*simple error-checking in the premises of an inductive definition*)
wenzelm@26189
    33
fun chk_prem rec_hd (Const (@{const_name "op &"}, _) $ _ $ _) =
clasohm@1461
    34
        error"Premises may not be conjuctive"
wenzelm@26189
    35
  | chk_prem rec_hd (Const (@{const_name mem}, _) $ t $ X) =
wenzelm@22567
    36
        (Logic.occs(rec_hd,t) andalso error "Recursion term on left of member symbol"; ())
wenzelm@26189
    37
  | chk_prem rec_hd t =
wenzelm@22567
    38
        (Logic.occs(rec_hd,t) andalso error "Recursion term in side formula"; ());
lcp@516
    39
lcp@14
    40
(*Return the conclusion of a rule, of the form t:X*)
wenzelm@26189
    41
fun rule_concl rl =
wenzelm@26189
    42
    let val Const (@{const_name Trueprop}, _) $ (Const (@{const_name mem}, _) $ t $ X) =
clasohm@1461
    43
                Logic.strip_imp_concl rl
lcp@435
    44
    in  (t,X)  end;
lcp@435
    45
lcp@435
    46
(*As above, but return error message if bad*)
lcp@435
    47
fun rule_concl_msg sign rl = rule_concl rl
wenzelm@26189
    48
    handle Bind => error ("Ill-formed conclusion of introduction rule: " ^
wenzelm@26939
    49
                          Syntax.string_of_term_global sign rl);
clasohm@0
    50
clasohm@0
    51
(*For deriving cases rules.  CollectD2 discards the domain, which is redundant;
clasohm@0
    52
  read_instantiate replaces a propositional variable by a formula variable*)
wenzelm@26189
    53
val equals_CollectD =
wenzelm@27239
    54
    read_instantiate @{context} [(("W", 0), "?Q")]
wenzelm@24893
    55
        (make_elim (@{thm equalityD1} RS @{thm subsetD} RS @{thm CollectD2}));
clasohm@0
    56
clasohm@0
    57
lcp@516
    58
(** For datatype definitions **)
lcp@516
    59
paulson@6053
    60
(*Constructor name, type, mixfix info;
paulson@6053
    61
  internal name from mixfix, datatype sets, full premises*)
wenzelm@26189
    62
type constructor_spec =
wenzelm@26189
    63
    (string * typ * mixfix) * string * term list * term list;
paulson@6053
    64
wenzelm@26189
    65
fun dest_mem (Const (@{const_name mem}, _) $ x $ A) = (x, A)
lcp@516
    66
  | dest_mem _ = error "Constructor specifications must have the form x:A";
lcp@516
    67
lcp@516
    68
(*read a constructor specification*)
wenzelm@27261
    69
fun read_construct ctxt (id, sprems, syn) =
wenzelm@27261
    70
    let val prems = map (Syntax.parse_term ctxt #> TypeInfer.constrain FOLogic.oT) sprems
wenzelm@27261
    71
          |> Syntax.check_terms ctxt
clasohm@1461
    72
        val args = map (#1 o dest_mem) prems
clasohm@1461
    73
        val T = (map (#2 o dest_Free) args) ---> iT
wenzelm@26189
    74
                handle TERM _ => error
clasohm@1461
    75
                    "Bad variable in constructor specification"
wenzelm@568
    76
        val name = Syntax.const_name id syn  (*handle infix constructors*)
lcp@516
    77
    in ((id,T,syn), name, args, prems) end;
lcp@516
    78
lcp@516
    79
val read_constructs = map o map o read_construct;
clasohm@0
    80
lcp@516
    81
(*convert constructor specifications into introduction rules*)
wenzelm@3925
    82
fun mk_intr_tms sg (rec_tm, constructs) =
wenzelm@3925
    83
  let
wenzelm@3925
    84
    fun mk_intr ((id,T,syn), name, args, prems) =
wenzelm@3925
    85
      Logic.list_implies
paulson@4352
    86
        (map FOLogic.mk_Trueprop prems,
paulson@4352
    87
	 FOLogic.mk_Trueprop
wenzelm@26189
    88
	    (@{const mem} $ list_comb (Const (Sign.full_name sg name, T), args)
paulson@4352
    89
	               $ rec_tm))
lcp@516
    90
  in  map mk_intr constructs  end;
lcp@516
    91
wenzelm@3925
    92
fun mk_all_intr_tms sg arg = List.concat (ListPair.map (mk_intr_tms sg) arg);
clasohm@0
    93
wenzelm@26189
    94
fun mk_Un (t1, t2) = @{const Un} $ t1 $ t2;
clasohm@0
    95
lcp@516
    96
(*Make a datatype's domain: form the union of its set parameters*)
paulson@6112
    97
fun union_params (rec_tm, cs) =
lcp@516
    98
  let val (_,args) = strip_comb rec_tm
paulson@6112
    99
      fun is_ind arg = (type_of arg = iT)
skalberg@15570
   100
  in  case List.filter is_ind (args @ cs) of
wenzelm@26189
   101
         []     => @{const 0}
wenzelm@23419
   102
       | u_args => BalancedTree.make mk_Un u_args
lcp@516
   103
  end;
lcp@516
   104
clasohm@0
   105
paulson@1418
   106
(*Includes rules for succ and Pair since they are common constructions*)
wenzelm@26189
   107
val elim_rls =
wenzelm@26189
   108
  [@{thm asm_rl}, @{thm FalseE}, @{thm succ_neq_0}, @{thm sym} RS @{thm succ_neq_0},
wenzelm@26189
   109
   @{thm Pair_neq_0}, @{thm sym} RS @{thm Pair_neq_0}, @{thm Pair_inject},
wenzelm@26189
   110
   make_elim @{thm succ_inject}, @{thm refl_thin}, @{thm conjE}, @{thm exE}, @{thm disjE}];
paulson@1418
   111
wenzelm@7694
   112
wenzelm@7694
   113
(*From HOL/ex/meson.ML: raises exception if no rules apply -- unlike RL*)
wenzelm@7694
   114
fun tryres (th, rl::rls) = (th RS rl handle THM _ => tryres(th,rls))
wenzelm@7694
   115
  | tryres (th, []) = raise THM("tryres", 0, [th]);
wenzelm@7694
   116
wenzelm@26189
   117
fun gen_make_elim elim_rls rl =
wenzelm@7694
   118
      standard (tryres (rl, elim_rls @ [revcut_rl]));
wenzelm@7694
   119
paulson@1418
   120
(*Turns iff rules into safe elimination rules*)
wenzelm@26189
   121
fun mk_free_SEs iffs = map (gen_make_elim [@{thm conjE}, @{thm FalseE}]) (iffs RL [@{thm iffD1}]);
paulson@1418
   122
lcp@516
   123
end;
lcp@516
   124