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