Skip to content

Latest commit

 

History

History
729 lines (663 loc) · 22 KB

CoqProtocol.md

File metadata and controls

729 lines (663 loc) · 22 KB

#This documentation is moving to the project wiki.

#CoqTop XML Protocol#

This documentation aims to provide a "hands on" description of the XML protocol that coqtop and coqide use to communicate. A somewhat out-of-date description of the async state machine is documented here. Typings for the protocol can be found here.

Sentences: each command sent to CoqTop is a "sentence"; they are typically terminated by ".\s" (followed by whitespace or EOF). Examples: "Lemma a: True.", "(* asdf *) Qed.", "auto; reflexivity." In practice, the command sentences sent to CoqTop are terminated at the "." and start with any previous whitespace. Each sentence is assigned a unique stateId after being sent to Coq (via Add). States:

  • Processing: has been received by Coq and has no obvious syntax error (that would prevent future parsing)
  • Processed:
  • InProgress:
  • Incomplete: the validity of the sentence cannot be checked due to a prior error
  • Complete:
  • Error: the sentence has an error error

State ID 0 is reserved as 'null' or 'default' state. (The 'query' command suggests that it might also refer to the currently-focused state, but I have not tested this yet). The first command should be added to state ID 0. Queries are typically performed w.r.t. state ID 0.


Adds a toplevel command (e.g. vernacular, definition, tactic) to the given state. verbose controls whether out-of-band messages will be generated for the added command (e.g. "foo is assumed" in response to adding "Axiom foo: nat.").

<call val="Add">
  <pair>
    <pair>
      <string>${command}</string>
      <int>${editId}</int>
    </pair>
    <pair>
      <state_id val="${stateId}"/>
      <bool val="${verbose}"/>
    </pair>
  </pair>
</call>

Returns

  • The added command is given a fresh stateId and becomes the next "tip".
<value val="good">
  <pair>
    <state_id val="${newStateId}"/>
    <pair>
      <union val="in_l"><unit/></union>
      <string>${message}</string>
    </pair>
  </pair>
</value>
  • When closing a focused proof (in the middle of a bunch of interpreted commands), the Qed will be assigned a prior stateId and nextStateId will be the id of an already-interpreted state that should become the next tip.
<value val="good">
  <pair>
    <state_id val="${stateId}"/>
    <pair>
      <union val="in_r"><state_id val="${nextStateId}"/></union>
      <string>${message}</string>
    </pair>
  </pair>
</value>
  • Failure:
    • Syntax error. Error offsets are with respect to the start of the sentence.
    <value val="fail"
        loc_s="${startOffsetOfError}"
        loc_e="${endOffsetOfError}">
      <state_id val="${stateId}"/>
      ${errorMessage}
    </value>
    • Another error (e.g. Qed before goal complete)
    <value val="fail"><state_id val="${stateId}"/>${errorMessage}</value>

Move focus to ${stateId}, such that commands may be added to the new state ID.

<call val="Edit_at"><state_id val="${stateId}"/></call>

Returns

  • Simple backtrack; focused stateId becomes the parent state
<value val="good">
  <union val="in_l"><unit/></union>
</value>
  • New focus; focusedQedStateId is the closing Qed of the new focus; senteneces between the two should be cleared
<value val="good">
  <union val="in_r">
    <pair>
      <state_id val="${focusedStateId}"/>
      <pair>
        <state_id val="${focusedQedStateId}"/>
        <state_id val="${oldFocusedStateId}"/>
      </pair>
    </pair>
  </union>
</value>
  • Failure: If stateId is in an error-state and cannot be jumped to, errorFreeStateId is the parent state of ``stateId` that shopuld be edited instead.
<value val="fail" loc_s="${startOffsetOfError}" loc_e="${endOffsetOfError}">
  <state_id val="${errorFreeStateId}"/>
  ${errorMessage}
</value>

  • No options.
<call val="Init"><option val="none"/></call>
  • With options. Looking at ide_slave.ml, it seems that options is just the name of a *.v file, whose path is added via Add LoadPath to the initial state.
<call val="Init">
  <option val="some">
    <string>${options}</string>
  </option>
</call>

Returns

  • The initial stateId (not associated with a sentence)
<value val="good">
  <state_id val="${initialStateId}"/>
</value>

<call val="Goal"><unit/></call>

Returns

  • If there is a goal. shelvedGoals and abandonedGoals have the same structure as the first set of (current/foreground) goals. backgroundGoals contains a list of pairs of lists of goals (list ((list Goal)*(list Goal))); it represents a "focus stack" (see code for reference). Each time a proof is focused, it will add a new pair of lists-of-goals. The first pair is the most nested set of background goals, the last pair is the top level set of background goals. The first list in the pair is in reverse order. Each time you focus the goal (e.g. using Focus or a bullet), a new pair will be prefixed to the list.
<value val="good">
  <option val="some">
  <goals>
    <!-- current goals -->
    <list>
      <goal>
        <string>3</string>
        <list>
          <string>${hyp1}</string>
          ...
          <string>${hypN}</string>
        </list>
        <string>${goal}</string>
      </goal>
      ...
      ${goalN}
    </list>
    <!-- `backgroundGoals` -->
    <list>
      <pair>
        <list><goal />...</list>
        <list><goal />...</list>
      </pair>
      ...
    </list>
    ${shelvedGoals}
    ${abandonedGoals}
  </goals>
  </option>
</value>

For example, this script:

Goal P -> (1=1/\2=2) /\ (3=3 /\ (4=4 /\ 5=5) /\ 6=6) /\ 7=7.
intros.
split; split. (* current visible goals are [1=1, 2=2, 3=3/\(4=4/\5=5)/\6=6, 7=7] *)
Focus 3. (* focus on 3=3/\(4=4/\5=5)/\6=6; bg-before: [1=1, 2=2], bg-after: [7=7] *)
split; [ | split ]. (* current visible goals are [3=3, 4=4/\5=5, 6=6] *)
Focus 2. (* focus on 4=4/\5=5; bg-before: [3=3], bg-after: [6=6] *)
* (* focus again on 4=4/\5=5; bg-before: [], bg-after: [] *)
split. (* current visible goals are [4=4,5=5] *)

should generate the following goals structure:

goals: [ P|-4=4, P|-5=5 ]
background:
[
  ( [], [] ), (* bullet with one goal has no before or after background goals *)
  ( [ P|-3=3 ], [ P|-6=6 ] ), (* Focus 2 *)
  ( [ P|-2=2, P|-1=1 ], [ P|-7=7 ] ) (* Focus 3; notice that 1=1 and 2=2 are reversed *)
]

Pseudocode for listing all of the goals in order: rev (flat_map fst background) ++ goals ++ flat_map snd background.

  • No goal:
<value val="good"><option val="none"/></value>

CoqIDE typically sets force to false.

<call val="Status"><bool val="${force}"/></call>

Returns

<status>
  <string>${path}</string>
  <string>${proofName}</string>
  <string>${allProofs}</string>
  <string>${proofNumber}</string>
</status>

In practice, stateId is 0, but the effect is to perform the query on the currently-focused state.

<call val="Query">
  <pair>
    <string>${query}</string>
    <state_id val="${stateId}"/>
  </pair>
</call>

Returns

<value val="good">
  <string>${message}</string>
</value>

<call val="Evars"><unit/></call>

Returns

<value val="good">
  <option val="some">
    <list>
      <evar>${evar1}</evar>
      ...
      <evar>${evarN}</evar>
    </list>
  </option>
</value>

<call val="Hints"><unit/></call>

Returns

<value val="good">
  <option val="some">
    <pair>
      <list/>
      <list>
        <pair>
          <string>${hint1}</string>
          <string>${hint2}</string>
        </pair>
        ...
        <pair>
          <string>${hintN-1}</string>
          <string>${hintN}</string>
        </pair>
      </list>
    </pair>
  </option>
</value>

Searches for objects that satisfy a list of constraints. If ${positiveConstraint} is false, then the constraint is inverted.

<call val="Search">
  <list>
    <pair>
      <search_cst val="${constraintType1}">
        ${constraintValue1}
      </search_cst>
      <bool val="${positiveConstraint1}"/>
    </pair>
    ...
    <!-- Example: -->
    <pair>
      <search_cst val="name_pattern">
        <string>bool_rect</string>
      </search_cst>
      <bool val="true"/>
    </pair>
  </list>
</call>

Returns

<value val="good">
  <list>
      <coq_object>
          <list>
              <string>${metaInfo}</string>
              ...
          </list>
          <list>
              <string>${name}</string>
          </list>
          <string>${definition}</string>
      </coq_object>
      ...
  </list>
</value>
Types of constraints:
  • Name pattern: ${constraintType} = "name_pattern"; ${constraintValue} is a regular expression string.
  • Type pattern: ${constraintType} = "type_pattern"; ${constraintValue} is a pattern (???: an open gallina term) string.
  • SubType pattern: ${constraintType} = "subtype_pattern"; ${constraintValue} is a pattern (???: an open gallina term) string.
  • In module: ${constraintType} = "in_module"; ${constraintValue} is a list of strings specifying the module/directory structure.
  • Include blacklist: ${constraintType} = "include_blacklist"; ${constraintValue} is ommitted.

<call val="GetOptions"><unit/></call>

Returns

<value val="good">
  <list>
    <pair>
      <list><string>${string1}</string>...</list>
      <option_state>
        <bool>${sync}</bool>
        <bool>${deprecated}</bool>
        <string>${name}</string>
        ${option_value}
      </option_state>
    </pair>
    ...
  </list>
</value>

Sends a list of option settings, where each setting roughly looks like: ([optionNamePart1, ..., optionNamePartN], value).

<call val="SetOptions">
  <list>
    <pair>
      <list>
        <string>optionNamePart1</string>
        ...
        <string>optionNamePartN</string>
      </list>
      <option_value val="${typeOfOption}">
        <option val="some">
          ${value}
        </option>
      </option_value>
    </pair>
    ...
    <!-- Example: -->
    <pair>
      <list>
        <string>Printing</string>
        <string>Width</string>
      </list>
      <option_value val="intvalue">
        <option val="some"><int>60</int></option>
      </option_value>
    </pair>
  </list>
</call>

CoqIDE sends the following settings (defaults in parentheses):

Printing Width : (<option_value val="intvalue"><int>60</int></option_value>),
Printing Coercions : (<option_value val="boolvalue"><bool val="false"/></option_value>),
Printing Matching : (...true...)
Printing Notations : (...true...)
Printing Existential Instances : (...false...)
Printing Implicit : (...false...)
Printing All : (...false...)
Printing Universes : (...false...)

Returns

<value val="good"><unit/></value>

<call val="MkCases"><string>...</string></call>

Returns

<value val="good">
  <list>
    <list><string>${string1}</string>...</list>
    ...
  </list>
</value>

<call val="StopWorker"><string>${worker}</string></call>

Returns

<value val="good"><unit/></value>

<call val="PrintAst"><state_id val="${stateId}"/></call>

Returns

<value val="good">
  <gallina begin="${gallina_begin}" end="${gallina_end}">
    <theorem begin="${theorem_begin}" end="${theorem_end}" type="Theorem" name="${theorem_name}">
      <apply begin="${apply_begin}" end="${apply_end}">
        <operator begin="${operator_begin}" end="${operator_end}" name="${operator_name}"/>
        <typed begin="${typed_begin}" end="${typed_end}">
          <constant begin="${constant_begin}" end="${constant_end}" name="${constant_name}"/>
          ...
          <token begin="${token_begin}" end="token_end">${token}</token>
          ...
        </typed>
        ...
      </apply>
    </theorem>
    ...
  </gallina>
</value>

<call val="Annotate"><string>${annotation}</string></call>

Returns

take <call val="Annotate"><string>Theorem plus_0_r : forall n : nat, n + 0 = n.</string></call> as an example.

<value val="good">
  <pp startpos="0" endpos="45">
    <vernac_expr startpos="0" endpos="44">
      <keyword startpos="0" endpos="7">Theorem</keyword>
      &nbsp;plus_0_r&nbsp;:&nbsp;
      <constr_expr startpos="19" endpos="44">
        <keyword startpos="19" endpos="25">forall</keyword>
        &nbsp;n&nbsp;:&nbsp;
        <constr_expr startpos="30" endpos="33">nat</constr_expr>
        ,&nbsp;
        <unparsing startpos="35" endpos="44">
          <unparsing startpos="35" endpos="40">
            <unparsing startpos="35" endpos="40">
              <unparsing startpos="35" endpos="36">
                <constr_expr startpos="35" endpos="36">n</constr_expr>
              </unparsing>
              <unparsing startpos="36" endpos="38">&nbsp;+</unparsing>
              <unparsing startpos="38" endpos="39">&nbsp;</unparsing>
              <unparsing startpos="39" endpos="40">
                <constr_expr startpos="39" endpos="40">0</constr_expr>
              </unparsing>
            </unparsing>
          </unparsing>
          <unparsing startpos="40" endpos="42">&nbsp;=</unparsing>
          <unparsing startpos="42" endpos="43">&nbsp;</unparsing>
          <unparsing startpos="43" endpos="44">
            <constr_expr startpos="43" endpos="44">n</constr_expr>
          </unparsing>
        </unparsing>
      </constr_expr>
    </vernac_expr>
    .
  </pp>
</value>

Feedback messages are issued out-of-band, giving updates on the current state of sentences/stateIds, worker-thread status, etc.

  • Added Axiom: in response to Axiom, admit, Admitted, etc.
<feedback object="state" route="0">
  <state_id val="${stateId}"/>
  <feedback_content val="addedaxiom" />
</feedback>
<feedback object="state" route="0">
  <state_id val="${stateId}"/>
  <feedback_content val="processingin">
    <string>${workerName}</string>
  </feedback_content>
</feedback>
<feedback object="state" route="0">
  <feedback object="state" route="0">
    <state_id val="${stateId}"/>
  <feedback_content val="processed"/>
</feedback>
<feedback object="state" route="0">
  <state_id val="${stateId}"/>
  <feedback_content val="incomplete" />
</feedback>
  • Complete
  • GlobRef
  • Error. Issued, for example, when a processed tactic has failed or is unknown. The error offsets may both be 0 if there is no particular syntax involved.
<feedback object="state" route="0">
  <state_id val="${stateId}"/>
  <feedback_content val="errormsg">
    <loc start="${sentenceOffsetBegin}" stop="${sentenceOffsetEnd}"/>
    <string>${errorMessage}</string>
  </feedback_content>
</feedback>
<feedback object="state" route="0">
  <state_id val="${stateId}"/>
  <feedback_content val="inprogress">
    <int>1</int>
  </feedback_content>
</feedback>
  • WorkerStatus Ex: workername = "proofworker:0" Ex: status = "Idle" or status = "proof: myLemmaName" or status = "Dead"
<feedback object="state" route="0">
  <state_id val="${stateId}"/>
  <feedback_content val="workerstatus">
    <pair>
      <string>${workerName}</string>
      <string>${status}</string>
    </pair>
  </feedback_content>
</feedback>
  • File Dependencies. Typically in response to a Require. Dependencies are *.vo files.
    • State stateId directly depends on dependency:
    <feedback object="state" route="0">
      <state_id val="${stateId}"/>
      <feedback_content val="filedependency">
        <option val="none"/>
        <string>${dependency}</string>
      </feedback_content>
    </feedback>
    • State stateId depends on dependency via dependency sourceDependency
    <feedback object="state" route="0">
      <state_id val="${stateId}"/>
      <feedback_content val="filedependency">
        <option val="some"><string>${sourceDependency}</string></option>
        <string>${dependency}</string>
      </feedback_content>
    </feedback>
  • File Loaded. For state stateId, module module is being loaded from voFileName
<feedback object="state" route="0">
  <state_id val="${stateId}"/>
  <feedback_content val="fileloaded">
    <string>${module}</string>
    <string>${voFileName`}</string>
  </feedback_content>
</feedback>
  • Message. level is one of {info,warning,notice,error,debug}. E.g. in response to an add "Axiom foo: nat." with verbose=true, message foo is assumed will be emitted in response.
<feedback object="state" route="0">
  <state_id val="${stateId}"/>
  <feedback_content val="message">
    <message>
      <message_level val="${level}"/>
      <string>${message}</string>
    </message>
  </feedback_content>
</feedback>
  • Custom. A feedback message that Coq plugins can use to return structured results. Optionally, startPos and stopPos define a range of offsets in the document that the message refers to; otherwise, they will be 0. customTag is indended as a unique string that identifies what kind of payload is contained in customXML.
<feedback object="state" route="0">
  <state_id val="${stateId}"/>
  <feedback_content val="custom">
    <loc start="${startPos}" stop="${stopPos}"/>
    <string>${customTag}</string>
    ${customXML}
  </feedback_content>
</feedback>
  • LtacProf. As of 8.6, the ltac profiler (LtacProf) will generate an additional feedback message in response to "Show Ltac Profile" with the full, structured profiling results. <ltacprof_tactic /> forms a tree of tactic invocations and their profiling results. When a tactic has multiple invocations of e.g. tactic "foo", the profiling results for "foo" under the tactic will be combined together. Each tactic entry in <ltacprof/> represents a tactic that was run at the top level, where multiple invocations of the same tactic are combined together. totalTimeSec is total time taken by all of the tactics. tacticName is the name of the tactic that the entry corresponds to. totalSec is the total time taken be a tactic over all invocations made by its parent tactic. selfSec is the portion of the time running the tactic itself, as opposed to running subtactics. num_calls is the number of invocations of the tactic that have been made by its parent. max_total is the maximum time spent in the tactic by a single invocation from its parent.
<feedback object="state" route="0">
  <state_id val="${stateId}"/>
  <feedback_content val="custom">
    <loc start="0" stop="0"/>
    <string>ltacprof_results</string>
    <ltacprof total_time="${totalTimeSec}">
      <ltacprof_tactic name="${tacticName1}" total="${totalSec1}" self="${selfSec1}" num_calls="${num_calls1}" max_total="${max_totalSec1}">
        <ltacprof_tactic ... />...
      </ltacprof_tactic>
      ...
    </ltacprof>
  </feedback_content>
</feedback>