src/Pure/thm.ML
changeset 1238 289c573327f0
parent 1229 f191f25a5ec8
child 1258 2a2d8c74a756
equal deleted inserted replaced
1237:45ac644b0052 1238:289c573327f0
     8 simplification).
     8 simplification).
     9 *)
     9 *)
    10 
    10 
    11 signature THM =
    11 signature THM =
    12 sig
    12 sig
    13   structure Envir 	: ENVIR
    13   structure Envir       : ENVIR
    14   structure Sequence 	: SEQUENCE
    14   structure Sequence    : SEQUENCE
    15   structure Sign 	: SIGN
    15   structure Sign        : SIGN
    16 
    16 
    17   (*certified types*)
    17   (*certified types*)
    18   type ctyp
    18   type ctyp
    19   val rep_ctyp		: ctyp -> {sign: Sign.sg, T: typ}
    19   val rep_ctyp          : ctyp -> {sign: Sign.sg, T: typ}
    20   val typ_of		: ctyp -> typ
    20   val typ_of            : ctyp -> typ
    21   val ctyp_of		: Sign.sg -> typ -> ctyp
    21   val ctyp_of           : Sign.sg -> typ -> ctyp
    22   val read_ctyp		: Sign.sg -> string -> ctyp
    22   val read_ctyp         : Sign.sg -> string -> ctyp
    23 
    23 
    24   (*certified terms*)
    24   (*certified terms*)
    25   type cterm
    25   type cterm
    26   val rep_cterm		: cterm -> {sign: Sign.sg, t: term, T: typ, maxidx: int}
    26   val rep_cterm         : cterm -> {sign: Sign.sg, t: term, T: typ, maxidx: int}
    27   val term_of		: cterm -> term
    27   val term_of           : cterm -> term
    28   val cterm_of		: Sign.sg -> term -> cterm
    28   val cterm_of          : Sign.sg -> term -> cterm
    29   val read_cterm	: Sign.sg -> string * typ -> cterm
    29   val read_cterm        : Sign.sg -> string * typ -> cterm
    30   val cterm_fun		: (term -> term) -> (cterm -> cterm)
    30   val cterm_fun         : (term -> term) -> (cterm -> cterm)
    31   val dest_cimplies	: cterm -> cterm * cterm
    31   val dest_cimplies     : cterm -> cterm * cterm
    32   val read_def_cterm 	:
    32   val read_def_cterm    :
    33     Sign.sg * (indexname -> typ option) * (indexname -> sort option) ->
    33     Sign.sg * (indexname -> typ option) * (indexname -> sort option) ->
    34     string list -> bool -> string * typ -> cterm * (indexname * typ) list
    34     string list -> bool -> string * typ -> cterm * (indexname * typ) list
    35 
    35 
    36   (*meta theorems*)
    36   (*meta theorems*)
    37   type thm
    37   type thm
    38   exception THM of string * int * thm list
    38   exception THM of string * int * thm list
    39   val rep_thm		: thm -> {sign: Sign.sg, maxidx: int,
    39   val rep_thm           : thm -> {sign: Sign.sg, maxidx: int,
    40     shyps: sort list, hyps: term list, prop: term}
    40     shyps: sort list, hyps: term list, prop: term}
    41   val stamps_of_thm	: thm -> string ref list
    41   val stamps_of_thm     : thm -> string ref list
    42   val tpairs_of		: thm -> (term * term) list
    42   val tpairs_of         : thm -> (term * term) list
    43   val prems_of		: thm -> term list
    43   val prems_of          : thm -> term list
    44   val nprems_of		: thm -> int
    44   val nprems_of         : thm -> int
    45   val concl_of		: thm -> term
    45   val concl_of          : thm -> term
    46   val cprop_of		: thm -> cterm
    46   val cprop_of          : thm -> cterm
    47   val cert_axm		: Sign.sg -> string * term -> string * term
    47   val extra_shyps       : thm -> sort list
    48   val read_axm		: Sign.sg -> string * string -> string * term
    48   val force_strip_shyps : bool ref      (* FIXME tmp *)
    49   val inferT_axm	: Sign.sg -> string * term -> string * term
    49   val strip_shyps       : thm -> thm
       
    50   val implies_intr_shyps: thm -> thm
       
    51   val cert_axm          : Sign.sg -> string * term -> string * term
       
    52   val read_axm          : Sign.sg -> string * string -> string * term
       
    53   val inferT_axm        : Sign.sg -> string * term -> string * term
    50 
    54 
    51   (*theories*)
    55   (*theories*)
    52   type theory
    56   type theory
    53   exception THEORY of string * theory list
    57   exception THEORY of string * theory list
    54   val rep_theory	: theory ->
    58   val rep_theory        : theory ->
    55     {sign: Sign.sg, new_axioms: term Sign.Symtab.table, parents: theory list}
    59     {sign: Sign.sg, new_axioms: term Sign.Symtab.table, parents: theory list}
    56   val sign_of		: theory -> Sign.sg
    60   val sign_of           : theory -> Sign.sg
    57   val syn_of		: theory -> Sign.Syntax.syntax
    61   val syn_of            : theory -> Sign.Syntax.syntax
    58   val stamps_of_thy	: theory -> string ref list
    62   val stamps_of_thy     : theory -> string ref list
    59   val parents_of	: theory -> theory list
    63   val parents_of        : theory -> theory list
    60   val subthy		: theory * theory -> bool
    64   val subthy            : theory * theory -> bool
    61   val eq_thy		: theory * theory -> bool
    65   val eq_thy            : theory * theory -> bool
    62   val get_axiom		: theory -> string -> thm
    66   val get_axiom         : theory -> string -> thm
    63   val axioms_of		: theory -> (string * thm) list
    67   val axioms_of         : theory -> (string * thm) list
    64   val proto_pure_thy	: theory
    68   val proto_pure_thy    : theory
    65   val pure_thy		: theory
    69   val pure_thy          : theory
    66   val cpure_thy		: theory
    70   val cpure_thy         : theory
    67   local open Sign.Syntax in
    71   local open Sign.Syntax in
    68     val add_classes	: (class * class list) list -> theory -> theory
    72     val add_classes     : (class * class list) list -> theory -> theory
    69     val add_classrel	: (class * class) list -> theory -> theory
    73     val add_classrel    : (class * class) list -> theory -> theory
    70     val add_defsort	: sort -> theory -> theory
    74     val add_defsort     : sort -> theory -> theory
    71     val add_types	: (string * int * mixfix) list -> theory -> theory
    75     val add_types       : (string * int * mixfix) list -> theory -> theory
    72     val add_tyabbrs	: (string * string list * string * mixfix) list
    76     val add_tyabbrs     : (string * string list * string * mixfix) list
    73       -> theory -> theory
    77       -> theory -> theory
    74     val add_tyabbrs_i	: (string * string list * typ * mixfix) list
    78     val add_tyabbrs_i   : (string * string list * typ * mixfix) list
    75       -> theory -> theory
    79       -> theory -> theory
    76     val add_arities	: (string * sort list * sort) list -> theory -> theory
    80     val add_arities     : (string * sort list * sort) list -> theory -> theory
    77     val add_consts	: (string * string * mixfix) list -> theory -> theory
    81     val add_consts      : (string * string * mixfix) list -> theory -> theory
    78     val add_consts_i	: (string * typ * mixfix) list -> theory -> theory
    82     val add_consts_i    : (string * typ * mixfix) list -> theory -> theory
    79     val add_syntax	: (string * string * mixfix) list -> theory -> theory
    83     val add_syntax      : (string * string * mixfix) list -> theory -> theory
    80     val add_syntax_i	: (string * typ * mixfix) list -> theory -> theory
    84     val add_syntax_i    : (string * typ * mixfix) list -> theory -> theory
    81     val add_trfuns	:
    85     val add_trfuns      :
    82       (string * (ast list -> ast)) list *
    86       (string * (ast list -> ast)) list *
    83       (string * (term list -> term)) list *
    87       (string * (term list -> term)) list *
    84       (string * (term list -> term)) list *
    88       (string * (term list -> term)) list *
    85       (string * (ast list -> ast)) list -> theory -> theory
    89       (string * (ast list -> ast)) list -> theory -> theory
    86     val add_trrules	: (string * string) trrule list -> theory -> theory
    90     val add_trrules     : (string * string) trrule list -> theory -> theory
    87     val add_trrules_i	: ast trrule list -> theory -> theory
    91     val add_trrules_i   : ast trrule list -> theory -> theory
    88     val add_axioms	: (string * string) list -> theory -> theory
    92     val add_axioms      : (string * string) list -> theory -> theory
    89     val add_axioms_i	: (string * term) list -> theory -> theory
    93     val add_axioms_i    : (string * term) list -> theory -> theory
    90     val add_thyname	: string -> theory -> theory
    94     val add_thyname     : string -> theory -> theory
    91   end
    95   end
    92   val merge_theories	: theory * theory -> theory
    96   val merge_theories    : theory * theory -> theory
    93   val merge_thy_list	: bool -> theory list -> theory
    97   val merge_thy_list    : bool -> theory list -> theory
    94 
    98 
    95   (*meta rules*)
    99   (*meta rules*)
    96   val force_strip_shyps	: bool ref	(* FIXME tmp *)
   100   val assume            : cterm -> thm
    97   val strip_shyps	: thm -> thm
   101   val implies_intr      : cterm -> thm -> thm
    98   val implies_intr_shyps: thm -> thm
   102   val implies_elim      : thm -> thm -> thm
    99   val assume		: cterm -> thm
   103   val forall_intr       : cterm -> thm -> thm
   100   val implies_intr	: cterm -> thm -> thm
   104   val forall_elim       : cterm -> thm -> thm
   101   val implies_elim	: thm -> thm -> thm
   105   val flexpair_def      : thm
   102   val forall_intr	: cterm -> thm -> thm
   106   val reflexive         : cterm -> thm
   103   val forall_elim	: cterm -> thm -> thm
   107   val symmetric         : thm -> thm
   104   val flexpair_def	: thm
   108   val transitive        : thm -> thm -> thm
   105   val reflexive		: cterm -> thm
   109   val beta_conversion   : cterm -> thm
   106   val symmetric		: thm -> thm
   110   val extensional       : thm -> thm
   107   val transitive	: thm -> thm -> thm
   111   val abstract_rule     : string -> cterm -> thm -> thm
   108   val beta_conversion	: cterm -> thm
   112   val combination       : thm -> thm -> thm
   109   val extensional	: thm -> thm
   113   val equal_intr        : thm -> thm -> thm
   110   val abstract_rule	: string -> cterm -> thm -> thm
   114   val equal_elim        : thm -> thm -> thm
   111   val combination	: thm -> thm -> thm
   115   val implies_intr_hyps : thm -> thm
   112   val equal_intr	: thm -> thm -> thm
   116   val flexflex_rule     : thm -> thm Sequence.seq
   113   val equal_elim	: thm -> thm -> thm
   117   val instantiate       :
   114   val implies_intr_hyps	: thm -> thm
       
   115   val flexflex_rule	: thm -> thm Sequence.seq
       
   116   val instantiate	:
       
   117     (indexname * ctyp) list * (cterm * cterm) list -> thm -> thm
   118     (indexname * ctyp) list * (cterm * cterm) list -> thm -> thm
   118   val trivial		: cterm -> thm
   119   val trivial           : cterm -> thm
   119   val class_triv	: theory -> class -> thm
   120   val class_triv        : theory -> class -> thm
   120   val varifyT		: thm -> thm
   121   val varifyT           : thm -> thm
   121   val freezeT		: thm -> thm
   122   val freezeT           : thm -> thm
   122   val dest_state	: thm * int ->
   123   val dest_state        : thm * int ->
   123     (term * term) list * term list * term * term
   124     (term * term) list * term list * term * term
   124   val lift_rule		: (thm * int) -> thm -> thm
   125   val lift_rule         : (thm * int) -> thm -> thm
   125   val assumption	: int -> thm -> thm Sequence.seq
   126   val assumption        : int -> thm -> thm Sequence.seq
   126   val eq_assumption	: int -> thm -> thm
   127   val eq_assumption     : int -> thm -> thm
   127   val rename_params_rule: string list * int -> thm -> thm
   128   val rename_params_rule: string list * int -> thm -> thm
   128   val bicompose		: bool -> bool * thm * int ->
   129   val bicompose         : bool -> bool * thm * int ->
   129     int -> thm -> thm Sequence.seq
   130     int -> thm -> thm Sequence.seq
   130   val biresolution	: bool -> (bool * thm) list ->
   131   val biresolution      : bool -> (bool * thm) list ->
   131     int -> thm -> thm Sequence.seq
   132     int -> thm -> thm Sequence.seq
   132 
   133 
   133   (*meta simplification*)
   134   (*meta simplification*)
   134   type meta_simpset
   135   type meta_simpset
   135   exception SIMPLIFIER of string * thm
   136   exception SIMPLIFIER of string * thm
   136   val empty_mss		: meta_simpset
   137   val empty_mss         : meta_simpset
   137   val add_simps		: meta_simpset * thm list -> meta_simpset
   138   val add_simps         : meta_simpset * thm list -> meta_simpset
   138   val del_simps		: meta_simpset * thm list -> meta_simpset
   139   val del_simps         : meta_simpset * thm list -> meta_simpset
   139   val mss_of		: thm list -> meta_simpset
   140   val mss_of            : thm list -> meta_simpset
   140   val add_congs		: meta_simpset * thm list -> meta_simpset
   141   val add_congs         : meta_simpset * thm list -> meta_simpset
   141   val add_prems		: meta_simpset * thm list -> meta_simpset
   142   val add_prems         : meta_simpset * thm list -> meta_simpset
   142   val prems_of_mss	: meta_simpset -> thm list
   143   val prems_of_mss      : meta_simpset -> thm list
   143   val set_mk_rews	: meta_simpset * (thm -> thm list) -> meta_simpset
   144   val set_mk_rews       : meta_simpset * (thm -> thm list) -> meta_simpset
   144   val mk_rews_of_mss	: meta_simpset -> thm -> thm list
   145   val mk_rews_of_mss    : meta_simpset -> thm -> thm list
   145   val trace_simp	: bool ref
   146   val trace_simp        : bool ref
   146   val rewrite_cterm	: bool * bool -> meta_simpset ->
   147   val rewrite_cterm     : bool * bool -> meta_simpset ->
   147     (meta_simpset -> thm -> thm option) -> cterm -> thm
   148     (meta_simpset -> thm -> thm option) -> cterm -> thm
   148 end;
   149 end;
   149 
   150 
   150 functor ThmFun (structure Logic: LOGIC and Unify: UNIFY and Pattern: PATTERN
   151 functor ThmFun (structure Logic: LOGIC and Unify: UNIFY and Pattern: PATTERN
   151   and Net:NET sharing type Pattern.type_sig = Unify.Sign.Type.type_sig): THM =
   152   and Net:NET sharing type Pattern.type_sig = Unify.Sign.Type.type_sig): THM =
   221 
   222 
   222 fun read_cterm sign = #1 o read_def_cterm (sign, K None, K None) [] true;
   223 fun read_cterm sign = #1 o read_def_cterm (sign, K None, K None) [] true;
   223 
   224 
   224 
   225 
   225 
   226 
   226 (* FIXME -> library.ML *)
       
   227 
       
   228 fun unions [] = []
       
   229   | unions (xs :: xss) = foldr (op union) (xss, xs);
       
   230 
       
   231 
       
   232 (* FIXME -> term.ML *)
       
   233 
       
   234 (*accumulates the sorts in a type, suppressing duplicates*)
       
   235 fun add_typ_sorts (Type (_, Ts), Ss) = foldr add_typ_sorts (Ts, Ss)
       
   236   | add_typ_sorts (TFree (_, S), Ss) = S ins Ss
       
   237   | add_typ_sorts (TVar (_, S), Ss) = S ins Ss;
       
   238 
       
   239 val add_term_sorts = it_term_types add_typ_sorts;
       
   240 
       
   241 fun typ_sorts T = add_typ_sorts (T, []);
       
   242 fun term_sorts t = add_term_sorts (t, []);
       
   243 
       
   244 
       
   245 (* FIXME move? *)
       
   246 
       
   247 fun env_codT (Envir.Envir {iTs, ...}) = map snd iTs;
       
   248 
       
   249 
       
   250 
       
   251 (*** Meta theorems ***)
   227 (*** Meta theorems ***)
   252 
   228 
       
   229 (* FIXME comment *)
   253 datatype thm = Thm of
   230 datatype thm = Thm of
   254   {sign: Sign.sg, maxidx: int,
   231   {sign: Sign.sg, maxidx: int,
   255     shyps: sort list, hyps: term list, prop: term};
   232     shyps: sort list, hyps: term list, prop: term};
   256 
   233 
   257 fun rep_thm (Thm args) = args;
   234 fun rep_thm (Thm args) = args;
   284 fun concl_of (Thm {prop, ...}) = Logic.strip_imp_concl prop;
   261 fun concl_of (Thm {prop, ...}) = Logic.strip_imp_concl prop;
   285 
   262 
   286 (*the statement of any thm is a cterm*)
   263 (*the statement of any thm is a cterm*)
   287 fun cprop_of (Thm {sign, maxidx, prop, ...}) =
   264 fun cprop_of (Thm {sign, maxidx, prop, ...}) =
   288   Cterm {sign = sign, maxidx = maxidx, T = propT, t = prop};
   265   Cterm {sign = sign, maxidx = maxidx, T = propT, t = prop};
       
   266 
       
   267 
       
   268 
       
   269 (** sort contexts of theorems **)
       
   270 
       
   271 (* basic utils *)
       
   272 
       
   273 (*accumulate sorts suppressing duplicates; these are coded low level
       
   274   to improve efficiency a bit*)
       
   275 
       
   276 fun add_typ_sorts (Type (_, Ts), Ss) = add_typs_sorts (Ts, Ss)
       
   277   | add_typ_sorts (TFree (_, S), Ss) = S ins Ss
       
   278   | add_typ_sorts (TVar (_, S), Ss) = S ins Ss
       
   279 and add_typs_sorts ([], Ss) = Ss
       
   280   | add_typs_sorts (T :: Ts, Ss) = add_typs_sorts (Ts, add_typ_sorts (T, Ss));
       
   281 
       
   282 fun add_term_sorts (Const (_, T), Ss) = add_typ_sorts (T, Ss)
       
   283   | add_term_sorts (Free (_, T), Ss) = add_typ_sorts (T, Ss)
       
   284   | add_term_sorts (Var (_, T), Ss) = add_typ_sorts (T, Ss)
       
   285   | add_term_sorts (Bound _, Ss) = Ss
       
   286   | add_term_sorts (Abs (_, T, t), Ss) = add_term_sorts (t, add_typ_sorts (T, Ss))
       
   287   | add_term_sorts (t $ u, Ss) = add_term_sorts (t, add_term_sorts (u, Ss));
       
   288 
       
   289 fun add_terms_sorts ([], Ss) = Ss
       
   290   | add_terms_sorts (t :: ts, Ss) = add_terms_sorts (ts, add_term_sorts (t, Ss));
       
   291 
       
   292 fun add_thm_sorts (Thm {hyps, prop, ...}, Ss) =
       
   293   add_terms_sorts (hyps, add_term_sorts (prop, Ss));
       
   294 
       
   295 fun add_thms_shyps ([], Ss) = Ss
       
   296   | add_thms_shyps (Thm {shyps, ...} :: ths, Ss) =
       
   297       add_thms_shyps (ths, shyps union Ss);
       
   298 
       
   299 
       
   300 (*get 'dangling' sort constraints of a thm*)
       
   301 fun extra_shyps (th as Thm {shyps, ...}) =
       
   302   shyps \\ add_thm_sorts (th, []);
       
   303 
       
   304 
       
   305 (* fix_shyps *)
       
   306 
       
   307 (*preserve sort contexts of rule premises and substituted types*)
       
   308 fun fix_shyps thms Ts thm =
       
   309   let
       
   310     val Thm {sign, maxidx, hyps, prop, ...} = thm;
       
   311     val shyps =
       
   312       add_thm_sorts (thm, add_typs_sorts (Ts, add_thms_shyps (thms, [])));
       
   313   in
       
   314     Thm {sign = sign, maxidx = maxidx,
       
   315       shyps = shyps, hyps = hyps, prop = prop}
       
   316   end;
       
   317 
       
   318 fun env_codT (Envir.Envir {iTs, ...}) = map snd iTs;
       
   319 
       
   320 
       
   321 (* strip_shyps *)       (* FIXME improve? (e.g. only minimal extra sorts) *)
       
   322 
       
   323 val force_strip_shyps = ref true;  (* FIXME tmp *)
       
   324 
       
   325 (*remove extra sorts that are known to be syntactically non-empty*)
       
   326 fun strip_shyps thm =
       
   327   let
       
   328     val Thm {sign, maxidx, shyps, hyps, prop} = thm;
       
   329     val sorts = add_thm_sorts (thm, []);
       
   330     val maybe_empty = not o Sign.nonempty_sort sign sorts;
       
   331     val shyps' = filter (fn S => S mem sorts orelse maybe_empty S) shyps;
       
   332   in
       
   333     Thm {sign = sign, maxidx = maxidx,
       
   334       shyps =
       
   335        (if eq_set (shyps', sorts) orelse not (! force_strip_shyps) then shyps'
       
   336         else    (* FIXME tmp *)
       
   337          (writeln ("WARNING Removed sort hypotheses: " ^
       
   338            commas (map Type.str_of_sort (shyps' \\ sorts)));
       
   339            writeln "WARNING Let's hope these sorts are non-empty!";
       
   340            sorts)),
       
   341       hyps = hyps, prop = prop}
       
   342   end;
       
   343 
       
   344 
       
   345 (* implies_intr_shyps *)
       
   346 
       
   347 (*discharge all extra sort hypotheses*)
       
   348 fun implies_intr_shyps thm =
       
   349   (case extra_shyps thm of
       
   350     [] => thm
       
   351   | xshyps =>
       
   352       let
       
   353         val Thm {sign, maxidx, shyps, hyps, prop} = thm;
       
   354         val shyps' = logicS ins (shyps \\ xshyps);
       
   355         val used_names = foldr add_term_tfree_names (prop :: hyps, []);
       
   356         val names =
       
   357           tl (variantlist (replicate (length xshyps + 1) "'", used_names));
       
   358         val tfrees = map (TFree o rpair logicS) names;
       
   359 
       
   360         fun mk_insort (T, S) = map (Logic.mk_inclass o pair T) S;
       
   361         val sort_hyps = flat (map2 mk_insort (tfrees, xshyps));
       
   362       in
       
   363         Thm {sign = sign, maxidx = maxidx, shyps = shyps',
       
   364           hyps = hyps, prop = Logic.list_implies (sort_hyps, prop)}
       
   365       end);
   289 
   366 
   290 
   367 
   291 
   368 
   292 (*** Theories ***)
   369 (*** Theories ***)
   293 
   370 
   322 fun get_axiom theory name =
   399 fun get_axiom theory name =
   323   let
   400   let
   324     fun get_ax [] = raise Match
   401     fun get_ax [] = raise Match
   325       | get_ax (Theory {sign, new_axioms, parents} :: thys) =
   402       | get_ax (Theory {sign, new_axioms, parents} :: thys) =
   326           (case Symtab.lookup (new_axioms, name) of
   403           (case Symtab.lookup (new_axioms, name) of
   327             Some t => Thm {sign = sign, maxidx = maxidx_of_term t,
   404             Some t => fix_shyps [] []
   328               shyps = [], hyps = [], prop = t}
   405               (Thm {sign = sign, maxidx = maxidx_of_term t,
       
   406                 shyps = [], hyps = [], prop = t})
   329           | None => get_ax parents handle Match => get_ax thys);
   407           | None => get_ax parents handle Match => get_ax thys);
   330   in
   408   in
   331     get_ax [theory] handle Match
   409     get_ax [theory] handle Match
   332       => raise THEORY ("get_axiom: no axiom " ^ quote name, [theory])
   410       => raise THEORY ("get_axiom: no axiom " ^ quote name, [theory])
   333   end;
   411   end;
   460 
   538 
   461 
   539 
   462 
   540 
   463 (*** Meta rules ***)
   541 (*** Meta rules ***)
   464 
   542 
   465 (** sort contexts **)
       
   466 
       
   467 (*account for lost sort constraints*)
       
   468 fun fix_shyps ths Ts th = th;
       
   469 (* FIXME
       
   470   let
       
   471     fun thm_sorts (Thm {shyps, hyps, prop, ...}) =
       
   472       unions (shyps :: term_sorts prop :: map term_sorts hyps);
       
   473     val lost_sorts =
       
   474       unions (map thm_sorts ths @ map typ_sorts Ts) \\ thm_sorts th;
       
   475     val Thm {sign, maxidx, shyps, hyps, prop} = th;
       
   476   in
       
   477     Thm {sign = sign, maxidx = maxidx,
       
   478       shyps = lost_sorts @ shyps, hyps = hyps, prop = prop}
       
   479   end;
       
   480 *)
       
   481 (*remove sorts that are known to be non-empty (syntactically)*)
       
   482 val force_strip_shyps = ref true;  (* FIXME tmp *)
       
   483 fun strip_shyps th =
       
   484   let
       
   485     fun sort_hyps_of t =
       
   486       term_tfrees t @ map (apfst Syntax.string_of_vname) (term_tvars t);
       
   487 
       
   488     val Thm {sign, maxidx, shyps, hyps, prop} = th;
       
   489     (* FIXME no varnames (?) *)
       
   490     val sort_hyps = unions (sort_hyps_of prop :: map sort_hyps_of hyps);
       
   491     (* FIXME improve (e.g. only minimal sorts) *)
       
   492     val shyps' = filter_out (Sign.nonempty_sort sign sort_hyps) shyps;
       
   493   in
       
   494     Thm {sign = sign, maxidx = maxidx,
       
   495       shyps =
       
   496        (if null shyps' orelse not (! force_strip_shyps) then shyps'
       
   497         else	(* FIXME tmp *)
       
   498          (writeln ("WARNING Removed sort hypotheses: " ^
       
   499            commas (map Type.str_of_sort shyps'));
       
   500            writeln "WARNING Let's hope these sorts are non-empty!";
       
   501            [])),
       
   502       hyps = hyps, prop = prop}
       
   503   end;
       
   504 
       
   505 (*discharge all sort hypotheses*)
       
   506 fun implies_intr_shyps (th as Thm {shyps = [], ...}) = th
       
   507   | implies_intr_shyps (Thm {sign, maxidx, shyps, hyps, prop}) =
       
   508       let
       
   509         val used_names = foldr add_term_tfree_names (prop :: hyps, []);
       
   510         val names =
       
   511           tl (variantlist (replicate (length shyps + 1) "'", used_names));
       
   512         val tfrees = map (TFree o rpair logicS) names;
       
   513     
       
   514         fun mk_insort (T, S) = map (Logic.mk_inclass o pair T) S;
       
   515         val sort_hyps = flat (map2 mk_insort (tfrees, shyps));
       
   516       in
       
   517         Thm {sign = sign, maxidx = maxidx, shyps = [],
       
   518           hyps = hyps, prop = Logic.list_implies (sort_hyps, prop)}
       
   519       end;
       
   520 
       
   521 
       
   522 
       
   523 (** 'primitive' rules **)
   543 (** 'primitive' rules **)
   524 
   544 
   525 (*discharge all assumptions t from ts*)
   545 (*discharge all assumptions t from ts*)
   526 val disch = gen_rem (op aconv);
   546 val disch = gen_rem (op aconv);
   527 
   547 
   531   in  if T<>propT then
   551   in  if T<>propT then
   532         raise THM("assume: assumptions must have type prop", 0, [])
   552         raise THM("assume: assumptions must have type prop", 0, [])
   533       else if maxidx <> ~1 then
   553       else if maxidx <> ~1 then
   534         raise THM("assume: assumptions may not contain scheme variables",
   554         raise THM("assume: assumptions may not contain scheme variables",
   535                   maxidx, [])
   555                   maxidx, [])
   536       else Thm{sign = sign, maxidx = ~1, shyps = [], hyps = [prop], prop = prop}
   556       else fix_shyps [] []
       
   557         (Thm{sign = sign, maxidx = ~1, shyps = [], hyps = [prop], prop = prop})
   537   end;
   558   end;
   538 
   559 
   539 (*Implication introduction
   560 (*Implication introduction
   540   A |- B
   561   A |- B
   541   -------
   562   -------
   542   A ==> B
   563   A ==> B
   543 *)
   564 *)
   544 fun implies_intr cA (thB as Thm{sign,maxidx,shyps,hyps,prop}) : thm =
   565 fun implies_intr cA (thB as Thm{sign,maxidx,hyps,prop,...}) : thm =
   545   let val {sign=signA, t=A, T, maxidx=maxidxA} = rep_cterm cA
   566   let val {sign=signA, t=A, T, maxidx=maxidxA} = rep_cterm cA
   546   in  if T<>propT then
   567   in  if T<>propT then
   547         raise THM("implies_intr: assumptions must have type prop", 0, [thB])
   568         raise THM("implies_intr: assumptions must have type prop", 0, [thB])
   548       else Thm{sign= Sign.merge (sign,signA),  maxidx= max[maxidxA, maxidx],
   569       else fix_shyps [thB] []
   549              shyps = shyps, hyps= disch(hyps,A),  prop= implies$A$prop}
   570         (Thm{sign= Sign.merge (sign,signA),  maxidx= max[maxidxA, maxidx],
       
   571           shyps= [], hyps= disch(hyps,A),  prop= implies$A$prop})
   550       handle TERM _ =>
   572       handle TERM _ =>
   551         raise THM("implies_intr: incompatible signatures", 0, [thB])
   573         raise THM("implies_intr: incompatible signatures", 0, [thB])
   552   end;
   574   end;
   553 
   575 
   554 (*Implication elimination
   576 (*Implication elimination
   576 (*Forall introduction.  The Free or Var x must not be free in the hypotheses.
   598 (*Forall introduction.  The Free or Var x must not be free in the hypotheses.
   577     A
   599     A
   578   -----
   600   -----
   579   !!x.A
   601   !!x.A
   580 *)
   602 *)
   581 fun forall_intr cx (th as Thm{sign,maxidx,shyps,hyps,prop}) =
   603 fun forall_intr cx (th as Thm{sign,maxidx,hyps,prop,...}) =
   582   let val x = term_of cx;
   604   let val x = term_of cx;
   583       fun result(a,T) = Thm{sign= sign, maxidx= maxidx, shyps= shyps, hyps= hyps,
   605       fun result(a,T) = fix_shyps [th] []
   584                             prop= all(T) $ Abs(a, T, abstract_over (x,prop))}
   606         (Thm{sign= sign, maxidx= maxidx, shyps= [], hyps= hyps,
       
   607           prop= all(T) $ Abs(a, T, abstract_over (x,prop))})
   585   in  case x of
   608   in  case x of
   586         Free(a,T) =>
   609         Free(a,T) =>
   587           if exists (apl(x, Logic.occs)) hyps
   610           if exists (apl(x, Logic.occs)) hyps
   588           then  raise THM("forall_intr: variable free in assumptions", 0, [th])
   611           then  raise THM("forall_intr: variable free in assumptions", 0, [th])
   589           else  result(a,T)
   612           else  result(a,T)
   613 
   636 
   614 
   637 
   615 (* Equality *)
   638 (* Equality *)
   616 
   639 
   617 (* Definition of the relation =?= *)
   640 (* Definition of the relation =?= *)
   618 val flexpair_def =
   641 val flexpair_def = fix_shyps [] []
   619   Thm{sign= Sign.proto_pure, shyps= [], hyps= [], maxidx= 0,
   642   (Thm{sign= Sign.proto_pure, shyps= [], hyps= [], maxidx= 0,
   620       prop= term_of
   643         prop= term_of (read_cterm Sign.proto_pure
   621               (read_cterm Sign.proto_pure
   644                 ("(?t =?= ?u) == (?t == ?u::?'a::{})", propT))});
   622                  ("(?t =?= ?u) == (?t == ?u::?'a::{})", propT))};
       
   623 
   645 
   624 (*The reflexivity rule: maps  t   to the theorem   t==t   *)
   646 (*The reflexivity rule: maps  t   to the theorem   t==t   *)
   625 fun reflexive ct =
   647 fun reflexive ct =
   626   let val {sign, t, T, maxidx} = rep_cterm ct
   648   let val {sign, t, T, maxidx} = rep_cterm ct
   627   in  Thm{sign= sign, shyps= [], hyps= [], maxidx= maxidx,
   649   in  fix_shyps [] []
   628         prop= Logic.mk_equals(t,t)}
   650        (Thm{sign= sign, shyps= [], hyps= [], maxidx= maxidx,
       
   651          prop= Logic.mk_equals(t,t)})
   629   end;
   652   end;
   630 
   653 
   631 (*The symmetry rule
   654 (*The symmetry rule
   632   t==u
   655   t==u
   633   ----
   656   ----
   634   u==t
   657   u==t
   635 *)
   658 *)
   636 fun symmetric (th as Thm{sign,shyps,hyps,prop,maxidx}) =
   659 fun symmetric (th as Thm{sign,shyps,hyps,prop,maxidx}) =
   637   case prop of
   660   case prop of
   638       (eq as Const("==",_)) $ t $ u =>
   661       (eq as Const("==",_)) $ t $ u =>
   639           Thm{sign=sign, shyps=shyps, hyps=hyps, maxidx=maxidx, prop= eq$u$t}
   662         (*no fix_shyps*)
       
   663         Thm{sign=sign, shyps=shyps, hyps=hyps, maxidx=maxidx, prop= eq$u$t}
   640     | _ => raise THM("symmetric", 0, [th]);
   664     | _ => raise THM("symmetric", 0, [th]);
   641 
   665 
   642 (*The transitive rule
   666 (*The transitive rule
   643   t1==u    u==t2
   667   t1==u    u==t2
   644   --------------
   668   --------------
   660 
   684 
   661 (*Beta-conversion: maps (%x.t)(u) to the theorem (%x.t)(u) == t[u/x] *)
   685 (*Beta-conversion: maps (%x.t)(u) to the theorem (%x.t)(u) == t[u/x] *)
   662 fun beta_conversion ct =
   686 fun beta_conversion ct =
   663   let val {sign, t, T, maxidx} = rep_cterm ct
   687   let val {sign, t, T, maxidx} = rep_cterm ct
   664   in  case t of
   688   in  case t of
   665           Abs(_,_,bodt) $ u =>
   689           Abs(_,_,bodt) $ u => fix_shyps [] []
   666             Thm{sign= sign,  shyps= [], hyps= [],
   690             (Thm{sign= sign,  shyps= [], hyps= [],
   667                 maxidx= maxidx_of_term t,
   691                 maxidx= maxidx_of_term t,
   668                 prop= Logic.mk_equals(t, subst_bounds([u],bodt))}
   692                 prop= Logic.mk_equals(t, subst_bounds([u],bodt))})
   669         | _ =>  raise THM("beta_conversion: not a redex", 0, [])
   693         | _ =>  raise THM("beta_conversion: not a redex", 0, [])
   670   end;
   694   end;
   671 
   695 
   672 (*The extensionality rule   (proviso: x not free in f, g, or hypotheses)
   696 (*The extensionality rule   (proviso: x not free in f, g, or hypotheses)
   673   f(x) == g(x)
   697   f(x) == g(x)
   685                   then err"variable free in hyps or functions"    else  ()
   709                   then err"variable free in hyps or functions"    else  ()
   686               | Var _ =>
   710               | Var _ =>
   687                   if Logic.occs(y,f)  orelse  Logic.occs(y,g)
   711                   if Logic.occs(y,f)  orelse  Logic.occs(y,g)
   688                   then err"variable free in functions"   else  ()
   712                   then err"variable free in functions"   else  ()
   689               | _ => err"not a variable");
   713               | _ => err"not a variable");
       
   714           (*no fix_shyps*)
   690           Thm{sign=sign, shyps=shyps, hyps=hyps, maxidx=maxidx,
   715           Thm{sign=sign, shyps=shyps, hyps=hyps, maxidx=maxidx,
   691               prop= Logic.mk_equals(f,g)}
   716               prop= Logic.mk_equals(f,g)}
   692       end
   717       end
   693  | _ =>  raise THM("extensional: premise", 0, [th]);
   718  | _ =>  raise THM("extensional: premise", 0, [th]);
   694 
   719 
   696   The bound variable will be named "a" (since x will be something like x320)
   721   The bound variable will be named "a" (since x will be something like x320)
   697      t == u
   722      t == u
   698   ------------
   723   ------------
   699   %x.t == %x.u
   724   %x.t == %x.u
   700 *)
   725 *)
   701 fun abstract_rule a cx (th as Thm{sign,maxidx,shyps,hyps,prop}) =
   726 fun abstract_rule a cx (th as Thm{sign,maxidx,hyps,prop,...}) =
   702   let val x = term_of cx;
   727   let val x = term_of cx;
   703       val (t,u) = Logic.dest_equals prop
   728       val (t,u) = Logic.dest_equals prop
   704             handle TERM _ =>
   729             handle TERM _ =>
   705                 raise THM("abstract_rule: premise not an equality", 0, [th])
   730                 raise THM("abstract_rule: premise not an equality", 0, [th])
   706       fun result T =
   731       fun result T = fix_shyps [th] []
   707             Thm{sign= sign, maxidx= maxidx, shyps= shyps, hyps= hyps,
   732             (Thm{sign= sign, maxidx= maxidx, shyps= [], hyps= hyps,
   708                 prop= Logic.mk_equals(Abs(a, T, abstract_over (x,t)),
   733                 prop= Logic.mk_equals(Abs(a, T, abstract_over (x,t)),
   709                                       Abs(a, T, abstract_over (x,u)))}
   734                                       Abs(a, T, abstract_over (x,u)))})
   710   in  case x of
   735   in  case x of
   711         Free(_,T) =>
   736         Free(_,T) =>
   712          if exists (apl(x, Logic.occs)) hyps
   737          if exists (apl(x, Logic.occs)) hyps
   713          then raise THM("abstract_rule: variable free in assumptions", 0, [th])
   738          then raise THM("abstract_rule: variable free in assumptions", 0, [th])
   714          else result T
   739          else result T
   724 fun combination th1 th2 =
   749 fun combination th1 th2 =
   725   let val Thm{maxidx=max1, shyps=shyps1, hyps=hyps1, prop=prop1,...} = th1
   750   let val Thm{maxidx=max1, shyps=shyps1, hyps=hyps1, prop=prop1,...} = th1
   726       and Thm{maxidx=max2, shyps=shyps2, hyps=hyps2, prop=prop2,...} = th2
   751       and Thm{maxidx=max2, shyps=shyps2, hyps=hyps2, prop=prop2,...} = th2
   727   in  case (prop1,prop2)  of
   752   in  case (prop1,prop2)  of
   728        (Const("==",_) $ f $ g, Const("==",_) $ t $ u) =>
   753        (Const("==",_) $ f $ g, Const("==",_) $ t $ u) =>
       
   754               (*no fix_shyps*)
   729               Thm{sign= merge_thm_sgs(th1,th2), shyps= shyps1 union shyps2,
   755               Thm{sign= merge_thm_sgs(th1,th2), shyps= shyps1 union shyps2,
   730                   hyps= hyps1 union hyps2,
   756                   hyps= hyps1 union hyps2,
   731                   maxidx= max[max1,max2], prop= Logic.mk_equals(f$t, g$u)}
   757                   maxidx= max[max1,max2], prop= Logic.mk_equals(f$t, g$u)}
   732      | _ =>  raise THM("combination: premises", 0, [th1,th2])
   758      | _ =>  raise THM("combination: premises", 0, [th1,th2])
   733   end;
   759   end;
   763     and Thm{maxidx=max2, shyps=shyps2, hyps=hyps2, prop=prop2,...} = th2;
   789     and Thm{maxidx=max2, shyps=shyps2, hyps=hyps2, prop=prop2,...} = th2;
   764     fun err(msg) = raise THM("equal_intr: "^msg, 0, [th1,th2])
   790     fun err(msg) = raise THM("equal_intr: "^msg, 0, [th1,th2])
   765 in case (prop1,prop2) of
   791 in case (prop1,prop2) of
   766      (Const("==>",_) $ A $ B, Const("==>",_) $ B' $ A')  =>
   792      (Const("==>",_) $ A $ B, Const("==>",_) $ B' $ A')  =>
   767         if A aconv A' andalso B aconv B'
   793         if A aconv A' andalso B aconv B'
   768         then Thm{sign= merge_thm_sgs(th1,th2), shyps= shyps1 union shyps2,
   794         then
   769                  hyps= hyps1 union hyps2,
   795           (*no fix_shyps*)
   770                  maxidx= max[max1,max2], prop= Logic.mk_equals(A,B)}
   796           Thm{sign= merge_thm_sgs(th1,th2), shyps= shyps1 union shyps2,
       
   797                 hyps= hyps1 union hyps2,
       
   798                 maxidx= max[max1,max2], prop= Logic.mk_equals(A,B)}
   771         else err"not equal"
   799         else err"not equal"
   772    | _ =>  err"premises"
   800    | _ =>  err"premises"
   773 end;
   801 end;
   774 
   802 
   775 
   803 
   777 (**** Derived rules ****)
   805 (**** Derived rules ****)
   778 
   806 
   779 (*Discharge all hypotheses (need not verify cterms)
   807 (*Discharge all hypotheses (need not verify cterms)
   780   Repeated hypotheses are discharged only once;  fold cannot do this*)
   808   Repeated hypotheses are discharged only once;  fold cannot do this*)
   781 fun implies_intr_hyps (Thm{sign, maxidx, shyps, hyps=A::As, prop}) =
   809 fun implies_intr_hyps (Thm{sign, maxidx, shyps, hyps=A::As, prop}) =
   782       implies_intr_hyps
   810       implies_intr_hyps (*no fix_shyps*)
   783             (Thm{sign=sign,  maxidx=maxidx, shyps=shyps,
   811             (Thm{sign=sign,  maxidx=maxidx, shyps=shyps,
   784                  hyps= disch(As,A),  prop= implies$A$prop})
   812                  hyps= disch(As,A),  prop= implies$A$prop})
   785   | implies_intr_hyps th = th;
   813   | implies_intr_hyps th = th;
   786 
   814 
   787 (*Smash" unifies the list of term pairs leaving no flex-flex pairs.
   815 (*Smash" unifies the list of term pairs leaving no flex-flex pairs.
   862   A can contain Vars, not so for assume!   *)
   890   A can contain Vars, not so for assume!   *)
   863 fun trivial ct : thm =
   891 fun trivial ct : thm =
   864   let val {sign, t=A, T, maxidx} = rep_cterm ct
   892   let val {sign, t=A, T, maxidx} = rep_cterm ct
   865   in  if T<>propT then
   893   in  if T<>propT then
   866             raise THM("trivial: the term must have type prop", 0, [])
   894             raise THM("trivial: the term must have type prop", 0, [])
   867       else Thm{sign= sign, maxidx= maxidx, shyps= [], hyps= [],
   895       else fix_shyps [] []
   868              prop= implies$A$A}
   896         (Thm{sign= sign, maxidx= maxidx, shyps= [], hyps= [],
       
   897               prop= implies$A$A})
   869   end;
   898   end;
   870 
   899 
   871 (*Axiom-scheme reflecting signature contents: "OFCLASS(?'a::c, c_class)" --
   900 (*Axiom-scheme reflecting signature contents: "OFCLASS(?'a::c, c_class)" --
   872   essentially just an instance of A==>A.*)
   901   essentially just an instance of A==>A.*)
   873 fun class_triv thy c =
   902 fun class_triv thy c =
   875     val sign = sign_of thy;
   904     val sign = sign_of thy;
   876     val Cterm {t, maxidx, ...} =
   905     val Cterm {t, maxidx, ...} =
   877       cterm_of sign (Logic.mk_inclass (TVar (("'a", 0), [c]), c))
   906       cterm_of sign (Logic.mk_inclass (TVar (("'a", 0), [c]), c))
   878         handle TERM (msg, _) => raise THM ("class_triv: " ^ msg, 0, []);
   907         handle TERM (msg, _) => raise THM ("class_triv: " ^ msg, 0, []);
   879   in
   908   in
   880     Thm {sign = sign, maxidx = maxidx, shyps = [], hyps = [], prop = t}
   909     fix_shyps [] []
       
   910       (Thm {sign = sign, maxidx = maxidx, shyps = [], hyps = [], prop = t})
   881   end;
   911   end;
   882 
   912 
   883 
   913 
   884 (* Replace all TFrees not in the hyps by new TVars *)
   914 (* Replace all TFrees not in the hyps by new TVars *)
   885 fun varifyT(Thm{sign,maxidx,shyps,hyps,prop}) =
   915 fun varifyT(Thm{sign,maxidx,shyps,hyps,prop}) =
   886   let val tfrees = foldr add_term_tfree_names (hyps,[])
   916   let val tfrees = foldr add_term_tfree_names (hyps,[])
   887   in Thm{sign=sign, maxidx=max[0,maxidx], shyps=shyps, hyps=hyps,
   917   in (*no fix_shyps*)
   888          prop= Type.varify(prop,tfrees)}
   918     Thm{sign=sign, maxidx=max[0,maxidx], shyps=shyps, hyps=hyps,
       
   919         prop= Type.varify(prop,tfrees)}
   889   end;
   920   end;
   890 
   921 
   891 (* Replace all TVars by new TFrees *)
   922 (* Replace all TVars by new TFrees *)
   892 fun freezeT(Thm{sign,maxidx,shyps,hyps,prop}) =
   923 fun freezeT(Thm{sign,maxidx,shyps,hyps,prop}) =
   893   let val prop' = Type.freeze prop
   924   let val prop' = Type.freeze prop
   894   in Thm{sign=sign, maxidx=maxidx_of_term prop', shyps=shyps, hyps=hyps,
   925   in (*no fix_shyps*)
   895        prop=prop'}
   926     Thm{sign=sign, maxidx=maxidx_of_term prop', shyps=shyps, hyps=hyps,
       
   927         prop=prop'}
   896   end;
   928   end;
   897 
   929 
   898 
   930 
   899 (*** Inference rules for tactics ***)
   931 (*** Inference rules for tactics ***)
   900 
   932 
   908   handle TERM _ => raise THM("dest_state", i, [state]);
   940   handle TERM _ => raise THM("dest_state", i, [state]);
   909 
   941 
   910 (*Increment variables and parameters of orule as required for
   942 (*Increment variables and parameters of orule as required for
   911   resolution with goal i of state. *)
   943   resolution with goal i of state. *)
   912 fun lift_rule (state, i) orule =
   944 fun lift_rule (state, i) orule =
   913   let val Thm{prop=sprop,maxidx=smax,...} = state;
   945   let val Thm{shyps=sshyps,prop=sprop,maxidx=smax,...} = state;
   914       val (Bi::_, _) = Logic.strip_prems(i, [], Logic.skip_flexpairs sprop)
   946       val (Bi::_, _) = Logic.strip_prems(i, [], Logic.skip_flexpairs sprop)
   915         handle TERM _ => raise THM("lift_rule", i, [orule,state]);
   947         handle TERM _ => raise THM("lift_rule", i, [orule,state]);
   916       val (lift_abs,lift_all) = Logic.lift_fns(Bi,smax+1);
   948       val (lift_abs,lift_all) = Logic.lift_fns(Bi,smax+1);
   917       val (Thm{sign,maxidx,hyps,prop,...}) = orule
   949       val (Thm{sign,maxidx,shyps,hyps,prop}) = orule
   918       val (tpairs,As,B) = Logic.strip_horn prop
   950       val (tpairs,As,B) = Logic.strip_horn prop
   919   in  fix_shyps [state, orule] []
   951   in  (*no fix_shyps*)
   920         (Thm{hyps=hyps, sign= merge_thm_sgs(state,orule),
   952       Thm{hyps=hyps, sign= merge_thm_sgs(state,orule),
   921           shyps=[], maxidx= maxidx+smax+1,
   953           shyps=sshyps union shyps, maxidx= maxidx+smax+1,
   922           prop= Logic.rule_of(map (pairself lift_abs) tpairs,
   954           prop= Logic.rule_of(map (pairself lift_abs) tpairs,
   923                               map lift_all As,    lift_all B)})
   955                               map lift_all As,    lift_all B)}
   924   end;
   956   end;
   925 
   957 
   926 (*Solve subgoal Bi of proof state B1...Bn/C by assumption. *)
   958 (*Solve subgoal Bi of proof state B1...Bn/C by assumption. *)
   927 fun assumption i state =
   959 fun assumption i state =
   928   let val Thm{sign,maxidx,hyps,prop,...} = state;
   960   let val Thm{sign,maxidx,hyps,prop,...} = state;
   982 
  1014 
   983 (*** Preservation of bound variable names ***)
  1015 (*** Preservation of bound variable names ***)
   984 
  1016 
   985 (*Scan a pair of terms; while they are similar,
  1017 (*Scan a pair of terms; while they are similar,
   986   accumulate corresponding bound vars in "al"*)
  1018   accumulate corresponding bound vars in "al"*)
   987 fun match_bvs(Abs(x,_,s),Abs(y,_,t), al) = 
  1019 fun match_bvs(Abs(x,_,s),Abs(y,_,t), al) =
   988       match_bvs(s, t, if x="" orelse y="" then al
  1020       match_bvs(s, t, if x="" orelse y="" then al
   989 		                          else (x,y)::al)
  1021                                           else (x,y)::al)
   990   | match_bvs(f$s, g$t, al) = match_bvs(f,g,match_bvs(s,t,al))
  1022   | match_bvs(f$s, g$t, al) = match_bvs(f,g,match_bvs(s,t,al))
   991   | match_bvs(_,_,al) = al;
  1023   | match_bvs(_,_,al) = al;
   992 
  1024 
   993 (* strip abstractions created by parameters *)
  1025 (* strip abstractions created by parameters *)
   994 fun match_bvars((s,t),al) = match_bvs(strip_abs_body s, strip_abs_body t, al);
  1026 fun match_bvars((s,t),al) = match_bvs(strip_abs_body s, strip_abs_body t, al);
  1048 
  1080 
  1049 (*Faster normalization: skip assumptions that were lifted over*)
  1081 (*Faster normalization: skip assumptions that were lifted over*)
  1050 fun norm_term_skip env 0 t = Envir.norm_term env t
  1082 fun norm_term_skip env 0 t = Envir.norm_term env t
  1051   | norm_term_skip env n (Const("all",_)$Abs(a,T,t)) =
  1083   | norm_term_skip env n (Const("all",_)$Abs(a,T,t)) =
  1052         let val Envir.Envir{iTs, ...} = env
  1084         let val Envir.Envir{iTs, ...} = env
  1053 	    val T' = typ_subst_TVars iTs T
  1085             val T' = typ_subst_TVars iTs T
  1054 	    (*Must instantiate types of parameters because they are flattened;
  1086             (*Must instantiate types of parameters because they are flattened;
  1055               this could be a NEW parameter*)
  1087               this could be a NEW parameter*)
  1056         in  all T' $ Abs(a, T', norm_term_skip env n t)  end
  1088         in  all T' $ Abs(a, T', norm_term_skip env n t)  end
  1057   | norm_term_skip env n (Const("==>", _) $ A $ B) =
  1089   | norm_term_skip env n (Const("==>", _) $ A $ B) =
  1058 	implies $ A $ norm_term_skip env (n-1) B
  1090         implies $ A $ norm_term_skip env (n-1) B
  1059   | norm_term_skip env n t = error"norm_term_skip: too few assumptions??";
  1091   | norm_term_skip env n t = error"norm_term_skip: too few assumptions??";
  1060 
  1092 
  1061 
  1093 
  1062 (*Composition of object rule r=(A1...Am/B) with proof state s=(B1...Bn/C)
  1094 (*Composition of object rule r=(A1...Am/B) with proof state s=(B1...Bn/C)
  1063   Unifies B with Bi, replacing subgoal i    (1 <= i <= n)
  1095   Unifies B with Bi, replacing subgoal i    (1 <= i <= n)
  1071 in
  1103 in
  1072 fun bicompose_aux match (state, (stpairs, Bs, Bi, C), lifted)
  1104 fun bicompose_aux match (state, (stpairs, Bs, Bi, C), lifted)
  1073                         (eres_flg, orule, nsubgoal) =
  1105                         (eres_flg, orule, nsubgoal) =
  1074  let val Thm{maxidx=smax, hyps=shyps, ...} = state
  1106  let val Thm{maxidx=smax, hyps=shyps, ...} = state
  1075      and Thm{maxidx=rmax, hyps=rhyps, prop=rprop,...} = orule
  1107      and Thm{maxidx=rmax, hyps=rhyps, prop=rprop,...} = orule
  1076 	     (*How many hyps to skip over during normalization*)
  1108              (*How many hyps to skip over during normalization*)
  1077      and nlift = Logic.count_prems(strip_all_body Bi, 
  1109      and nlift = Logic.count_prems(strip_all_body Bi,
  1078 				   if eres_flg then ~1 else 0)
  1110                                    if eres_flg then ~1 else 0)
  1079      val sign = merge_thm_sgs(state,orule);
  1111      val sign = merge_thm_sgs(state,orule);
  1080      (** Add new theorem with prop = '[| Bs; As |] ==> C' to thq **)
  1112      (** Add new theorem with prop = '[| Bs; As |] ==> C' to thq **)
  1081      fun addth As ((env as Envir.Envir {maxidx, ...}, tpairs), thq) =
  1113      fun addth As ((env as Envir.Envir {maxidx, ...}, tpairs), thq) =
  1082        let val normt = Envir.norm_term env;
  1114        let val normt = Envir.norm_term env;
  1083            (*perform minimal copying here by examining env*)
  1115            (*perform minimal copying here by examining env*)
  1084            val normp =
  1116            val normp =
  1085              if Envir.is_empty env then (tpairs, Bs @ As, C)
  1117              if Envir.is_empty env then (tpairs, Bs @ As, C)
  1086              else
  1118              else
  1087              let val ntps = map (pairself normt) tpairs
  1119              let val ntps = map (pairself normt) tpairs
  1088              in if the (Envir.minidx env) > smax then 
  1120              in if the (Envir.minidx env) > smax then
  1089 		  (*no assignments in state; normalize the rule only*)
  1121                   (*no assignments in state; normalize the rule only*)
  1090                   if lifted 
  1122                   if lifted
  1091 		  then (ntps, Bs @ map (norm_term_skip env nlift) As, C)
  1123                   then (ntps, Bs @ map (norm_term_skip env nlift) As, C)
  1092 		  else (ntps, Bs @ map normt As, C)
  1124                   else (ntps, Bs @ map normt As, C)
  1093                 else if match then raise Bicompose
  1125                 else if match then raise Bicompose
  1094                 else (*normalize the new rule fully*)
  1126                 else (*normalize the new rule fully*)
  1095                   (ntps, map normt (Bs @ As), normt C)
  1127                   (ntps, map normt (Bs @ As), normt C)
  1096              end
  1128              end
  1097            val th =
  1129            val th = (* FIXME improve *)
  1098              fix_shyps [state, orule] (env_codT env)
  1130              fix_shyps [state, orule] (env_codT env)
  1099                (Thm{sign=sign, shyps=[], hyps=rhyps union shyps,
  1131                (Thm{sign=sign, shyps=[], hyps=rhyps union shyps,
  1100                  maxidx=maxidx, prop= Logic.rule_of normp})
  1132                  maxidx=maxidx, prop= Logic.rule_of normp})
  1101         in  cons(th, thq)  end  handle Bicompose => thq
  1133         in  cons(th, thq)  end  handle Bicompose => thq
  1102      val (rtpairs,rhorn) = Logic.strip_flexpairs(rprop);
  1134      val (rtpairs,rhorn) = Logic.strip_flexpairs(rprop);
  1215    conditional rewrites with extra variables in the conditions may terminate
  1247    conditional rewrites with extra variables in the conditions may terminate
  1216    although the rhs is an instance of the lhs. Example:
  1248    although the rhs is an instance of the lhs. Example:
  1217    ?m < ?n ==> f(?n) == f(?m)
  1249    ?m < ?n ==> f(?n) == f(?m)
  1218 *)
  1250 *)
  1219 
  1251 
  1220 fun mk_rrule (thm as Thm{sign,prop,maxidx,...}) =
  1252 fun mk_rrule raw_thm =
  1221   let val prems = Logic.strip_imp_prems prop
  1253   let
       
  1254       val thm = strip_shyps raw_thm;            (* FIXME tmp *)
       
  1255       val Thm{sign,prop,maxidx,...} = thm;
       
  1256       val prems = Logic.strip_imp_prems prop
  1222       val concl = Logic.strip_imp_concl prop
  1257       val concl = Logic.strip_imp_concl prop
  1223       val (lhs,_) = Logic.dest_equals concl handle TERM _ =>
  1258       val (lhs,_) = Logic.dest_equals concl handle TERM _ =>
  1224                       raise SIMPLIFIER("Rewrite rule not a meta-equality",thm)
  1259                       raise SIMPLIFIER("Rewrite rule not a meta-equality",thm)
  1225       val econcl = Pattern.eta_contract concl
  1260       val econcl = Pattern.eta_contract concl
  1226       val (elhs,erhs) = Logic.dest_equals econcl
  1261       val (elhs,erhs) = Logic.dest_equals econcl
  1227       val perm = var_perm(elhs,erhs) andalso not(elhs aconv erhs)
  1262       val perm = var_perm(elhs,erhs) andalso not(elhs aconv erhs)
  1228                                      andalso not(is_Var(elhs))
  1263                                      andalso not(is_Var(elhs))
  1229   in
  1264   in
  1230      if not (null (#shyps (rep_thm (strip_shyps thm)))) then     (* FIXME tmp hack *)
  1265      if not (null (extra_shyps thm)) then     (* FIXME tmp *)
  1231        raise SIMPLIFIER ("Rewrite rule may not contain sort hypotheses", thm)
  1266        raise SIMPLIFIER ("Rewrite rule may not contain extra sort hypotheses", thm)
  1232      else if not perm andalso loops sign prems (elhs,erhs) then
  1267      else if not perm andalso loops sign prems (elhs,erhs) then
  1233        (prtm "Warning: ignoring looping rewrite rule" sign prop; None)
  1268        (prtm "Warning: ignoring looping rewrite rule" sign prop; None)
  1234      else Some{thm=thm,lhs=lhs,perm=perm}
  1269      else Some{thm=thm,lhs=lhs,perm=perm}
  1235   end;
  1270   end;
  1236 
  1271 
  1499         let val (hyps1,_,s1) = if simprem then try_botc mss (hyps,maxidx,s)
  1534         let val (hyps1,_,s1) = if simprem then try_botc mss (hyps,maxidx,s)
  1500                                else (hyps,0,s);
  1535                                else (hyps,0,s);
  1501             val maxidx1 = maxidx_of_term s1
  1536             val maxidx1 = maxidx_of_term s1
  1502             val mss1 =
  1537             val mss1 =
  1503               if not useprem orelse maxidx1 <> ~1 then mss
  1538               if not useprem orelse maxidx1 <> ~1 then mss
  1504               else let val thm = Thm{sign=sign,shyps=[],hyps=[s1],prop=s1,maxidx= ~1}
  1539               else let val thm = fix_shyps [] []        (* FIXME ??? *)
       
  1540                      (Thm{sign=sign,shyps=[],hyps=[s1],prop=s1,maxidx= ~1})
  1505                    in add_simps(add_prems(mss,[thm]), mk_rews thm) end
  1541                    in add_simps(add_prems(mss,[thm]), mk_rews thm) end
  1506             val (hyps2,maxidx2,u1) = try_botc mss1 (hyps1,maxidx,u)
  1542             val (hyps2,maxidx2,u1) = try_botc mss1 (hyps1,maxidx,u)
  1507             val hyps3 = if s1 mem hyps1 then hyps2 else hyps2\s1
  1543             val hyps3 = if s1 mem hyps1 then hyps2 else hyps2\s1
  1508         in (hyps3, max[maxidx1,maxidx2], Logic.mk_implies(s1,u1)) end
  1544         in (hyps3, max[maxidx1,maxidx2], Logic.mk_implies(s1,u1)) end
  1509 
  1545 
  1520 (*** FIXME: check that #bounds(mss) does not "occur" in ct alread ***)
  1556 (*** FIXME: check that #bounds(mss) does not "occur" in ct alread ***)
  1521 fun rewrite_cterm mode mss prover ct =
  1557 fun rewrite_cterm mode mss prover ct =
  1522   let val {sign, t, T, maxidx} = rep_cterm ct;
  1558   let val {sign, t, T, maxidx} = rep_cterm ct;
  1523       val (hyps,maxidxu,u) = bottomc (mode,prover,sign) mss ([],maxidx,t);
  1559       val (hyps,maxidxu,u) = bottomc (mode,prover,sign) mss ([],maxidx,t);
  1524       val prop = Logic.mk_equals(t,u)
  1560       val prop = Logic.mk_equals(t,u)
  1525   in  Thm{sign= sign, shyps=[], hyps= hyps, maxidx= max[maxidx,maxidxu],
  1561   in  fix_shyps [] []
  1526         prop= prop}
  1562        (Thm{sign= sign, shyps=[], hyps= hyps, maxidx= max[maxidx,maxidxu],
       
  1563          prop= prop})
  1527   end
  1564   end
  1528 
  1565 
  1529 end;
  1566 end;
  1530