| author | wenzelm | 
| Thu, 08 Dec 2022 22:38:03 +0100 | |
| changeset 76610 | 6e2383488a55 | 
| parent 75627 | c8263ac985e1 | 
| child 78725 | 3c02ad5a1586 | 
| permissions | -rw-r--r-- | 
| 45709 
87017fcbad83
clarified modules (again) -- NB: both Document and Protocol are specific to this particular prover;
 wenzelm parents: 
45672diff
changeset | 1 | (* Title: Pure/PIDE/protocol.ML | 
| 38412 
c23f3abbf42d
moved isar_document.ML/scala to Pure/System/ -- side-by-side with isar.ML;
 wenzelm parents: diff
changeset | 2 | Author: Makarius | 
| 
c23f3abbf42d
moved isar_document.ML/scala to Pure/System/ -- side-by-side with isar.ML;
 wenzelm parents: diff
changeset | 3 | |
| 45709 
87017fcbad83
clarified modules (again) -- NB: both Document and Protocol are specific to this particular prover;
 wenzelm parents: 
45672diff
changeset | 4 | Protocol message formats for interactive proof documents. | 
| 38412 
c23f3abbf42d
moved isar_document.ML/scala to Pure/System/ -- side-by-side with isar.ML;
 wenzelm parents: diff
changeset | 5 | *) | 
| 
c23f3abbf42d
moved isar_document.ML/scala to Pure/System/ -- side-by-side with isar.ML;
 wenzelm parents: diff
changeset | 6 | |
| 45709 
87017fcbad83
clarified modules (again) -- NB: both Document and Protocol are specific to this particular prover;
 wenzelm parents: 
45672diff
changeset | 7 | structure Protocol: sig end = | 
| 38412 
c23f3abbf42d
moved isar_document.ML/scala to Pure/System/ -- side-by-side with isar.ML;
 wenzelm parents: diff
changeset | 8 | struct | 
| 
c23f3abbf42d
moved isar_document.ML/scala to Pure/System/ -- side-by-side with isar.ML;
 wenzelm parents: diff
changeset | 9 | |
| 38418 
9a7af64d71bb
more explicit / functional ML version of document model;
 wenzelm parents: 
38417diff
changeset | 10 | val _ = | 
| 73225 
3ab0cedaccad
clarified modules: allow early definition of protocol commands;
 wenzelm parents: 
72946diff
changeset | 11 | Protocol_Command.define "Prover.echo" | 
| 52786 | 12 | (fn args => List.app writeln args); | 
| 13 | ||
| 14 | val _ = | |
| 73225 
3ab0cedaccad
clarified modules: allow early definition of protocol commands;
 wenzelm parents: 
72946diff
changeset | 15 | Protocol_Command.define "Prover.stop" | 
| 72217 | 16 | (fn rc :: msgs => | 
| 17 | (List.app Output.system_message msgs; | |
| 73225 
3ab0cedaccad
clarified modules: allow early definition of protocol commands;
 wenzelm parents: 
72946diff
changeset | 18 | raise Protocol_Command.STOP (Value.parse_int rc))); | 
| 71879 | 19 | |
| 20 | val _ = | |
| 75627 | 21 | Protocol_Command.define_bytes "Prover.options" | 
| 52786 | 22 | (fn [options_yxml] => | 
| 75627 | 23 | (Options.set_default (Options.decode (YXML.parse_body_bytes options_yxml)); | 
| 65300 | 24 | Isabelle_Process.init_options_interactive ())); | 
| 52585 | 25 | |
| 26 | val _ = | |
| 75626 | 27 | Protocol_Command.define_bytes "Prover.init_session" | 
| 72637 | 28 | (fn [yxml] => Resources.init_session_yxml yxml); | 
| 65470 | 29 | |
| 30 | val _ = | |
| 73225 
3ab0cedaccad
clarified modules: allow early definition of protocol commands;
 wenzelm parents: 
72946diff
changeset | 31 | Protocol_Command.define "Document.define_blob" | 
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
53192diff
changeset | 32 | (fn [digest, content] => Document.change_state (Document.define_blob digest content)); | 
| 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
53192diff
changeset | 33 | |
| 72946 | 34 | fun decode_command id name parents_xml blobs_xml toks_xml sources : Document.command = | 
| 70663 | 35 | let | 
| 36 | open XML.Decode; | |
| 72946 | 37 | val parents = list string parents_xml; | 
| 70663 | 38 | val (blobs_digests, blobs_index) = | 
| 70664 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70663diff
changeset | 39 | blobs_xml |> | 
| 70663 | 40 | let | 
| 41 | val message = YXML.string_of_body o Protocol_Message.command_positions id; | |
| 72747 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
72637diff
changeset | 42 | val blob = | 
| 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
72637diff
changeset | 43 | triple string string (option string) | 
| 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
72637diff
changeset | 44 |             #> (fn (a, b, c) => {file_node = a, src_path = Path.explode b, digest = c});
 | 
| 70663 | 45 | in | 
| 46 | pair | |
| 47 | (list (variant | |
| 72747 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
72637diff
changeset | 48 | [fn ([], a) => Exn.Res (blob a), | 
| 70663 | 49 | fn ([], a) => Exn.Exn (ERROR (message a))])) | 
| 50 | int | |
| 51 | end; | |
| 70664 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70663diff
changeset | 52 | val toks = list (pair int int) toks_xml; | 
| 70663 | 53 | in | 
| 54 |    {command_id = Document_ID.parse id,
 | |
| 55 | name = name, | |
| 72946 | 56 | parents = parents, | 
| 70663 | 57 | blobs_digests = blobs_digests, | 
| 58 | blobs_index = blobs_index, | |
| 59 | tokens = toks ~~ sources} | |
| 60 | end; | |
| 61 | ||
| 71024 | 62 | fun commands_accepted ids = | 
| 73559 | 63 | Output.protocol_message Markup.commands_accepted [[XML.Text (space_implode "," ids)]]; | 
| 70665 
94442fce40a5
prefer commands_accepted: fewer protocol messages;
 wenzelm parents: 
70664diff
changeset | 64 | |
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
53192diff
changeset | 65 | val _ = | 
| 73225 
3ab0cedaccad
clarified modules: allow early definition of protocol commands;
 wenzelm parents: 
72946diff
changeset | 66 | Protocol_Command.define "Document.define_command" | 
| 72946 | 67 | (fn id :: name :: parents :: blobs :: toks :: sources => | 
| 70664 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70663diff
changeset | 68 | let | 
| 72946 | 69 | val command = | 
| 70 | decode_command id name (YXML.parse_body parents) (YXML.parse_body blobs) | |
| 71 | (YXML.parse_body toks) sources; | |
| 70665 
94442fce40a5
prefer commands_accepted: fewer protocol messages;
 wenzelm parents: 
70664diff
changeset | 72 | val _ = Document.change_state (Document.define_command command); | 
| 
94442fce40a5
prefer commands_accepted: fewer protocol messages;
 wenzelm parents: 
70664diff
changeset | 73 | in commands_accepted [id] end); | 
| 70664 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70663diff
changeset | 74 | |
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70663diff
changeset | 75 | val _ = | 
| 73225 
3ab0cedaccad
clarified modules: allow early definition of protocol commands;
 wenzelm parents: 
72946diff
changeset | 76 | Protocol_Command.define "Document.define_commands" | 
| 70664 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70663diff
changeset | 77 | (fn args => | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70663diff
changeset | 78 | let | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70663diff
changeset | 79 | fun decode arg = | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70663diff
changeset | 80 | let | 
| 
2bd9e30183b1
prefer define_commands_bulk: fewer protocol messages;
 wenzelm parents: 
70663diff
changeset | 81 | open XML.Decode; | 
| 72946 | 82 | val (id, (name, (parents_xml, (blobs_xml, (toks_xml, sources))))) = | 
| 83 | pair string (pair string (pair I (pair I (pair I (list string))))) | |
| 84 | (YXML.parse_body arg); | |
| 85 | in decode_command id name parents_xml blobs_xml toks_xml sources end; | |
| 70665 
94442fce40a5
prefer commands_accepted: fewer protocol messages;
 wenzelm parents: 
70664diff
changeset | 86 | |
| 
94442fce40a5
prefer commands_accepted: fewer protocol messages;
 wenzelm parents: 
70664diff
changeset | 87 | val commands = map decode args; | 
| 
94442fce40a5
prefer commands_accepted: fewer protocol messages;
 wenzelm parents: 
70664diff
changeset | 88 | val _ = Document.change_state (fold Document.define_command commands); | 
| 
94442fce40a5
prefer commands_accepted: fewer protocol messages;
 wenzelm parents: 
70664diff
changeset | 89 | in commands_accepted (map (Value.print_int o #command_id) commands) end); | 
| 38412 
c23f3abbf42d
moved isar_document.ML/scala to Pure/System/ -- side-by-side with isar.ML;
 wenzelm parents: diff
changeset | 90 | |
| 38418 
9a7af64d71bb
more explicit / functional ML version of document model;
 wenzelm parents: 
38417diff
changeset | 91 | val _ = | 
| 73225 
3ab0cedaccad
clarified modules: allow early definition of protocol commands;
 wenzelm parents: 
72946diff
changeset | 92 | Protocol_Command.define "Document.discontinue_execution" | 
| 52606 | 93 | (fn [] => Execution.discontinue ()); | 
| 47343 
b8aeab386414
less aggressive discontinue_execution before document update, to avoid unstable execs that need to be re-assigned;
 wenzelm parents: 
46938diff
changeset | 94 | |
| 
b8aeab386414
less aggressive discontinue_execution before document update, to avoid unstable execs that need to be re-assigned;
 wenzelm parents: 
46938diff
changeset | 95 | val _ = | 
| 73225 
3ab0cedaccad
clarified modules: allow early definition of protocol commands;
 wenzelm parents: 
72946diff
changeset | 96 | Protocol_Command.define "Document.cancel_exec" | 
| 52931 
ac6648c0c0fb
cancel_query via direct access to the exec_id of the running query process;
 wenzelm parents: 
52862diff
changeset | 97 | (fn [exec_id] => Execution.cancel (Document_ID.parse exec_id)); | 
| 
ac6648c0c0fb
cancel_query via direct access to the exec_id of the running query process;
 wenzelm parents: 
52862diff
changeset | 98 | |
| 
ac6648c0c0fb
cancel_query via direct access to the exec_id of the running query process;
 wenzelm parents: 
52862diff
changeset | 99 | val _ = | 
| 75627 | 100 | Protocol_Command.define_bytes "Document.update" | 
| 59370 
b13ff987c559
refrain from default task_context for all protocol commands, e.g. relevant for "build_theories" to admit Session.shutdown;
 wenzelm parents: 
59366diff
changeset | 101 | (Future.task_context "Document.update" (Future.new_group NONE) | 
| 75627 | 102 | (fn old_id_bytes :: new_id_bytes :: consolidate_yxml :: edits_yxml => | 
| 68336 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 103 | Document.change_state (fn state => | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 104 | let | 
| 75627 | 105 | val old_id = Document_ID.parse (Bytes.content old_id_bytes); | 
| 106 | val new_id = Document_ID.parse (Bytes.content new_id_bytes); | |
| 69849 | 107 | val consolidate = | 
| 75627 | 108 | YXML.parse_body_bytes consolidate_yxml |> | 
| 69849 | 109 | let open XML.Decode in list string end; | 
| 68336 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 110 | val edits = | 
| 75627 | 111 | edits_yxml |> map (YXML.parse_body_bytes #> | 
| 68336 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 112 | let open XML.Decode in | 
| 69849 | 113 | pair string | 
| 68336 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 114 | (variant | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 115 | [fn ([], a) => Document.Edits (list (pair (option int) (option int)) a), | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 116 | fn ([], a) => | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 117 | let | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 118 | val (master, (name, (imports, (keywords, errors)))) = | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 119 | pair string (pair string (pair (list string) | 
| 72747 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
72637diff
changeset | 120 | (pair (list (pair string (pair string (list string)))) | 
| 68336 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 121 | (list YXML.string_of_body)))) a; | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 122 | val imports' = map (rpair Position.none) imports; | 
| 74671 | 123 | val keywords' = | 
| 124 | map (fn (x, y) => ((x, Position.none), Keyword.command_spec y)) keywords; | |
| 68336 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 125 | val header = Thy_Header.make (name, Position.none) imports' keywords'; | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 126 |                         in Document.Deps {master = master, header = header, errors = errors} end,
 | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 127 | fn (a :: b, c) => | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 128 | Document.Perspective (bool_atom a, map int_atom b, | 
| 69849 | 129 | list (pair int (pair string (list string))) c)]) | 
| 130 | end); | |
| 47404 
e6e5750f1311
simplified Future.cancel/cancel_group (again) -- running threads only;
 wenzelm parents: 
47388diff
changeset | 131 | |
| 68336 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 132 | val _ = Execution.discontinue (); | 
| 38412 
c23f3abbf42d
moved isar_document.ML/scala to Pure/System/ -- side-by-side with isar.ML;
 wenzelm parents: diff
changeset | 133 | |
| 70284 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69849diff
changeset | 134 | val (edited, removed, assign_update, state') = | 
| 68336 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 135 | Document.update old_id new_id edits consolidate state; | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 136 | val _ = | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 137 | (singleton o Future.forks) | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 138 |                {name = "Document.update/remove", group = NONE,
 | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 139 | deps = Execution.snapshot removed, | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 140 | pri = Task_Queue.urgent_pri + 2, interrupts = false} | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 141 | (fn () => (Execution.purge removed; List.app Isabelle_Process.reset_tracing removed)); | 
| 52601 | 142 | |
| 68336 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 143 | val _ = | 
| 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 144 | Output.protocol_message Markup.assign_update | 
| 73559 | 145 | [(new_id, edited, assign_update) |> | 
| 69846 | 146 | let | 
| 147 | open XML.Encode; | |
| 148 | fun encode_upd (a, bs) = | |
| 149 | string (space_implode "," (map Value.print_int (a :: bs))); | |
| 73559 | 150 | in triple int (list string) (list encode_upd) end]; | 
| 68336 
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
 wenzelm parents: 
67493diff
changeset | 151 | in Document.start_execution state' end))); | 
| 38412 
c23f3abbf42d
moved isar_document.ML/scala to Pure/System/ -- side-by-side with isar.ML;
 wenzelm parents: diff
changeset | 152 | |
| 43748 | 153 | val _ = | 
| 75627 | 154 | Protocol_Command.define_bytes "Document.remove_versions" | 
| 44673 | 155 | (fn [versions_yxml] => Document.change_state (fn state => | 
| 156 | let | |
| 157 | val versions = | |
| 75627 | 158 | YXML.parse_body_bytes versions_yxml |> | 
| 44673 | 159 | let open XML.Decode in list int end; | 
| 44676 | 160 | val state1 = Document.remove_versions versions state; | 
| 75627 | 161 | val _ = Output.protocol_message Markup.removed_versions [Bytes.contents_blob versions_yxml]; | 
| 44676 | 162 | in state1 end)); | 
| 44673 | 163 | |
| 164 | val _ = | |
| 73225 
3ab0cedaccad
clarified modules: allow early definition of protocol commands;
 wenzelm parents: 
72946diff
changeset | 165 | Protocol_Command.define "Document.dialog_result" | 
| 50500 
c94bba7906d2
identify dialogs via official serial and maintain as result message;
 wenzelm parents: 
50498diff
changeset | 166 | (fn [serial, result] => | 
| 63806 | 167 | Active.dialog_result (Value.parse_int serial) result | 
| 62505 | 168 | handle exn => if Exn.is_interrupt exn then () (*sic!*) else Exn.reraise exn); | 
| 50498 | 169 | |
| 56616 
abc2da18d08d
added protocol command "use_theories", with core functionality of batch build;
 wenzelm parents: 
56458diff
changeset | 170 | val _ = | 
| 73225 
3ab0cedaccad
clarified modules: allow early definition of protocol commands;
 wenzelm parents: 
72946diff
changeset | 171 | Protocol_Command.define "ML_Heap.full_gc" | 
| 72146 | 172 | (fn [] => ML_Heap.full_gc ()); | 
| 173 | ||
| 174 | val _ = | |
| 73225 
3ab0cedaccad
clarified modules: allow early definition of protocol commands;
 wenzelm parents: 
72946diff
changeset | 175 | Protocol_Command.define "ML_Heap.share_common_data" | 
| 62467 | 176 | (fn [] => ML_Heap.share_common_data ()); | 
| 57868 
0b954ac94827
protocol command for heap management, e.g. in Isabelle/jEdit/Scala console: PIDE.session.protocol_command("ML_System.share_common_data");
 wenzelm parents: 
56616diff
changeset | 177 | |
| 38412 
c23f3abbf42d
moved isar_document.ML/scala to Pure/System/ -- side-by-side with isar.ML;
 wenzelm parents: diff
changeset | 178 | end; |