src/Pure/theory.ML
changeset 48927 ef462b5558eb
parent 48638 22d65e375c01
child 48929 05d4e5f660ae
equal deleted inserted replaced
48926:8d7778a19857 48927:ef462b5558eb
    18   val merge_refs: theory_ref * theory_ref -> theory_ref
    18   val merge_refs: theory_ref * theory_ref -> theory_ref
    19   val merge_list: theory list -> theory
    19   val merge_list: theory list -> theory
    20   val checkpoint: theory -> theory
    20   val checkpoint: theory -> theory
    21   val copy: theory -> theory
    21   val copy: theory -> theory
    22   val requires: theory -> string -> string -> unit
    22   val requires: theory -> string -> string -> unit
       
    23   val get_markup: theory -> Markup.T
    23   val axiom_space: theory -> Name_Space.T
    24   val axiom_space: theory -> Name_Space.T
    24   val axiom_table: theory -> term Symtab.table
    25   val axiom_table: theory -> term Symtab.table
    25   val axioms_of: theory -> (string * term) list
    26   val axioms_of: theory -> (string * term) list
    26   val all_axioms_of: theory -> (string * term) list
    27   val all_axioms_of: theory -> (string * term) list
    27   val defs_of: theory -> Defs.T
    28   val defs_of: theory -> Defs.T
    28   val at_begin: (theory -> theory option) -> theory -> theory
    29   val at_begin: (theory -> theory option) -> theory -> theory
    29   val at_end: (theory -> theory option) -> theory -> theory
    30   val at_end: (theory -> theory option) -> theory -> theory
    30   val begin_theory: string -> theory list -> theory
    31   val begin_theory: string * Position.T -> theory list -> theory
    31   val end_theory: theory -> theory
    32   val end_theory: theory -> theory
    32   val add_axiom: Proof.context -> binding * term -> theory -> theory
    33   val add_axiom: Proof.context -> binding * term -> theory -> theory
    33   val add_deps: Proof.context -> string -> string * typ -> (string * typ) list -> theory -> theory
    34   val add_deps: Proof.context -> string -> string * typ -> (string * typ) list -> theory -> theory
    34   val add_deps_global: string -> string * typ -> (string * typ) list -> theory -> theory
    35   val add_deps_global: string -> string * typ -> (string * typ) list -> theory -> theory
    35   val add_def: Proof.context -> bool -> bool -> binding * term -> theory -> theory
    36   val add_def: Proof.context -> bool -> bool -> binding * term -> theory -> theory
    78 
    79 
    79 fun apply_wrappers (wrappers: wrapper list) =
    80 fun apply_wrappers (wrappers: wrapper list) =
    80   perhaps (perhaps_loop (perhaps_apply (map fst wrappers)));
    81   perhaps (perhaps_loop (perhaps_apply (map fst wrappers)));
    81 
    82 
    82 datatype thy = Thy of
    83 datatype thy = Thy of
    83  {axioms: term Name_Space.table,
    84  {pos: Position.T,
       
    85   id: serial,
       
    86   axioms: term Name_Space.table,
    84   defs: Defs.T,
    87   defs: Defs.T,
    85   wrappers: wrapper list * wrapper list};
    88   wrappers: wrapper list * wrapper list};
    86 
    89 
    87 fun make_thy (axioms, defs, wrappers) = Thy {axioms = axioms, defs = defs, wrappers = wrappers};
    90 fun make_thy (pos, id, axioms, defs, wrappers) =
       
    91   Thy {pos = pos, id = id, axioms = axioms, defs = defs, wrappers = wrappers};
    88 
    92 
    89 structure Thy = Theory_Data_PP
    93 structure Thy = Theory_Data_PP
    90 (
    94 (
    91   type T = thy;
    95   type T = thy;
    92   val empty_axioms = Name_Space.empty_table "axiom" : term Name_Space.table;
    96   val empty_axioms = Name_Space.empty_table "axiom" : term Name_Space.table;
    93   val empty = make_thy (empty_axioms, Defs.empty, ([], []));
    97   val empty = make_thy (Position.none, 0, empty_axioms, Defs.empty, ([], []));
    94 
    98 
    95   fun extend (Thy {axioms = _, defs, wrappers}) = make_thy (empty_axioms, defs, wrappers);
    99   fun extend (Thy {pos = _, id = _, axioms = _, defs, wrappers}) =
       
   100     make_thy (Position.none, 0, empty_axioms, defs, wrappers);
    96 
   101 
    97   fun merge pp (thy1, thy2) =
   102   fun merge pp (thy1, thy2) =
    98     let
   103     let
    99       val ctxt = Syntax.init_pretty pp;
   104       val ctxt = Syntax.init_pretty pp;
   100       val Thy {axioms = _, defs = defs1, wrappers = (bgs1, ens1)} = thy1;
   105       val Thy {pos = _, id = _, axioms = _, defs = defs1, wrappers = (bgs1, ens1)} = thy1;
   101       val Thy {axioms = _, defs = defs2, wrappers = (bgs2, ens2)} = thy2;
   106       val Thy {pos = _, id = _, axioms = _, defs = defs2, wrappers = (bgs2, ens2)} = thy2;
   102 
   107 
   103       val axioms' = empty_axioms;
   108       val axioms' = empty_axioms;
   104       val defs' = Defs.merge ctxt (defs1, defs2);
   109       val defs' = Defs.merge ctxt (defs1, defs2);
   105       val bgs' = Library.merge (eq_snd op =) (bgs1, bgs2);
   110       val bgs' = Library.merge (eq_snd op =) (bgs1, bgs2);
   106       val ens' = Library.merge (eq_snd op =) (ens1, ens2);
   111       val ens' = Library.merge (eq_snd op =) (ens1, ens2);
   107     in make_thy (axioms', defs', (bgs', ens')) end;
   112     in make_thy (Position.none, 0, axioms', defs', (bgs', ens')) end;
   108 );
   113 );
   109 
   114 
   110 fun rep_theory thy = Thy.get thy |> (fn Thy args => args);
   115 fun rep_theory thy = Thy.get thy |> (fn Thy args => args);
   111 
   116 
   112 fun map_thy f = Thy.map (fn (Thy {axioms, defs, wrappers}) =>
   117 fun map_thy f = Thy.map (fn (Thy {pos, id, axioms, defs, wrappers}) =>
   113   make_thy (f (axioms, defs, wrappers)));
   118   make_thy (f (pos, id, axioms, defs, wrappers)));
   114 
   119 
   115 
   120 fun map_axioms f =
   116 fun map_axioms f = map_thy (fn (axioms, defs, wrappers) => (f axioms, defs, wrappers));
   121   map_thy (fn (pos, id, axioms, defs, wrappers) => (pos, id, f axioms, defs, wrappers));
   117 fun map_defs f = map_thy (fn (axioms, defs, wrappers) => (axioms, f defs, wrappers));
   122 
   118 fun map_wrappers f = map_thy (fn (axioms, defs, wrappers) => (axioms, defs, f wrappers));
   123 fun map_defs f =
       
   124   map_thy (fn (pos, id, axioms, defs, wrappers) => (pos, id, axioms, f defs, wrappers));
       
   125 
       
   126 fun map_wrappers f =
       
   127   map_thy (fn (pos, id, axioms, defs, wrappers) => (pos, id, axioms, defs, f wrappers));
       
   128 
       
   129 
       
   130 (* entity markup *)
       
   131 
       
   132 fun theory_markup def name id pos =
       
   133   if id = 0 then Markup.empty
       
   134   else
       
   135     Markup.properties (Position.entity_properties_of def id pos)
       
   136       (Isabelle_Markup.entity Isabelle_Markup.theoryN name);
       
   137 
       
   138 fun get_markup thy =
       
   139   let val {pos, id, ...} = rep_theory thy
       
   140   in theory_markup false (Context.theory_name thy) id pos end;
   119 
   141 
   120 
   142 
   121 (* basic operations *)
   143 (* basic operations *)
   122 
   144 
   123 val axiom_space = #1 o #axioms o rep_theory;
   145 val axiom_space = #1 o #axioms o rep_theory;
   135 val end_wrappers = rev o #2 o #wrappers o rep_theory;
   157 val end_wrappers = rev o #2 o #wrappers o rep_theory;
   136 
   158 
   137 fun at_begin f = map_wrappers (apfst (cons (f, stamp ())));
   159 fun at_begin f = map_wrappers (apfst (cons (f, stamp ())));
   138 fun at_end f = map_wrappers (apsnd (cons (f, stamp ())));
   160 fun at_end f = map_wrappers (apsnd (cons (f, stamp ())));
   139 
   161 
   140 fun begin_theory name imports =
   162 fun begin_theory (name, pos) imports =
   141   if name = Context.PureN then
   163   if name = Context.PureN then
   142     (case imports of [thy] => thy | _ => error "Bad bootstrapping of theory Pure")
   164     (case imports of [thy] => thy | _ => error "Bad bootstrapping of theory Pure")
   143   else
   165   else
   144     let
   166     let
   145       val thy = Context.begin_thy Context.pretty_global name imports;
   167       val id = serial ();
       
   168       val thy =
       
   169         Context.begin_thy Context.pretty_global name imports
       
   170         |> map_thy (fn (_, _, axioms, defs, wrappers) => (pos, id, axioms, defs, wrappers));
       
   171       val _ = Position.report pos (theory_markup true name id pos);
       
   172 
   146       val wrappers = begin_wrappers thy;
   173       val wrappers = begin_wrappers thy;
   147     in
   174     in
   148       thy
   175       thy
   149       |> Sign.local_path
   176       |> Sign.local_path
   150       |> Sign.map_naming (Name_Space.set_theory_name name)
   177       |> Sign.map_naming (Name_Space.set_theory_name name)