src/Pure/context.ML
changeset 61044 b7af255dd200
parent 60948 b710a5087116
child 61045 c7a7f063704a
equal deleted inserted replaced
61043:0810068379d8 61044:b7af255dd200
    35   val parents_of: theory -> theory list
    35   val parents_of: theory -> theory list
    36   val ancestors_of: theory -> theory list
    36   val ancestors_of: theory -> theory list
    37   val theory_id_name: theory_id -> string
    37   val theory_id_name: theory_id -> string
    38   val theory_name: theory -> string
    38   val theory_name: theory -> string
    39   val PureN: string
    39   val PureN: string
       
    40   val display_name: theory_id -> string
    40   val display_names: theory -> string list
    41   val display_names: theory -> string list
    41   val pretty_thy: theory -> Pretty.T
    42   val pretty_thy: theory -> Pretty.T
    42   val string_of_thy: theory -> string
    43   val string_of_thy: theory -> string
    43   val pretty_abbrev_thy: theory -> Pretty.T
    44   val pretty_abbrev_thy: theory -> Pretty.T
    44   val str_of_thy: theory -> string
    45   val str_of_thy: theory -> string
    48   val eq_thy: theory * theory -> bool
    49   val eq_thy: theory * theory -> bool
    49   val proper_subthy_id: theory_id * theory_id -> bool
    50   val proper_subthy_id: theory_id * theory_id -> bool
    50   val proper_subthy: theory * theory -> bool
    51   val proper_subthy: theory * theory -> bool
    51   val subthy_id: theory_id * theory_id -> bool
    52   val subthy_id: theory_id * theory_id -> bool
    52   val subthy: theory * theory -> bool
    53   val subthy: theory * theory -> bool
    53   val merge: theory * theory -> theory
       
    54   val finish_thy: theory -> theory
    54   val finish_thy: theory -> theory
    55   val begin_thy: (theory -> pretty) -> string -> theory list -> theory
    55   val begin_thy: (theory -> pretty) -> string -> theory list -> theory
    56   (*proof context*)
    56   (*proof context*)
    57   val raw_transfer: theory -> Proof.context -> Proof.context
    57   val raw_transfer: theory -> Proof.context -> Proof.context
       
    58   (*certificate*)
       
    59   datatype certificate = Certificate of theory | Certificate_Id of theory_id
       
    60   val certificate_theory: certificate -> theory
       
    61   val certificate_theory_id: certificate -> theory_id
       
    62   val eq_certificate: certificate * certificate -> bool
       
    63   val join_certificate: certificate * certificate -> certificate
    58   (*generic context*)
    64   (*generic context*)
    59   datatype generic = Theory of theory | Proof of Proof.context
    65   datatype generic = Theory of theory | Proof of Proof.context
    60   val cases: (theory -> 'a) -> (Proof.context -> 'a) -> generic -> 'a
    66   val cases: (theory -> 'a) -> (Proof.context -> 'a) -> generic -> 'a
    61   val mapping: (theory -> theory) -> (Proof.context -> Proof.context) -> generic -> generic
    67   val mapping: (theory -> theory) -> (Proof.context -> Proof.context) -> generic -> generic
    62   val mapping_result: (theory -> 'a * theory) -> (Proof.context -> 'a * Proof.context) ->
    68   val mapping_result: (theory -> 'a * theory) -> (Proof.context -> 'a * Proof.context) ->
   205 (* names *)
   211 (* names *)
   206 
   212 
   207 val PureN = "Pure";
   213 val PureN = "Pure";
   208 val finished = ~1;
   214 val finished = ~1;
   209 
   215 
       
   216 fun display_name thy_id =
       
   217   let val {name, stage} = history_of_id thy_id
       
   218   in if stage = finished then name else name ^ ":" ^ string_of_int stage end;
       
   219 
   210 fun display_names thy =
   220 fun display_names thy =
   211   let
   221   let
   212     val {name, stage} = history_of thy;
   222     val name = display_name (theory_id thy);
   213     val name' =
       
   214       if stage = finished then name
       
   215       else name ^ ":" ^ string_of_int stage;
       
   216     val ancestor_names = map theory_name (ancestors_of thy);
   223     val ancestor_names = map theory_name (ancestors_of thy);
   217   in rev (name' :: ancestor_names) end;
   224   in rev (name :: ancestor_names) end;
   218 
   225 
   219 val pretty_thy = Pretty.str_list "{" "}" o display_names;
   226 val pretty_thy = Pretty.str_list "{" "}" o display_names;
   220 val string_of_thy = Pretty.string_of o pretty_thy;
   227 val string_of_thy = Pretty.string_of o pretty_thy;
   221 
   228 
   222 fun pretty_abbrev_thy thy =
   229 fun pretty_abbrev_thy thy =
   276   if member eq_thy_consistent thys thy then
   283   if member eq_thy_consistent thys thy then
   277     raise THEORY ("Duplicate theory node", thy :: thys)
   284     raise THEORY ("Duplicate theory node", thy :: thys)
   278   else thy :: thys;
   285   else thy :: thys;
   279 
   286 
   280 val merge_ancestors = merge eq_thy_consistent;
   287 val merge_ancestors = merge eq_thy_consistent;
   281 
       
   282 
       
   283 (* trivial merge *)
       
   284 
       
   285 fun merge (thy1, thy2) =
       
   286   if eq_thy (thy1, thy2) then thy1
       
   287   else if proper_subthy (thy2, thy1) then thy1
       
   288   else if proper_subthy (thy1, thy2) then thy2
       
   289   else error (cat_lines ["Attempt to perform non-trivial merge of theories:",
       
   290     str_of_thy thy1, str_of_thy thy2]);
       
   291 
   288 
   292 
   289 
   293 
   290 
   294 (** build theories **)
   291 (** build theories **)
   295 
   292 
   324 end;
   321 end;
   325 
   322 
   326 
   323 
   327 (* named theory nodes *)
   324 (* named theory nodes *)
   328 
   325 
       
   326 local
       
   327 
   329 fun merge_thys pp (thy1, thy2) =
   328 fun merge_thys pp (thy1, thy2) =
   330   let
   329   let
   331     val ids = merge_ids thy1 thy2;
   330     val ids = merge_ids thy1 thy2;
   332     val history = make_history "" 0;
   331     val history = make_history "" 0;
   333     val ancestry = make_ancestry [] [];
   332     val ancestry = make_ancestry [] [];
   334     val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
   333     val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
   335   in create_thy ids history ancestry data end;
   334   in create_thy ids history ancestry data end;
   336 
   335 
   337 fun maximal_thys thys =
   336 fun maximal_thys thys =
   338   thys |> filter_out (fn thy => exists (fn thy' => proper_subthy (thy, thy')) thys);
   337   thys |> filter_out (fn thy => exists (fn thy' => proper_subthy (thy, thy')) thys);
       
   338 
       
   339 in
   339 
   340 
   340 fun begin_thy pp name imports =
   341 fun begin_thy pp name imports =
   341   if name = "" then error ("Bad theory name: " ^ quote name)
   342   if name = "" then error ("Bad theory name: " ^ quote name)
   342   else
   343   else
   343     let
   344     let
   354 
   355 
   355       val history = make_history name 0;
   356       val history = make_history name 0;
   356       val ancestry = make_ancestry parents ancestors;
   357       val ancestry = make_ancestry parents ancestors;
   357     in create_thy ids history ancestry data end;
   358     in create_thy ids history ancestry data end;
   358 
   359 
       
   360 end;
       
   361 
   359 
   362 
   360 (* theory data *)
   363 (* theory data *)
   361 
   364 
   362 structure Theory_Data =
   365 structure Theory_Data =
   363 struct
   366 struct
   437   Proof.Context (Datatab.update (k, mk x) data, thy);
   440   Proof.Context (Datatab.update (k, mk x) data, thy);
   438 
   441 
   439 end;
   442 end;
   440 
   443 
   441 end;
   444 end;
       
   445 
       
   446 
       
   447 
       
   448 (*** theory certificate ***)
       
   449 
       
   450 datatype certificate = Certificate of theory | Certificate_Id of theory_id;
       
   451 
       
   452 fun certificate_theory (Certificate thy) = thy
       
   453   | certificate_theory (Certificate_Id thy_id) =
       
   454       raise Fail ("No content for theory certificate " ^ quote (display_name thy_id));
       
   455 
       
   456 fun certificate_theory_id (Certificate thy) = theory_id thy
       
   457   | certificate_theory_id (Certificate_Id thy_id) = thy_id;
       
   458 
       
   459 fun eq_certificate (Certificate thy1, Certificate thy2) = eq_thy (thy1, thy2)
       
   460   | eq_certificate (Certificate_Id thy_id1, Certificate_Id thy_id2) = eq_thy_id (thy_id1, thy_id2)
       
   461   | eq_certificate _ = false;
       
   462 
       
   463 fun join_certificate (cert1, cert2) =
       
   464   let val (thy_id1, thy_id2) = apply2 certificate_theory_id (cert1, cert2) in
       
   465     if eq_thy_id (thy_id1, thy_id2) then (case cert1 of Certificate _ => cert1 | _ => cert2)
       
   466     else if proper_subthy_id (thy_id2, thy_id1) then cert1
       
   467     else if proper_subthy_id (thy_id1, thy_id2) then cert2
       
   468     else
       
   469       raise Fail ("Cannot join unrelated theory certificates " ^
       
   470         quote (display_name thy_id1) ^ " and " ^ quote (display_name thy_id2))
       
   471   end;
   442 
   472 
   443 
   473 
   444 
   474 
   445 (*** generic context ***)
   475 (*** generic context ***)
   446 
   476 
   643 
   673 
   644 end;
   674 end;
   645 
   675 
   646 (*hide private interface*)
   676 (*hide private interface*)
   647 structure Context: CONTEXT = Context;
   677 structure Context: CONTEXT = Context;
   648