src/Pure/Isar/locale.ML
changeset 18678 dd0c569fa43d
parent 18671 621efeed255b
child 18698 a95c2adc8900
equal deleted inserted replaced
18677:01265301db7f 18678:dd0c569fa43d
   473     val prt_ids = List.concat (separate [Pretty.str " +", Pretty.brk 1] (map prt_id ids));
   473     val prt_ids = List.concat (separate [Pretty.str " +", Pretty.brk 1] (map prt_id ids));
   474     val err_msg =
   474     val err_msg =
   475       if forall (equal "" o #1) ids then msg
   475       if forall (equal "" o #1) ids then msg
   476       else msg ^ "\n" ^ Pretty.string_of (Pretty.block
   476       else msg ^ "\n" ^ Pretty.string_of (Pretty.block
   477         (Pretty.str "The error(s) above occurred in locale:" :: Pretty.brk 1 :: prt_ids));
   477         (Pretty.str "The error(s) above occurred in locale:" :: Pretty.brk 1 :: prt_ids));
   478   in raise ProofContext.CONTEXT (err_msg, ctxt) end;
   478   in error err_msg end;
   479 
   479 
   480 fun err_in_locale' ctxt msg ids' = err_in_locale ctxt msg (map fst ids');
   480 fun err_in_locale' ctxt msg ids' = err_in_locale ctxt msg (map fst ids');
   481 
   481 
   482 
   482 
   483 
   483 
   666     val thy = ProofContext.theory_of ctxt;
   666     val thy = ProofContext.theory_of ctxt;
   667 
   667 
   668     fun renaming (SOME x :: xs) (y :: ys) = (y, x) :: renaming xs ys
   668     fun renaming (SOME x :: xs) (y :: ys) = (y, x) :: renaming xs ys
   669       | renaming (NONE :: xs) (y :: ys) = renaming xs ys
   669       | renaming (NONE :: xs) (y :: ys) = renaming xs ys
   670       | renaming [] _ = []
   670       | renaming [] _ = []
   671       | renaming xs [] = raise ERROR_MESSAGE ("Too many arguments in renaming: " ^
   671       | renaming xs [] = error ("Too many arguments in renaming: " ^
   672           commas (map (fn NONE => "_" | SOME x => quote (fst x)) xs));
   672           commas (map (fn NONE => "_" | SOME x => quote (fst x)) xs));
   673 
   673 
   674     fun rename_parms top ren ((name, ps), (parms, mode)) =
   674     fun rename_parms top ren ((name, ps), (parms, mode)) =
   675       let val ps' = map (Element.rename ren) ps in
   675       let val ps' = map (Element.rename ren) ps in
   676         (case duplicates ps' of
   676         (case duplicates ps' of
   747             in (ids_ax, merge_lists parms' ps, syn) end
   747             in (ids_ax, merge_lists parms' ps, syn) end
   748       | identify top (Rename (e, xs)) =
   748       | identify top (Rename (e, xs)) =
   749           let
   749           let
   750             val (ids', parms', syn') = identify top e;
   750             val (ids', parms', syn') = identify top e;
   751             val ren = renaming xs parms'
   751             val ren = renaming xs parms'
   752               handle ERROR_MESSAGE msg => err_in_locale' ctxt msg ids';
   752               handle ERROR msg => err_in_locale' ctxt msg ids';
   753 
   753 
   754             val ids'' = gen_distinct (eq_fst (op =)) (map (rename_parms top ren) ids');
   754             val ids'' = gen_distinct (eq_fst (op =)) (map (rename_parms top ren) ids');
   755             val parms'' = distinct (List.concat (map (#2 o #1) ids''));
   755             val parms'' = distinct (List.concat (map (#2 o #1) ids''));
   756             val syn'' = syn' |> Symtab.dest |> map (Element.rename_var ren) |> Symtab.make;
   756             val syn'' = syn' |> Symtab.dest |> map (Element.rename_var ren) |> Symtab.make;
   757             (* check for conflicting syntax? *)
   757             (* check for conflicting syntax? *)
   867 fun activate_elems (((name, ps), mode), elems) ctxt =
   867 fun activate_elems (((name, ps), mode), elems) ctxt =
   868   let
   868   let
   869     val thy = ProofContext.theory_of ctxt;
   869     val thy = ProofContext.theory_of ctxt;
   870     val ((ctxt', _), res) =
   870     val ((ctxt', _), res) =
   871         foldl_map (activate_elem (name = "")) ((ProofContext.qualified_names ctxt, mode), elems)
   871         foldl_map (activate_elem (name = "")) ((ProofContext.qualified_names ctxt, mode), elems)
   872       handle ProofContext.CONTEXT (msg, ctxt) => err_in_locale ctxt msg [(name, map fst ps)]
   872       handle ERROR msg => err_in_locale ctxt msg [(name, map fst ps)]
   873     val ctxt'' = if name = "" then ctxt'
   873     val ctxt'' = if name = "" then ctxt'
   874           else let
   874           else let
   875               val ps' = map (fn (n, SOME T) => Free (n, T)) ps;
   875               val ps' = map (fn (n, SOME T) => Free (n, T)) ps;
   876               val ctxt'' = put_local_registration (name, ps') ("", []) ctxt'
   876               val ctxt'' = put_local_registration (name, ps') ("", []) ctxt'
   877             in case mode of
   877             in case mode of
   990 fun declare_elems prep_vars (ctxt, (((name, ps), Assumed _), elems)) =
   990 fun declare_elems prep_vars (ctxt, (((name, ps), Assumed _), elems)) =
   991     let val (ctxt', propps) =
   991     let val (ctxt', propps) =
   992       (case elems of
   992       (case elems of
   993         Int es => foldl_map declare_int_elem (ctxt, es)
   993         Int es => foldl_map declare_int_elem (ctxt, es)
   994       | Ext e => foldl_map (declare_ext_elem prep_vars) (ctxt, [e]))
   994       | Ext e => foldl_map (declare_ext_elem prep_vars) (ctxt, [e]))
   995       handle ProofContext.CONTEXT (msg, ctxt) =>
   995       handle ERROR msg => err_in_locale ctxt msg [(name, map fst ps)]
   996         err_in_locale ctxt msg [(name, map fst ps)]
       
   997     in (ctxt', propps) end
   996     in (ctxt', propps) end
   998   | declare_elems _ (ctxt, ((_, Derived _), elems)) = (ctxt, []);
   997   | declare_elems _ (ctxt, ((_, Derived _), elems)) = (ctxt, []);
   999 
   998 
  1000 in
   999 in
  1001 
  1000 
  1100           let val frees = rev (filter_out (ProofContext.is_fixed ctxt o #1)
  1099           let val frees = rev (filter_out (ProofContext.is_fixed ctxt o #1)
  1101             (Term.add_frees t []))
  1100             (Term.add_frees t []))
  1102           in Term.list_all_free (frees, t) end;
  1101           in Term.list_all_free (frees, t) end;
  1103 
  1102 
  1104         fun no_binds [] = []
  1103         fun no_binds [] = []
  1105           | no_binds _ =
  1104           | no_binds _ = error "Illegal term bindings in locale element";
  1106               raise ProofContext.CONTEXT ("Illegal term bindings in locale element", ctxt);
       
  1107       in
  1105       in
  1108         (case elem of
  1106         (case elem of
  1109           Assumes asms => Assumes (asms |> map (fn (a, propps) =>
  1107           Assumes asms => Assumes (asms |> map (fn (a, propps) =>
  1110             (a, map (fn (t, (ps, qs)) => (close_frees t, (no_binds ps, no_binds qs))) propps)))
  1108             (a, map (fn (t, (ps, qs)) => (close_frees t, (no_binds ps, no_binds qs))) propps)))
  1111         | Defines defs => Defines (defs |> map (fn (a, (t, ps)) =>
  1109         | Defines defs => Defines (defs |> map (fn (a, (t, ps)) =>
  1242 
  1240 
  1243 (* facts and attributes *)
  1241 (* facts and attributes *)
  1244 
  1242 
  1245 local
  1243 local
  1246 
  1244 
  1247 fun prep_name ctxt name =
  1245 fun prep_name name =
  1248   if NameSpace.is_qualified name then
  1246   if NameSpace.is_qualified name then error ("Illegal qualified name: " ^ quote name)
  1249     raise ProofContext.CONTEXT ("Illegal qualified name: " ^ quote name, ctxt)
       
  1250   else name;
  1247   else name;
  1251 
  1248 
  1252 fun prep_facts _ _ ctxt (Int elem) =
  1249 fun prep_facts _ _ ctxt (Int elem) =
  1253       Element.map_ctxt_values I I (Thm.transfer (ProofContext.theory_of ctxt)) elem
  1250       Element.map_ctxt_values I I (Thm.transfer (ProofContext.theory_of ctxt)) elem
  1254   | prep_facts get intern ctxt (Ext elem) = elem |> Element.map_ctxt
  1251   | prep_facts get intern ctxt (Ext elem) = elem |> Element.map_ctxt
  1255      {var = I, typ = I, term = I,
  1252      {var = I, typ = I, term = I,
  1256       name = prep_name ctxt,
  1253       name = prep_name,
  1257       fact = get ctxt,
  1254       fact = get ctxt,
  1258       attrib = Args.assignable o intern (ProofContext.theory_of ctxt)};
  1255       attrib = Args.assignable o intern (ProofContext.theory_of ctxt)};
  1259 
  1256 
  1260 in
  1257 in
  1261 
  1258