src/Pure/context.ML
changeset 18731 3989c3c41983
parent 18711 cf020c54e2f5
child 18931 427df66052a1
equal deleted inserted replaced
18730:843da46f89ac 18731:3989c3c41983
    73   (*generic context*)
    73   (*generic context*)
    74   datatype generic = Theory of theory | Proof of proof
    74   datatype generic = Theory of theory | Proof of proof
    75   val cases: (theory -> 'a) -> (proof -> 'a) -> generic -> 'a
    75   val cases: (theory -> 'a) -> (proof -> 'a) -> generic -> 'a
    76   val the_theory: generic -> theory
    76   val the_theory: generic -> theory
    77   val the_proof: generic -> proof
    77   val the_proof: generic -> proof
    78   val map_theory: (generic -> generic) -> theory -> theory
    78   val map_theory: (theory -> theory) -> generic -> generic
    79   val map_proof: (generic -> generic) -> proof -> proof
    79   val map_proof: (proof -> proof) -> generic -> generic
       
    80   val theory_map: (generic -> generic) -> theory -> theory
       
    81   val proof_map: (generic -> generic) -> proof -> proof
    80   val theory_of: generic -> theory   (*total*)
    82   val theory_of: generic -> theory   (*total*)
    81   val proof_of: generic -> proof     (*total*)
    83   val proof_of: generic -> proof     (*total*)
    82 end;
    84 end;
    83 
    85 
    84 signature PRIVATE_CONTEXT =
    86 signature PRIVATE_CONTEXT =
   184 fun make_identity self id ids iids = {self = self, id = id, ids = ids, iids = iids};
   186 fun make_identity self id ids iids = {self = self, id = id, ids = ids, iids = iids};
   185 fun make_data theory proof = {theory = theory, proof = proof};
   187 fun make_data theory proof = {theory = theory, proof = proof};
   186 fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
   188 fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
   187 fun make_history name vers ints = {name = name, version = vers, intermediates = ints};
   189 fun make_history name vers ints = {name = name, version = vers, intermediates = ints};
   188 
   190 
   189 fun map_theory f {theory, proof} = make_data (f theory) proof;
   191 fun map_theory_data f {theory, proof} = make_data (f theory) proof;
   190 fun map_proof f {theory, proof} = make_data theory (f proof);
   192 fun map_proof_data f {theory, proof} = make_data theory (f proof);
   191 
   193 
   192 val the_self = the o #self o identity_of;
   194 val the_self = the o #self o identity_of;
   193 val parents_of = #parents o ancestry_of;
   195 val parents_of = #parents o ancestry_of;
   194 val ancestors_of = #ancestors o ancestry_of;
   196 val ancestors_of = #ancestors o ancestry_of;
   195 val theory_name = #name o history_of;
   197 val theory_name = #name o history_of;
   330   let
   332   let
   331     val _ = check_thy thy;
   333     val _ = check_thy thy;
   332     val (self', data', ancestry') =
   334     val (self', data', ancestry') =
   333       if is_draft thy then (self, data, ancestry)    (*destructive change!*)
   335       if is_draft thy then (self, data, ancestry)    (*destructive change!*)
   334       else if #version history > 0
   336       else if #version history > 0
   335       then (NONE, map_theory copy_data data, ancestry)
   337       then (NONE, map_theory_data copy_data data, ancestry)
   336       else (NONE, map_theory extend_data data, make_ancestry [thy] (thy :: #ancestors ancestry));
   338       else (NONE, map_theory_data extend_data data,
       
   339         make_ancestry [thy] (thy :: #ancestors ancestry));
   337     val data'' = f data';
   340     val data'' = f data';
   338   in create_thy name self' id ids iids data'' ancestry' history end;
   341   in create_thy name self' id ids iids data'' ancestry' history end;
   339 
   342 
   340 fun name_thy name = change_thy name I;
   343 fun name_thy name = change_thy name I;
   341 val modify_thy = change_thy draftN;
   344 val modify_thy = change_thy draftN;
   342 val extend_thy = modify_thy I;
   345 val extend_thy = modify_thy I;
   343 
   346 
   344 fun copy_thy (thy as Theory ({id, ids, iids, ...}, data, ancestry, history)) =
   347 fun copy_thy (thy as Theory ({id, ids, iids, ...}, data, ancestry, history)) =
   345   (check_thy thy;
   348   (check_thy thy;
   346     create_thy draftN NONE id ids iids (map_theory copy_data data) ancestry history);
   349     create_thy draftN NONE id ids iids (map_theory_data copy_data data) ancestry history);
   347 
   350 
   348 val pre_pure_thy = create_thy draftN NONE (serial (), draftN) Inttab.empty Inttab.empty
   351 val pre_pure_thy = create_thy draftN NONE (serial (), draftN) Inttab.empty Inttab.empty
   349   (make_data Inttab.empty Inttab.empty) (make_ancestry [] []) (make_history ProtoPureN 0 []);
   352   (make_data Inttab.empty Inttab.empty) (make_ancestry [] []) (make_history ProtoPureN 0 []);
   350 
   353 
   351 
   354 
   428   (case Inttab.lookup (#theory (data_of thy)) k of
   431   (case Inttab.lookup (#theory (data_of thy)) k of
   429     SOME x => (dest x handle Match =>
   432     SOME x => (dest x handle Match =>
   430       error ("Failed to access theory data " ^ quote (invoke_name k)))
   433       error ("Failed to access theory data " ^ quote (invoke_name k)))
   431   | NONE => error ("Uninitialized theory data " ^ quote (invoke_name k)));
   434   | NONE => error ("Uninitialized theory data " ^ quote (invoke_name k)));
   432 
   435 
   433 fun put k mk x = modify_thy (map_theory (Inttab.update (k, mk x)));
   436 fun put k mk x = modify_thy (map_theory_data (Inttab.update (k, mk x)));
   434 fun init k = put k I (invoke_empty k);
   437 fun init k = put k I (invoke_empty k);
   435 
   438 
   436 end;
   439 end;
   437 
   440 
   438 
   441 
   551     val _ = conditional (Inttab.exists (equal name o #name o #2) (! kinds)) (fn () =>
   554     val _ = conditional (Inttab.exists (equal name o #name o #2) (! kinds)) (fn () =>
   552       warning ("Duplicate declaration of proof data " ^ quote name));
   555       warning ("Duplicate declaration of proof data " ^ quote name));
   553     val _ = change kinds (Inttab.update (k, kind));
   556     val _ = change kinds (Inttab.update (k, kind));
   554   in k end;
   557   in k end;
   555 
   558 
   556 fun init k = modify_thy (map_proof (Inttab.update (k, ())));
   559 fun init k = modify_thy (map_proof_data (Inttab.update (k, ())));
   557 
   560 
   558 fun get k dest prf =
   561 fun get k dest prf =
   559   (case Inttab.lookup (data_of_proof prf) k of
   562   (case Inttab.lookup (data_of_proof prf) k of
   560     SOME x => (dest x handle Match =>
   563     SOME x => (dest x handle Match =>
   561       error ("Failed to access proof data " ^ quote (invoke_name k)))
   564       error ("Failed to access proof data " ^ quote (invoke_name k)))
   574 datatype generic = Theory of theory | Proof of proof;
   577 datatype generic = Theory of theory | Proof of proof;
   575 
   578 
   576 fun cases f _ (Theory thy) = f thy
   579 fun cases f _ (Theory thy) = f thy
   577   | cases _ g (Proof prf) = g prf;
   580   | cases _ g (Proof prf) = g prf;
   578 
   581 
   579 val the_theory = cases I (fn _ => raise Fail "Bad generic context: theory expected");
   582 val the_theory = cases I (fn _ => raise Fail "Ill-typed context: theory expected");
   580 val the_proof = cases (fn _ => raise Fail "Bad generic context: proof context expected") I;
   583 val the_proof = cases (fn _ => raise Fail "Ill-typed context: proof expected") I;
   581 
   584 
   582 fun map_theory f = the_theory o f o Theory;
   585 fun map_theory f = Theory o f o the_theory;
   583 fun map_proof f = the_proof o f o Proof;
   586 fun map_proof f = Proof o f o the_proof;
       
   587 
       
   588 fun theory_map f = the_theory o f o Theory;
       
   589 fun proof_map f = the_proof o f o Proof;
   584 
   590 
   585 val theory_of = cases I theory_of_proof;
   591 val theory_of = cases I theory_of_proof;
   586 val proof_of = cases init_proof I;
   592 val proof_of = cases init_proof I;
   587 
   593 
   588 end;
   594 end;