src/Pure/logic.ML
changeset 18938 b401ee1cda14
parent 18762 9098c92a945f
child 18964 67f572e03236
equal deleted inserted replaced
18937:0eb35519f0f3 18938:b401ee1cda14
    26   val mk_conjunction_list: term list -> term
    26   val mk_conjunction_list: term list -> term
    27   val mk_conjunction_list2: term list list -> term
    27   val mk_conjunction_list2: term list list -> term
    28   val dest_conjunction: term -> term * term
    28   val dest_conjunction: term -> term * term
    29   val dest_conjunctions: term -> term list
    29   val dest_conjunctions: term -> term list
    30   val strip_horn: term -> term list * term
    30   val strip_horn: term -> term list * term
       
    31   val dest_type: term -> typ
       
    32   val const_of_class: class -> string
       
    33   val class_of_const: string -> class
       
    34   val mk_inclass: typ * class -> term
       
    35   val dest_inclass: term -> typ * class
       
    36   val dest_def: Pretty.pp -> (term -> bool) -> (string -> bool) -> (string -> bool) ->
       
    37     term -> (term * term) * term
       
    38   val abs_def: term -> term * term
    31   val mk_cond_defpair: term list -> term * term -> string * term
    39   val mk_cond_defpair: term list -> term * term -> string * term
    32   val mk_defpair: term * term -> string * term
    40   val mk_defpair: term * term -> string * term
    33   val mk_type: typ -> term
    41   val mk_type: typ -> term
    34   val dest_type: term -> typ
       
    35   val mk_inclass: typ * class -> term
       
    36   val dest_inclass: term -> typ * class
       
    37   val protectC: term
    42   val protectC: term
    38   val protect: term -> term
    43   val protect: term -> term
    39   val unprotect: term -> term
    44   val unprotect: term -> term
    40   val occs: term * term -> bool
    45   val occs: term * term -> bool
    41   val close_form: term -> term
    46   val close_form: term -> term
       
    47   val combound: term * int * int -> term
       
    48   val rlist_abs: (string * typ) list * term -> term
    42   val incr_indexes: typ list * int -> term -> term
    49   val incr_indexes: typ list * int -> term -> term
    43   val incr_tvar: int -> typ -> typ
    50   val incr_tvar: int -> typ -> typ
    44   val lift_abs: int -> term -> term -> term
    51   val lift_abs: int -> term -> term -> term
    45   val lift_all: int -> term -> term -> term
    52   val lift_all: int -> term -> term -> term
    46   val strip_assums_hyp: term -> term list
    53   val strip_assums_hyp: term -> term list
   165     NONE => [t]
   172     NONE => [t]
   166   | SOME (A, B) => dest_conjunctions A @ dest_conjunctions B);
   173   | SOME (A, B) => dest_conjunctions A @ dest_conjunctions B);
   167 
   174 
   168 
   175 
   169 
   176 
       
   177 (** types as terms **)
       
   178 
       
   179 fun mk_type ty = Const ("TYPE", itselfT ty);
       
   180 
       
   181 fun dest_type (Const ("TYPE", Type ("itself", [ty]))) = ty
       
   182   | dest_type t = raise TERM ("dest_type", [t]);
       
   183 
       
   184 
       
   185 (** class constraints **)
       
   186 
       
   187 val classN = "_class";
       
   188 
       
   189 val const_of_class = suffix classN;
       
   190 fun class_of_const c = unsuffix classN c
       
   191   handle Fail _ => raise TERM ("class_of_const: bad name " ^ quote c, []);
       
   192 
       
   193 fun mk_inclass (ty, c) =
       
   194   Const (const_of_class c, itselfT ty --> propT) $ mk_type ty;
       
   195 
       
   196 fun dest_inclass (t as Const (c_class, _) $ ty) =
       
   197       ((dest_type ty, class_of_const c_class)
       
   198         handle TERM _ => raise TERM ("dest_inclass", [t]))
       
   199   | dest_inclass t = raise TERM ("dest_inclass", [t]);
       
   200 
       
   201 
       
   202 
   170 (** definitions **)
   203 (** definitions **)
       
   204 
       
   205 (*c x == t[x] to !!x. c x == t[x]*)
       
   206 fun dest_def pp check_head is_fixed is_fixedT eq =
       
   207   let
       
   208     fun err msg = raise TERM (msg, [eq]);
       
   209     val bound_vars = Syntax.bound_vars (Term.strip_all_vars eq);
       
   210     val display_terms = commas_quote o map (Pretty.string_of_term pp o bound_vars);
       
   211     val display_types = commas_quote o map (Pretty.string_of_typ pp);
       
   212 
       
   213     val (lhs, rhs) = dest_equals (Term.strip_all_body eq)
       
   214       handle TERM _ => err "Not a meta-equality (==)";
       
   215     val (head, args) = Term.strip_comb (Envir.beta_eta_contract lhs);
       
   216     val head_tfrees = Term.add_tfrees head [];
       
   217 
       
   218     fun check_arg (Bound _) = true
       
   219       | check_arg (Free (x, _)) = not (is_fixed x)
       
   220       | check_arg (Const ("TYPE", Type ("itself", [TFree _]))) = true
       
   221       | check_arg _ = false;
       
   222     fun close_arg (Bound _) t = t
       
   223       | close_arg x t = Term.all (Term.fastype_of x) $ lambda x t;
       
   224 
       
   225     val lhs_bads = filter_out check_arg args;
       
   226     val lhs_dups = gen_duplicates (op aconv) args;
       
   227     val rhs_extras = Term.fold_aterms (fn v as Free (x, _) =>
       
   228       if is_fixed x orelse member (op aconv) args v then I
       
   229       else insert (op aconv) v | _ => I) rhs [];
       
   230     val rhs_extrasT = Term.fold_aterms (Term.fold_types (fn v as TFree (a, S) =>
       
   231       if is_fixedT a orelse member (op =) head_tfrees (a, S) then I
       
   232       else insert (op =) v | _ => I)) rhs [];
       
   233   in
       
   234     if not (check_head head) then
       
   235       err ("Bad head of lhs: " ^ display_terms [head])
       
   236     else if not (null lhs_bads) then
       
   237       err ("Bad arguments on lhs: " ^ display_terms lhs_bads)
       
   238     else if not (null lhs_dups) then
       
   239       err ("Duplicate arguments on lhs: " ^ display_terms lhs_dups)
       
   240     else if not (null rhs_extras) then
       
   241       err ("Extra variables on rhs: " ^ display_terms rhs_extras)
       
   242     else if not (null rhs_extrasT) then
       
   243       err ("Extra type variables on rhs: " ^ display_types rhs_extrasT)
       
   244     else if exists_subterm (fn t => t aconv head) rhs then
       
   245       err "Entity to be defined occurs on rhs"
       
   246     else ((lhs, rhs), fold_rev close_arg args eq)
       
   247   end;
       
   248 
       
   249 (*!!x. c x == t[x] to c == %x. t[x]*)
       
   250 fun abs_def eq =
       
   251   let
       
   252     val body = Term.strip_all_body eq;
       
   253     val vars = map Free (Term.rename_wrt_term body (Term.strip_all_vars eq));
       
   254     val (lhs, rhs) = dest_equals (Term.subst_bounds (vars, body));
       
   255     val (lhs', args) = Term.strip_comb lhs;
       
   256     val rhs' = Term.list_abs_free (map Term.dest_Free args, rhs);
       
   257   in (lhs', rhs') end;
   171 
   258 
   172 fun mk_cond_defpair As (lhs, rhs) =
   259 fun mk_cond_defpair As (lhs, rhs) =
   173   (case Term.head_of lhs of
   260   (case Term.head_of lhs of
   174     Const (name, _) =>
   261     Const (name, _) =>
   175       (Sign.base_name name ^ "_def", list_implies (As, mk_equals (lhs, rhs)))
   262       (NameSpace.base name ^ "_def", list_implies (As, mk_equals (lhs, rhs)))
   176   | _ => raise TERM ("Malformed definition: head of lhs not a constant", [lhs, rhs]));
   263   | _ => raise TERM ("Malformed definition: head of lhs not a constant", [lhs, rhs]));
   177 
   264 
   178 fun mk_defpair lhs_rhs = mk_cond_defpair [] lhs_rhs;
   265 fun mk_defpair lhs_rhs = mk_cond_defpair [] lhs_rhs;
   179 
       
   180 
       
   181 (** types as terms **)
       
   182 
       
   183 fun mk_type ty = Const ("TYPE", itselfT ty);
       
   184 
       
   185 fun dest_type (Const ("TYPE", Type ("itself", [ty]))) = ty
       
   186   | dest_type t = raise TERM ("dest_type", [t]);
       
   187 
       
   188 
       
   189 (** class constraints **)
       
   190 
       
   191 fun mk_inclass (ty, c) =
       
   192   Const (Sign.const_of_class c, itselfT ty --> propT) $ mk_type ty;
       
   193 
       
   194 fun dest_inclass (t as Const (c_class, _) $ ty) =
       
   195       ((dest_type ty, Sign.class_of_const c_class)
       
   196         handle TERM _ => raise TERM ("dest_inclass", [t]))
       
   197   | dest_inclass t = raise TERM ("dest_inclass", [t]);
       
   198 
   266 
   199 
   267 
   200 (** protected propositions **)
   268 (** protected propositions **)
   201 
   269 
   202 val protectC = Const ("prop", propT --> propT);
   270 val protectC = Const ("prop", propT --> propT);
   216 (*Close up a formula over all free variables by quantification*)
   284 (*Close up a formula over all free variables by quantification*)
   217 fun close_form A =
   285 fun close_form A =
   218   list_all_free (sort_wrt fst (map dest_Free (term_frees A)), A);
   286   list_all_free (sort_wrt fst (map dest_Free (term_frees A)), A);
   219 
   287 
   220 
   288 
       
   289 
   221 (*** Specialized operations for resolution... ***)
   290 (*** Specialized operations for resolution... ***)
       
   291 
       
   292 (*computes t(Bound(n+k-1),...,Bound(n))  *)
       
   293 fun combound (t, n, k) =
       
   294     if  k>0  then  combound (t,n+1,k-1) $ (Bound n)  else  t;
       
   295 
       
   296 (* ([xn,...,x1], t)   ======>   (x1,...,xn)t *)
       
   297 fun rlist_abs ([], body) = body
       
   298   | rlist_abs ((a,T)::pairs, body) = rlist_abs(pairs, Abs(a, T, body));
       
   299 
   222 
   300 
   223 local exception SAME in
   301 local exception SAME in
   224 
   302 
   225 fun incrT k =
   303 fun incrT k =
   226   let
   304   let
   240   let
   318   let
   241     val n = length Ts;
   319     val n = length Ts;
   242     val incrT = if k = 0 then I else incrT k;
   320     val incrT = if k = 0 then I else incrT k;
   243 
   321 
   244     fun incr lev (Var ((x, i), T)) =
   322     fun incr lev (Var ((x, i), T)) =
   245           Unify.combound (Var ((x, i + k), Ts ---> (incrT T handle SAME => T)), lev, n)
   323           combound (Var ((x, i + k), Ts ---> (incrT T handle SAME => T)), lev, n)
   246       | incr lev (Abs (x, T, body)) =
   324       | incr lev (Abs (x, T, body)) =
   247           (Abs (x, incrT T, incr (lev + 1) body handle SAME => body)
   325           (Abs (x, incrT T, incr (lev + 1) body handle SAME => body)
   248             handle SAME => Abs (x, T, incr (lev + 1) body))
   326             handle SAME => Abs (x, T, incr (lev + 1) body))
   249       | incr lev (t $ u) =
   327       | incr lev (t $ u) =
   250           (incr lev t $ (incr lev u handle SAME => u)
   328           (incr lev t $ (incr lev u handle SAME => u)
   383   nasms is the number of assumptions in the original subgoal, needed when B
   461   nasms is the number of assumptions in the original subgoal, needed when B
   384     has the form B1 ==> B2: it stops B1 from being taken as an assumption. *)
   462     has the form B1 ==> B2: it stops B1 from being taken as an assumption. *)
   385 fun assum_pairs(nasms,A) =
   463 fun assum_pairs(nasms,A) =
   386   let val (params, A') = strip_assums_all ([],A)
   464   let val (params, A') = strip_assums_all ([],A)
   387       val (Hs,B) = strip_assums_imp (nasms,[],A')
   465       val (Hs,B) = strip_assums_imp (nasms,[],A')
   388       fun abspar t = Unify.rlist_abs(params, t)
   466       fun abspar t = rlist_abs(params, t)
   389       val D = abspar B
   467       val D = abspar B
   390       fun pairrev ([], pairs) = pairs
   468       fun pairrev ([], pairs) = pairs
   391         | pairrev (H::Hs, pairs) = pairrev(Hs,  (abspar H, D) :: pairs)
   469         | pairrev (H::Hs, pairs) = pairrev(Hs,  (abspar H, D) :: pairs)
   392   in  pairrev (Hs,[])
   470   in  pairrev (Hs,[])
   393   end;
   471   end;
   394 
   472 
   395 (*Converts Frees to Vars and TFrees to TVars so that axioms can be written
   473 (*Converts Frees to Vars and TFrees to TVars*)
   396   without (?) everywhere*)
       
   397 fun varify (Const(a, T)) = Const (a, Type.varifyT T)
   474 fun varify (Const(a, T)) = Const (a, Type.varifyT T)
   398   | varify (Free (a, T)) = Var ((a, 0), Type.varifyT T)
   475   | varify (Free (a, T)) = Var ((a, 0), Type.varifyT T)
   399   | varify (Var (ixn, T)) = Var (ixn, Type.varifyT T)
   476   | varify (Var (ixn, T)) = Var (ixn, Type.varifyT T)
   400   | varify (t as Bound _) = t
   477   | varify (t as Bound _) = t
   401   | varify (Abs (a, T, body)) = Abs (a, Type.varifyT T, varify body)
   478   | varify (Abs (a, T, body)) = Abs (a, Type.varifyT T, varify body)
   402   | varify (f $ t) = varify f $ varify t;
   479   | varify (f $ t) = varify f $ varify t;
   403 
   480 
   404 (*Inverse of varify.  Converts axioms back to their original form.*)
   481 (*Inverse of varify.*)
   405 fun unvarify (Const (a, T)) = Const (a, Type.unvarifyT T)
   482 fun unvarify (Const (a, T)) = Const (a, Type.unvarifyT T)
   406   | unvarify (Free (a, T)) = Free (a, Type.unvarifyT T)
   483   | unvarify (Free (a, T)) = Free (a, Type.unvarifyT T)
   407   | unvarify (Var ((a, 0), T)) = Free (a, Type.unvarifyT T)
   484   | unvarify (Var ((a, 0), T)) = Free (a, Type.unvarifyT T)
   408   | unvarify (Var (ixn, T)) = Var (ixn, Type.unvarifyT T)  (*non-0 index!*)
   485   | unvarify (Var (ixn, T)) = Var (ixn, Type.unvarifyT T)  (*non-0 index!*)
   409   | unvarify (t as Bound _) = t
   486   | unvarify (t as Bound _) = t