src/Pure/Isar/locale.ML
changeset 16861 7446b4be013b
parent 16850 35e07087aba2
child 16947 c6a90f04924e
equal deleted inserted replaced
16860:43abdba4da5c 16861:7446b4be013b
   621 
   621 
   622 fun rename_thm ren th =
   622 fun rename_thm ren th =
   623   let
   623   let
   624     val {thy, hyps, prop, maxidx, ...} = Thm.rep_thm th;
   624     val {thy, hyps, prop, maxidx, ...} = Thm.rep_thm th;
   625     val cert = Thm.cterm_of thy;
   625     val cert = Thm.cterm_of thy;
   626     val (xs, Ts) = Library.split_list (Library.foldl Term.add_frees ([], prop :: hyps));
   626     val (xs, Ts) = Library.split_list (fold Term.add_frees (prop :: hyps) []);
   627     val xs' = map (rename ren) xs;
   627     val xs' = map (rename ren) xs;
   628     fun cert_frees names = map (cert o Free) (names ~~ Ts);
   628     fun cert_frees names = map (cert o Free) (names ~~ Ts);
   629     fun cert_vars names = map (cert o Var o apfst (rpair (maxidx + 1))) (names ~~ Ts);
   629     fun cert_vars names = map (cert o Var o apfst (rpair (maxidx + 1))) (names ~~ Ts);
   630   in
   630   in
   631     if xs = xs' then th
   631     if xs = xs' then th
   703 (* CB: frozen_tvars has the following type:
   703 (* CB: frozen_tvars has the following type:
   704   ProofContext.context -> Term.typ list -> (Term.indexname * (sort * Term.typ)) list *)
   704   ProofContext.context -> Term.typ list -> (Term.indexname * (sort * Term.typ)) list *)
   705 
   705 
   706 fun frozen_tvars ctxt Ts =
   706 fun frozen_tvars ctxt Ts =
   707   let
   707   let
   708     val tvars = rev (Library.foldl Term.add_tvarsT ([], Ts));
   708     val tvars = rev (fold Term.add_tvarsT Ts []);
   709     val tfrees = map TFree
   709     val tfrees = map TFree
   710       (Term.invent_names (ProofContext.used_types ctxt) "'a" (length tvars) ~~ map #2 tvars);
   710       (Term.invent_names (ProofContext.used_types ctxt) "'a" (length tvars) ~~ map #2 tvars);
   711   in map (fn ((x, S), y) => (x, (S, y))) (tvars ~~ tfrees) end;
   711   in map (fn ((x, S), y) => (x, (S, y))) (tvars ~~ tfrees) end;
   712 
   712 
   713 fun unify_frozen ctxt maxidx Ts Us =
   713 fun unify_frozen ctxt maxidx Ts Us =
   949     val elemss' = map (fn (((name, _), (ps, axs)), elems) =>
   949     val elemss' = map (fn (((name, _), (ps, axs)), elems) =>
   950          (((name, map (fn p => (p, assoc (all_params, p))) ps), axs), elems))
   950          (((name, map (fn p => (p, assoc (all_params, p))) ps), axs), elems))
   951          elemss;
   951          elemss;
   952     fun inst_ax th = let
   952     fun inst_ax th = let
   953          val {hyps, prop, ...} = Thm.rep_thm th;
   953          val {hyps, prop, ...} = Thm.rep_thm th;
   954          val ps = map (apsnd SOME) (Library.foldl Term.add_frees ([], prop :: hyps));
   954          val ps = map (apsnd SOME) (fold Term.add_frees (prop :: hyps) []);
   955          val [env] = unify_parms ctxt all_params [ps];
   955          val [env] = unify_parms ctxt all_params [ps];
   956          val th' = inst_thm ctxt env th;
   956          val th' = inst_thm ctxt env th;
   957        in th' end;
   957        in th' end;
   958     val final_elemss = map (fn ((id, axs), elems) =>
   958     val final_elemss = map (fn ((id, axs), elems) =>
   959          ((id, map inst_ax axs), elems)) elemss';
   959          ((id, map inst_ax axs), elems)) elemss';
  1206   in
  1206   in
  1207     conditional (exists (equal y o #1) xs) (fn () =>
  1207     conditional (exists (equal y o #1) xs) (fn () =>
  1208       err "Attempt to define previously specified variable");
  1208       err "Attempt to define previously specified variable");
  1209     conditional (exists (fn (Free (y', _), _) => y = y' | _ => false) env) (fn () =>
  1209     conditional (exists (fn (Free (y', _), _) => y = y' | _ => false) env) (fn () =>
  1210       err "Attempt to redefine variable");
  1210       err "Attempt to redefine variable");
  1211     (Term.add_frees (xs, b'), (Free (y, T), b') :: env, th :: ths)
  1211     (Term.add_frees b' xs, (Free (y, T), b') :: env, th :: ths)
  1212   end;
  1212   end;
  1213 
  1213 
  1214 (* CB: for finish_elems (Int and Ext) *)
  1214 (* CB: for finish_elems (Int and Ext) *)
  1215 
  1215 
  1216 fun eval_text _ _ _ (text, Fixes _) = text
  1216 fun eval_text _ _ _ (text, Fixes _) = text
  1220         val ts = List.concat (map (map #1 o #2) asms);
  1220         val ts = List.concat (map (map #1 o #2) asms);
  1221         val ts' = map (norm_term env) ts;
  1221         val ts' = map (norm_term env) ts;
  1222         val spec' =
  1222         val spec' =
  1223           if is_ext then ((exts @ ts, exts' @ ts'), (ints, ints'))
  1223           if is_ext then ((exts @ ts, exts' @ ts'), (ints, ints'))
  1224           else ((exts, exts'), (ints @ ts, ints' @ ts'));
  1224           else ((exts, exts'), (ints @ ts, ints' @ ts'));
  1225       in (spec', (Library.foldl Term.add_frees (xs, ts'), env, defs)) end
  1225       in (spec', (fold Term.add_frees ts' xs, env, defs)) end
  1226   | eval_text ctxt (id, _) _ ((spec, binds), Defines defs) =
  1226   | eval_text ctxt (id, _) _ ((spec, binds), Defines defs) =
  1227       (spec, Library.foldl (bind_def ctxt id) (binds, map (#1 o #2) defs))
  1227       (spec, Library.foldl (bind_def ctxt id) (binds, map (#1 o #2) defs))
  1228   | eval_text _ _ _ (text, Notes _) = text;
  1228   | eval_text _ _ _ (text, Notes _) = text;
  1229 
  1229 
  1230 (* CB: for finish_elems (Ext) *)
  1230 (* CB: for finish_elems (Ext) *)
  1232 fun closeup _ false elem = elem
  1232 fun closeup _ false elem = elem
  1233   | closeup ctxt true elem =
  1233   | closeup ctxt true elem =
  1234       let
  1234       let
  1235         fun close_frees t =
  1235         fun close_frees t =
  1236           let val frees = rev (filter_out (ProofContext.is_fixed ctxt o #1)
  1236           let val frees = rev (filter_out (ProofContext.is_fixed ctxt o #1)
  1237             (Term.add_frees ([], t)))
  1237             (Term.add_frees t []))
  1238           in Term.list_all_free (frees, t) end;
  1238           in Term.list_all_free (frees, t) end;
  1239 
  1239 
  1240         fun no_binds [] = []
  1240         fun no_binds [] = []
  1241           | no_binds _ =
  1241           | no_binds _ =
  1242               raise ProofContext.CONTEXT ("Illegal term bindings in locale element", ctxt);
  1242               raise ProofContext.CONTEXT ("Illegal term bindings in locale element", ctxt);
  1964     val tvars = foldr Term.add_typ_tvars [] pvTs;
  1964     val tvars = foldr Term.add_typ_tvars [] pvTs;
  1965     val used = foldr Term.add_typ_varnames [] pvTs;
  1965     val used = foldr Term.add_typ_varnames [] pvTs;
  1966     fun sorts (a, i) = assoc (tvars, (a, i));
  1966     fun sorts (a, i) = assoc (tvars, (a, i));
  1967     val (vs, vinst) = read_terms thy_ctxt sorts used given_insts;
  1967     val (vs, vinst) = read_terms thy_ctxt sorts used given_insts;
  1968     val vars = foldl Term.add_term_tvar_ixns [] vs \\ map fst tvars;
  1968     val vars = foldl Term.add_term_tvar_ixns [] vs \\ map fst tvars;
  1969     val vars' = Library.foldl Term.add_term_varnames (vars, vs);
  1969     val vars' = fold Term.add_term_varnames vs vars;
  1970     val _ = if null vars' then ()
  1970     val _ = if null vars' then ()
  1971          else error ("Illegal schematic variable(s) in instantiation: " ^
  1971          else error ("Illegal schematic variable(s) in instantiation: " ^
  1972            commas_quote (map Syntax.string_of_vname vars'));
  1972            commas_quote (map Syntax.string_of_vname vars'));
  1973     (* replace new types (which are TFrees) by ones with new names *)
  1973     (* replace new types (which are TFrees) by ones with new names *)
  1974     val new_Tnames = foldr Term.add_term_tfree_names [] vs;
  1974     val new_Tnames = foldr Term.add_term_tfree_names [] vs;