From 88c369a2715f0d0cdd7e294f4c11d9944a11cc6a Mon Sep 17 00:00:00 2001 From: Ryan Scott Date: Sun, 30 May 2021 10:55:01 -0400 Subject: [PATCH] Autogenerate cryptol-remote-api docs (and check them in CI) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unfortunately, the autogenerated documentation (now located in `Cryptol.rst`) doesn't yet have all of the information contained within the hand-written documentation (now located in `old-Cryptol.rst`—see #1206. In pursuit of eventually fixing that issue, the CI now makes sure that any changes to the autogenerated documentation are checked in. --- .github/ci.sh | 4 + .github/workflows/ci.yml | 3 + cry | 7 + cryptol-remote-api/check_docs.sh | 14 + cryptol-remote-api/docs/.gitignore | 1 + cryptol-remote-api/docs/Cryptol.rst | 772 ++++++++++++++++-------- cryptol-remote-api/docs/old-Cryptol.rst | 353 +++++++++++ 7 files changed, 893 insertions(+), 261 deletions(-) create mode 100755 cryptol-remote-api/check_docs.sh create mode 100644 cryptol-remote-api/docs/old-Cryptol.rst diff --git a/.github/ci.sh b/.github/ci.sh index f48171fe..418be505 100755 --- a/.github/ci.sh +++ b/.github/ci.sh @@ -147,6 +147,10 @@ test_rpc() { ./cry rpc-test } +check_rpc_docs() { + ./cry rpc-docs +} + bundle_files() { doc=dist/share/doc/cryptol lib=dist/share/cryptol diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2e5ba3f7..ec1b2b14 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -129,6 +129,9 @@ jobs: - shell: bash run: .github/ci.sh test_rpc if: runner.os != 'Windows' + - shell: bash + run: .github/ci.sh check_rpc_docs + if: runner.os != 'Windows' - if: matrix.ghc == '8.8.4' uses: actions/upload-artifact@v2 diff --git a/cry b/cry index bba30b7f..5566a095 100755 --- a/cry +++ b/cry @@ -21,6 +21,7 @@ Available commands: test Run some tests (may take a while) quick-test Like "test" but run fewer tests by default rpc-test Run RPC server tests + rpc-docs Check that the RPC documentation is up-to-date exe-path Print the location of the local executable EOM } @@ -90,6 +91,12 @@ case $COMMAND in $DIR/cryptol-remote-api/run_rpc_tests.sh ;; + rpc-docs) + echo "Checking cryptol-remote-api docs (Cryptol.rst) are up-to-date with server" + DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + $DIR/cryptol-remote-api/check_docs.sh + ;; + help) show_usage && exit 0 ;; diff --git a/cryptol-remote-api/check_docs.sh b/cryptol-remote-api/check_docs.sh new file mode 100755 index 00000000..d6ae3b1b --- /dev/null +++ b/cryptol-remote-api/check_docs.sh @@ -0,0 +1,14 @@ +#! /bin/bash + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +cd $DIR/docs + +export CRYPTOL_SERVER=$(cabal v2-exec which cryptol-remote-api) +if [[ ! -x "$CRYPTOL_SERVER" ]]; then + echo "could not locate cryptol-remote-api executable - try executing with cabal v2-exec" + exit 1 +fi + +$CRYPTOL_SERVER doc > TEMP.rst +diff Cryptol.rst TEMP.rst diff --git a/cryptol-remote-api/docs/.gitignore b/cryptol-remote-api/docs/.gitignore index 69fa449d..0b51bc8e 100644 --- a/cryptol-remote-api/docs/.gitignore +++ b/cryptol-remote-api/docs/.gitignore @@ -1 +1,2 @@ _build/ +TEMP.rst diff --git a/cryptol-remote-api/docs/Cryptol.rst b/cryptol-remote-api/docs/Cryptol.rst index bcebebf0..a59026bd 100644 --- a/cryptol-remote-api/docs/Cryptol.rst +++ b/cryptol-remote-api/docs/Cryptol.rst @@ -1,353 +1,603 @@ -================== -Cryptol Evaluation +Cryptol RPC Server ================== -All methods in this section additionally propagate server state in the -manner described in the prior section. - -These methods may return :ref:`a variety of Cryptol errors -`, with codes in the range of ``20000``-``29999``. - -Options -======= - -Many of the options that can be set using the ``:set`` command at the -Cryptol REPL can be provided as parameters to methods in this API. In -addition to the listed fields, every command accepts an optional -``options`` parameter. Its value should be an object, and may have -zero or more of the following fields: - - - ``call stacks``: A Boolean that determines whether to track call stacks. - - ``output``: An object that contains further parameters to control strings generated by Cryptol (these are visible in e.g. error messages, but do not affect the structured data returned in the API): - + ``ASCII``: A Boolean (default ``true``). - + ``base``: What base to display numbers in (default ``10``). - + ``prefix of infinite lengths``: How much of an infinite sequence to display (default ``5``. - + ``floating point base``: What base to display floating point numbers in (default ``10``). - + ``floating point format``: The output format used to display floating point numbers (accepts strings in the same format as the argument to ``:set fpFormat`` in the REPL). - -Module Management -================= - -Changing Directories +Fundamental Protocol -------------------- -:Method name: - ``change directory`` -:Parameters: - - ``directory``: The new working directory, represented as a string. +This application is a `JSON-RPC `_ server. Additionally, it maintains a persistent cache of application states and explicitly indicates the state in which each command is to be carried out. -Loading Modules ---------------- +Transport +~~~~~~~~~ -:Method name: - ``load module`` -:Parameters: - - ``module name``: The name of the Cryptol module to be loaded. +The server supports three transport methods: -Loading Files -------------- -:Method name: - ``load file`` -:Parameters: - - ``file``: The name of the Cryptol source file to be loaded. +``stdio`` + in which the server communicates over ``stdin`` and ``stdout`` + + -Module Context --------------- +Socket + in which the server communicates over ``stdin`` and ``stdout`` + + -:Method name: - ``focused module`` -:Parameters: none -:Return fields: - - ``module``: The name of the focused module, which would be shown in the - prompt in the Cryptol REPL, or ``null`` if there is no such focused module. - - ``parameterized``: A Boolean value indicating whether the focused module is - parameterized. This field is only present when the module name is not - ``null``. +HTTP + in which the server communicates over HTTP + + +In both ``stdio`` and socket mode, messages are delimited using `netstrings. `_ -Evaluation and Typechecking -=========================== -Evaluating Expressions ----------------------- +Application State +~~~~~~~~~~~~~~~~~ -This method evaluates a Cryptol expression. The type of the expression -needs to be fully-determined and finite - that is, functions and -infinite streams are not supported, and neither is polymorphism. +According to the JSON-RPC specification, the ``params`` field in a message object must be an array or object. In this protocol, it is always an object. While each message may specify its own arguments, every message has a parameter field named ``state``. -:Method name: - ``evaluate expression`` -:Parameters: - - ``expression``: The :ref:`JSON Cryptol expression ` to be evaluated -:Return fields: - - ``value``: A :ref:`JSON Cryptol expression ` that denotes the value - - ``type``: A :ref:`JSON Cryptol type ` that denotes the result type - - ``type string``: A human-readable representation of the result type +When the first message is sent from the client to the server, the ``state`` parameter should be initialized to the JSON null value ``null``. Replies from the server may contain a new state that should be used in subsequent requests, so that state changes executed by the request are visible. Prior versions of this protocol represented the initial state as the empty array ``[]``, but this is now deprecated and will be removed. -Calling Functions ------------------ +In particular, per JSON-RPC, non-error replies are always a JSON object that contains a ``result`` field. The result field always contains an ``answer`` field and a ``state`` field, as well as ``stdout`` and ``stderr``. -Note: this method may be removed in the future, because its abilities -have been subsumed by ``evaluate expression``. -This method applies a Cryptol function to some arguments. The type of -the resulting expression needs to be fully-determined and finite - -that is, functions and infinite streams are not supported, and neither -is polymorphism. +``answer`` + The value returned as a response to the request (the precise contents depend on which request was sent) + + -:Method name: - ``call`` -:Parameters: - - ``function``: The name of a Cryptol function that is currently in scope - - ``arguments``: A list of arguments to the function, encoded as JSON - Cryptol expressions -:Return fields: - - ``value``: A :ref:`JSON Cryptol expression ` that denotes the value - - ``type``: A :ref:`JSON Cryptol type ` that denotes the result type - - ``type string``: A human-readable representation of the result type +``state`` + The state, to be sent in subsequent requests. If the server did not modify its state in response to the command, then this state may be the same as the one sent by the client. + + -Visible Names -------------- +``stdout`` and ``stderr`` + These fields contain the contents of the Unix ``stdout`` and ``stderr`` file descriptors. They are intended as a stopgap measure for clients who are still in the process of obtaining structured information from the libraries on which they depend, so that information is not completely lost to users. However, the server may or may not cache this information and resend it. Applications are encouraged to used structured data and send it deliberately as the answer. + + +The precise structure of states is considered an implementation detail that could change at any time. Please treat them as opaque tokens that may be saved and re-used within a given server process, but not created by the client directly. -Return information about all names in scope. -:Method name: - ``visible names`` -:Parameters: none -:Return value: - A list of name information objects. Each name information object has the following - fields: - - ``name``: A human-readable representation of the name - - ``type string``: A human-readable representation of the name's type schema - - ``type``: A :ref:`JSON Cryptol type ` +Summary +------- - Some will additionally have the following field: +An RCP server for `Cryptol `_ that supports type checking and evaluation of Cryptol code via the methods documented below. - - ``documentation``: The documentation string for the name, if it is documented - -Checking Types --------------- - -Check the type of an expression. - -:Method name: - ``check type`` -:Parameters: - - ``expression``: A :ref:`JSON Cryptol expression ` for which a type is desired. -:Return fields: - - ``type schema``: A :ref:`JSON Cryptol type ` - -SAT ---- - -This method is not yet ready for public consumption. Terms and Types -=============== - -.. _cryptol-json-expression: +--------------- +.. _Expression: JSON Cryptol Expressions ------------------------- +~~~~~~~~~~~~~~~~~~~~~~~~ In the API, Cryptol expressions can be represented by the following: + JSON Booleans Represent the corresponding Cryptol Booleans + + JSON Integers Cryptol integer literals, that can be used at a variety of types + + JSON Strings Cryptol concrete syntax + + JSON Objects - Objects can represent a variety of Cryptol expressions. The field - ``expression`` contains a tag that can be used to determine the - remaining fields. - + Objects can represent a variety of Cryptol expressions. The field ``expression`` contains a tag that can be used to determine the remaining fields. + + The tag values in objects can be: -``bits`` - The expression is a bitvector. Further fields are: - + ``encoding``: Either the string ``base64`` or ``hex``, for base-64 or hexadecimal - representations of the bitvector - + ``data``: A string containing the actual data - + ``width``: An integer: the bit-width of the represented bit vector +``bits`` + + * The expression is a bitvector. Further fields are: + + + + * ``encoding``: Either the string ``base64`` or ``hex``, for base-64 or hexadecimal representations of the bitvector + + + + * ``data``: A string containing the actual data + + + + * ``width``: An integer: the bit-width of the represented bit vector + + + ``record`` - The expression is a record. The field ``data`` is a JSON - object that maps record field names to :ref:`JSON Cryptol expressions `. + The expression is a record. The field ``data`` is a JSON object that maps record field names to :ref:`JSON Cryptol expressions `. + + ``sequence`` - The expression is a sequence. The field ``data`` contains a - JSON array of the elements of the sequence; each is a JSON Cryptol - expression. + The expression is a sequence. The field ``data``contains a JSON array of the elements of the sequence; each is a JSON Cryptol expression. + + ``tuple`` - The expression is a tuple. The field ``data`` contains a JSON - array of the elements of the tuple; each is a JSON Cryptol - expression. + The expression is a tuple. The field ``data``contains a JSON array of the elements of the tuple; each is a JSON Cryptol expression. + + ``unit`` The expression is the unit constructor, and there are no further fields. + + ``let`` - The expression is a ``where`` binding. The fields are: - - ``binders`` - A list of binders. Each binder is an object with two fields: - - - ``name``: A string that is the name to be bound, and - - ``definition``: A :ref:`JSON Cryptol expression `. - - ``body`` - A :ref:`JSON Cryptol expression ` in which the bound names may be used. + + * The expression is a ``where``binding. The fields are: + + + + * + ``binders`` + + * A list of binders. Each binder is an object with two fields: + + + + * ``name``: A string that is the name to be bound, and + + + + * ``definition``A :ref:`JSON Cryptol expression `. + + + + + ``body`` + A :ref:`JSON Cryptol expression ` in which the bound names may be used. + + + + ``call`` - The expression is a function application. Further fields are: - - - ``function``: A :ref:`JSON Cryptol expressions `. - - ``arguments``: A JSON array of :ref:`JSON Cryptol expressions `. + + * The expression is a function application. Further fields are: + + + + * ``function``: A :ref:`JSON Cryptol expression `. + + + + * ``arguments``: A JSON array of :ref:`JSON Cryptol expressions `. + + + ``instantiate`` - The expression is a type application. Further fields are: - - - ``generic``: The polymorphic expression to be instantiated - - ``arguments``: A JSON object in which keys are the names of type parameters and values are :ref:`JSON Cryptol types `. + + * The expression is a type application. Further fields are: + + + + * ``generic``: The polymorphic expression to be instantiated + + + + * ``arguments``: A JSON object in which keys are the names of type parameters and values are :ref:`JSON Cryptol types `. + + + ``integer modulo`` - The expression is an integer with a modulus (the Cryptol ``Z`` type). Further fields are: - - - ``integer``: A JSON number, representing the integer - - ``modulus``: A JSON number, representing the modulus - -.. _cryptol-json-type: + + * The expression is an integer with a modulus (the Cryptol ``Z`` type). Further fields are: + + + + * ``integer``: A JSON number, representing the integer + + + + * ``modulus``: A JSON number, representing the modulus + + + +.. _JSONSchema: JSON Cryptol Types ------------------- +~~~~~~~~~~~~~~~~~~ + +JSON representations of types are type schemas. A type schema has three fields: -JSON representations of types are type schemas. A type schema has -three fields: ``forall`` - - Contains an array of objects. Each object has two fields: ``name`` - is the name of a type variable, and ``kind`` is its kind. There - are four kind formers: the string ``Type`` represents ordinary - datatypes, the string ``Num`` is the kind of numbers, and - ``Prop`` is the kind of propositions. Arrow kinds are represented - by objects in which the field ``kind`` is the string ``arrow``, - and the fields ``from`` and ``to`` are the kinds on the left and - right side of the arrow, respectively. + Contains an array of objects. Each object has two fields: ``name`` is the name of a type variable, and ``kind`` is its kind. There are four kind formers: the string ``Type`` represents ordinary datatypes, the string ``Num`` is the kind of numbers, and ``Prop`` is the kind of propositions. Arrow kinds are represented by objects in which the field ``kind`` is the string ``arrow``, and the fields ``from`` and ``to`` are the kinds on the left and right side of the arrow, respectively. + + ``propositions`` A JSON array of the constraints in the type. + + ``type`` - The type in which the variables from ``forall`` are in scope and - the constraints in ``propositions`` are in effect. + The type in which the variables from ``forall`` are in scope and the constraints in ``propositions`` are in effect. + + -Concrete Types + +Methods +------- + +check (command) +~~~~~~~~~~~~~~~ + +Tests a property against random values to give quick feedback. + +Parameter fields +++++++++++++++++ + + +``expression`` + The predicate (i.e., function) to check; must be a monomorphic function with return type Bit. + + + +``number of tests`` + The number of random inputs to test the property with, or ``all`` to exhaustively check the property (defaults to ``100`` if not provided). If ``all`` is specified and the property's argument types are not sufficiently small, checking may take longer than you are willing to wait! + + + +Return fields ++++++++++++++ + + +``tests run`` + The number of tests that were successfully run. + + + +``tests possible`` + The maximum number of possible tests. + + + +``result`` + The overall test result, represented as one of three string values:``pass`` (all tests succeeded), ``fail`` (a test evaluated to ``False``), or``error`` (an exception was raised during evaluation). + + + +``arguments`` + Only returned if the ``result`` is ``fail`` or ``error``. An array of JSON objects indicating the arguments passed to the property which triggered the failure or error. Each object has an ``expr`` field, which is an individual argument expression, and a ``type`` field, which is the type of the argument expression. + + + +``error message`` + Only returned if the ``result`` is ``error``. A human-readable representation of the exception that was raised during evaluation. + + + + +clear state (notification) +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Clear a particular state from the Cryptol server (making room for subsequent/unrelated states). + +Parameter fields +++++++++++++++++ + + +``state to clear`` + The state to clear from the server to make room for other unrelated states. + + + +Return fields ++++++++++++++ + +No return fields + + + +clear all states (notification) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Clear all states from the Cryptol server (making room for subsequent/unrelated states). + +Parameter fields +++++++++++++++++ + +No parameters + + +Return fields ++++++++++++++ + +No return fields + + + +extend search path (command) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Extend the server's search path with the given paths. + +Parameter fields +++++++++++++++++ + + +``paths`` + The paths to add to the search path. + + + +Return fields ++++++++++++++ + +No return fields + + + +load module (command) +~~~~~~~~~~~~~~~~~~~~~ + +Load the specified module (by name). + +Parameter fields +++++++++++++++++ + + +``module name`` + Name of module to load. + + + +Return fields ++++++++++++++ + +No return fields + + + +load file (command) +~~~~~~~~~~~~~~~~~~~ + +Load the specified module (by file path). + +Parameter fields +++++++++++++++++ + + +``file`` + File path of the module to load. + + + +Return fields ++++++++++++++ + +No return fields + + + +focused module (command) +~~~~~~~~~~~~~~~~~~~~~~~~ + +The 'current' module. Used to decide how to print names, for example. + +Parameter fields +++++++++++++++++ + +No parameters + + +Return fields ++++++++++++++ + + +``module`` + The name of the focused module, which would be shown in the prompt in the Cryptol REPL, or ``null`` if there is no such focused module. + + + +``parameterized`` + A Boolean value indicating whether the focused module is parameterized. This field is only present when the module name is not ``null``. + + + + +evaluate expression (command) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Evaluate the Cryptol expression to a value. + +Parameter fields +++++++++++++++++ + + +``expression`` + The expression to evaluate. + + + +Return fields ++++++++++++++ + + +``value`` + A :ref:`JSON Cryptol expression ` that denotes the value + + + +``type`` + A:ref:`JSON Cryptol type ` that denotes the result type + + + +``type string`` + A human-readable representation of the result type + + + + +call (command) ~~~~~~~~~~~~~~ -Types are represented as JSON objects. The ``type`` field contains one of the following tags (represented as JSON strings): +Evaluate the result of calling a Cryptol function on one or more parameters. -``variable`` - The type is a type variable. The remaining fields are ``name``, - which contains the variable's name, and ``kind``, which contains - its kind (represented as in the ``forall`` section). When providing - types to Cryptol, the ``kind`` field should be elided, and type synonyms - may be provided with arguments through an optional ``arguments`` field. +Parameter fields +++++++++++++++++ -``record`` - The type is a record type. The remaining field is ``fields``, - which contains a JSON object whose keys are the names of fields and - whose values are the fields' types. - -``number`` - The type is a number. The field ``value`` contains the number - itself. - -``inf`` - The type is the infinite number. There are no further fields. - -``Bit`` - The type is the bit type. There are no further fields. - -``Integer`` - The type is the integer type. There are no further fields. - -``Rational`` - The type is the rational number type. There are no further fields. - -``Z`` - The type is integers modulo another value. The field ``modulus`` - contains the modulus, which is a type. - -``bitvector`` - The type is a bitvector. The field ``width`` contains the number - of bits, which is a type. - -``sequence`` - The type is a sequence. The field ``length`` contains the length - of the sequence (a type), and the field ``contents`` contains the - type of entries in the sequence. ``function`` - The type is a function type. The fields ``domain`` and ``range`` - contain the domain and range types. + The function being called. + + -``unit`` - The type is the unit type. There are no further fields. +``arguments`` + The arguments the function is being applied to. + + -``tuple`` - The type is a tuple. The field ``contents`` is a JSON array - containing the types of the projections from the tuple. +Return fields ++++++++++++++ -One of ``+``, ``-``, ``*``, ``/``, ``%``, ``^^``, ``width``, ``min``, ``max``, ``/^``, ``%^``, ``lengthFromThenTo`` - The type is an application of the indicated type function. The - arguments are contained in the ``arguments`` field, as a JSON - array. -Propositions -~~~~~~~~~~~~ +``value`` + A :ref:`JSON Cryptol expression ` that denotes the value + + -Propositions/constraints have the key ``prop``, mapped to one of the -following tags: +``type`` + A:ref:`JSON Cryptol type ` that denotes the result type + + -``==`` - Equality. The equated terms are in the ``left`` and ``right`` - fields. +``type string`` + A human-readable representation of the result type + + -``!=`` - Inequality. The disequated terms are in the ``left`` and - ``right`` fields. -``>=`` - Greater than. The greater type is in the ``greater`` field and the - lesser type is in the ``lesser`` field. +visible names (command) +~~~~~~~~~~~~~~~~~~~~~~~ -``fin`` - Finitude. The finite type is in the ``subject`` field. +List the currently visible (i.e., in scope) names. + +Parameter fields +++++++++++++++++ + +No parameters + + +Return fields ++++++++++++++ + + +``name`` + A human-readable representation of the name + + + +``type string`` + A human-readable representation of the name's type schema + + + +``type`` + A:ref:`JSON Cryptol type ` + + + +``documentation`` + An optional field containing documentation string for the name, if it is documented + + + + +check type (command) +~~~~~~~~~~~~~~~~~~~~ + +Check and return the type of the given expression. + +Parameter fields +++++++++++++++++ + + +``expression`` + Expression to type check. + + + +Return fields ++++++++++++++ + + +``type schema`` + A :ref:`JSON Cryptol Type ` + + + + +prove or satisfy (command) +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Find a value which satisfies the given predicate, or show that it is valid.(i.e., find a value which when passed as the argument produces true or show that for all possible arguments the predicate will produce true). + +Parameter fields +++++++++++++++++ + + +``prover`` + The SMT solver to use to check for satisfiability. I.e., one of the following: ``cvc4``, ``yices``, ``z3``, ``boolector``, ``mathsat``, ``abc``, ``offline``, ``any``, ``sbv-cvc4``, ``sbv-yices``, ``sbv-z3``, ``sbv-boolector``, ``sbv-mathsat``, ``sbv-abc``, ``sbv-offline``, ``sbv-any``, . + + + +``expression`` + The function to check for validity, satisfiability, or safety depending on the specified value for ``query type``. For validity and satisfiability checks, the function must be a predicate (i.e., monomorphic function with return type Bit). + + + +``result count`` + How many satisfying results to search for; either a positive integer or ``all``. Only affects satisfiability checks. + + + +``query type`` + Whether to attempt to prove the predicate is true for all possible inputs (``prove``), find some inputs which make the predicate true (``sat``), or prove a function is safe (``safe``). + + + +Return fields ++++++++++++++ + + +``result`` + A string (one of ``unsatisfiable``, ``invalid``, or ``satisfied``) indicating the result of checking for validity, satisfiability, or safety. + + + +``counterexample type`` + Only used if the ``result`` is ``invalid``.This describes the variety of counterexample that was produced. This can be either ``safety violation`` or ``predicate falsified``. + + + +``counterexample`` + Only used if the ``result`` is ``invalid``.A list of objects where each object has an ``expr``field, indicating a counterexample expression, and a ``type``field, indicating the type of the expression. + + + +``models`` + Only used if the ``result`` is ``satisfied``.A list of list of objects where each object has an ``expr``field, indicating a expression in a model, and a ``type``field, indicating the type of the expression. + + -``has`` - The selector is in the ``selector`` field, the type that has this - selector is in the ``type`` field, and the type expected for the - projection is in the ``is`` field. -``Arith``, ``Cmp``, ``SignedCmp``, ``Zero``, ``Logic`` - The type that has these operations defined is in the ``subject`` - field. -``Literal`` - The size is in the ``size`` field, and the type is in the - ``subject`` field. -``True`` - There are no further fields. -``And`` - The conjuncts are in the ``left`` and ``right`` fields. diff --git a/cryptol-remote-api/docs/old-Cryptol.rst b/cryptol-remote-api/docs/old-Cryptol.rst new file mode 100644 index 00000000..bcebebf0 --- /dev/null +++ b/cryptol-remote-api/docs/old-Cryptol.rst @@ -0,0 +1,353 @@ +================== +Cryptol Evaluation +================== + +All methods in this section additionally propagate server state in the +manner described in the prior section. + +These methods may return :ref:`a variety of Cryptol errors +`, with codes in the range of ``20000``-``29999``. + +Options +======= + +Many of the options that can be set using the ``:set`` command at the +Cryptol REPL can be provided as parameters to methods in this API. In +addition to the listed fields, every command accepts an optional +``options`` parameter. Its value should be an object, and may have +zero or more of the following fields: + + - ``call stacks``: A Boolean that determines whether to track call stacks. + - ``output``: An object that contains further parameters to control strings generated by Cryptol (these are visible in e.g. error messages, but do not affect the structured data returned in the API): + + ``ASCII``: A Boolean (default ``true``). + + ``base``: What base to display numbers in (default ``10``). + + ``prefix of infinite lengths``: How much of an infinite sequence to display (default ``5``. + + ``floating point base``: What base to display floating point numbers in (default ``10``). + + ``floating point format``: The output format used to display floating point numbers (accepts strings in the same format as the argument to ``:set fpFormat`` in the REPL). + +Module Management +================= + +Changing Directories +-------------------- + +:Method name: + ``change directory`` +:Parameters: + - ``directory``: The new working directory, represented as a string. + +Loading Modules +--------------- + +:Method name: + ``load module`` +:Parameters: + - ``module name``: The name of the Cryptol module to be loaded. + +Loading Files +------------- + +:Method name: + ``load file`` +:Parameters: + - ``file``: The name of the Cryptol source file to be loaded. + +Module Context +-------------- + +:Method name: + ``focused module`` +:Parameters: none +:Return fields: + - ``module``: The name of the focused module, which would be shown in the + prompt in the Cryptol REPL, or ``null`` if there is no such focused module. + - ``parameterized``: A Boolean value indicating whether the focused module is + parameterized. This field is only present when the module name is not + ``null``. + +Evaluation and Typechecking +=========================== + +Evaluating Expressions +---------------------- + +This method evaluates a Cryptol expression. The type of the expression +needs to be fully-determined and finite - that is, functions and +infinite streams are not supported, and neither is polymorphism. + +:Method name: + ``evaluate expression`` +:Parameters: + - ``expression``: The :ref:`JSON Cryptol expression ` to be evaluated +:Return fields: + - ``value``: A :ref:`JSON Cryptol expression ` that denotes the value + - ``type``: A :ref:`JSON Cryptol type ` that denotes the result type + - ``type string``: A human-readable representation of the result type + +Calling Functions +----------------- + +Note: this method may be removed in the future, because its abilities +have been subsumed by ``evaluate expression``. + +This method applies a Cryptol function to some arguments. The type of +the resulting expression needs to be fully-determined and finite - +that is, functions and infinite streams are not supported, and neither +is polymorphism. + +:Method name: + ``call`` +:Parameters: + - ``function``: The name of a Cryptol function that is currently in scope + - ``arguments``: A list of arguments to the function, encoded as JSON + Cryptol expressions +:Return fields: + - ``value``: A :ref:`JSON Cryptol expression ` that denotes the value + - ``type``: A :ref:`JSON Cryptol type ` that denotes the result type + - ``type string``: A human-readable representation of the result type + +Visible Names +------------- + +Return information about all names in scope. + +:Method name: + ``visible names`` +:Parameters: none +:Return value: + A list of name information objects. Each name information object has the following + fields: + + - ``name``: A human-readable representation of the name + - ``type string``: A human-readable representation of the name's type schema + - ``type``: A :ref:`JSON Cryptol type ` + + Some will additionally have the following field: + + - ``documentation``: The documentation string for the name, if it is documented + +Checking Types +-------------- + +Check the type of an expression. + +:Method name: + ``check type`` +:Parameters: + - ``expression``: A :ref:`JSON Cryptol expression ` for which a type is desired. +:Return fields: + - ``type schema``: A :ref:`JSON Cryptol type ` + +SAT +--- + +This method is not yet ready for public consumption. + +Terms and Types +=============== + +.. _cryptol-json-expression: + +JSON Cryptol Expressions +------------------------ + +In the API, Cryptol expressions can be represented by the following: + +JSON Booleans + Represent the corresponding Cryptol Booleans + +JSON Integers + Cryptol integer literals, that can be used at a variety of types + +JSON Strings + Cryptol concrete syntax + +JSON Objects + Objects can represent a variety of Cryptol expressions. The field + ``expression`` contains a tag that can be used to determine the + remaining fields. + +The tag values in objects can be: + +``bits`` + The expression is a bitvector. Further fields are: + + + ``encoding``: Either the string ``base64`` or ``hex``, for base-64 or hexadecimal + representations of the bitvector + + ``data``: A string containing the actual data + + ``width``: An integer: the bit-width of the represented bit vector + +``record`` + The expression is a record. The field ``data`` is a JSON + object that maps record field names to :ref:`JSON Cryptol expressions `. + +``sequence`` + The expression is a sequence. The field ``data`` contains a + JSON array of the elements of the sequence; each is a JSON Cryptol + expression. + +``tuple`` + The expression is a tuple. The field ``data`` contains a JSON + array of the elements of the tuple; each is a JSON Cryptol + expression. + +``unit`` + The expression is the unit constructor, and there are no further fields. + +``let`` + The expression is a ``where`` binding. The fields are: + + ``binders`` + A list of binders. Each binder is an object with two fields: + + - ``name``: A string that is the name to be bound, and + - ``definition``: A :ref:`JSON Cryptol expression `. + + ``body`` + A :ref:`JSON Cryptol expression ` in which the bound names may be used. + +``call`` + The expression is a function application. Further fields are: + + - ``function``: A :ref:`JSON Cryptol expressions `. + - ``arguments``: A JSON array of :ref:`JSON Cryptol expressions `. + +``instantiate`` + The expression is a type application. Further fields are: + + - ``generic``: The polymorphic expression to be instantiated + - ``arguments``: A JSON object in which keys are the names of type parameters and values are :ref:`JSON Cryptol types `. + +``integer modulo`` + The expression is an integer with a modulus (the Cryptol ``Z`` type). Further fields are: + + - ``integer``: A JSON number, representing the integer + - ``modulus``: A JSON number, representing the modulus + +.. _cryptol-json-type: + +JSON Cryptol Types +------------------ + +JSON representations of types are type schemas. A type schema has +three fields: + +``forall`` + + Contains an array of objects. Each object has two fields: ``name`` + is the name of a type variable, and ``kind`` is its kind. There + are four kind formers: the string ``Type`` represents ordinary + datatypes, the string ``Num`` is the kind of numbers, and + ``Prop`` is the kind of propositions. Arrow kinds are represented + by objects in which the field ``kind`` is the string ``arrow``, + and the fields ``from`` and ``to`` are the kinds on the left and + right side of the arrow, respectively. + +``propositions`` + A JSON array of the constraints in the type. + +``type`` + The type in which the variables from ``forall`` are in scope and + the constraints in ``propositions`` are in effect. + +Concrete Types +~~~~~~~~~~~~~~ + +Types are represented as JSON objects. The ``type`` field contains one of the following tags (represented as JSON strings): + +``variable`` + The type is a type variable. The remaining fields are ``name``, + which contains the variable's name, and ``kind``, which contains + its kind (represented as in the ``forall`` section). When providing + types to Cryptol, the ``kind`` field should be elided, and type synonyms + may be provided with arguments through an optional ``arguments`` field. + +``record`` + The type is a record type. The remaining field is ``fields``, + which contains a JSON object whose keys are the names of fields and + whose values are the fields' types. + +``number`` + The type is a number. The field ``value`` contains the number + itself. + +``inf`` + The type is the infinite number. There are no further fields. + +``Bit`` + The type is the bit type. There are no further fields. + +``Integer`` + The type is the integer type. There are no further fields. + +``Rational`` + The type is the rational number type. There are no further fields. + +``Z`` + The type is integers modulo another value. The field ``modulus`` + contains the modulus, which is a type. + +``bitvector`` + The type is a bitvector. The field ``width`` contains the number + of bits, which is a type. + +``sequence`` + The type is a sequence. The field ``length`` contains the length + of the sequence (a type), and the field ``contents`` contains the + type of entries in the sequence. + +``function`` + The type is a function type. The fields ``domain`` and ``range`` + contain the domain and range types. + +``unit`` + The type is the unit type. There are no further fields. + +``tuple`` + The type is a tuple. The field ``contents`` is a JSON array + containing the types of the projections from the tuple. + +One of ``+``, ``-``, ``*``, ``/``, ``%``, ``^^``, ``width``, ``min``, ``max``, ``/^``, ``%^``, ``lengthFromThenTo`` + The type is an application of the indicated type function. The + arguments are contained in the ``arguments`` field, as a JSON + array. + +Propositions +~~~~~~~~~~~~ + +Propositions/constraints have the key ``prop``, mapped to one of the +following tags: + +``==`` + Equality. The equated terms are in the ``left`` and ``right`` + fields. + +``!=`` + Inequality. The disequated terms are in the ``left`` and + ``right`` fields. + +``>=`` + Greater than. The greater type is in the ``greater`` field and the + lesser type is in the ``lesser`` field. + +``fin`` + Finitude. The finite type is in the ``subject`` field. + +``has`` + The selector is in the ``selector`` field, the type that has this + selector is in the ``type`` field, and the type expected for the + projection is in the ``is`` field. + +``Arith``, ``Cmp``, ``SignedCmp``, ``Zero``, ``Logic`` + The type that has these operations defined is in the ``subject`` + field. + +``Literal`` + The size is in the ``size`` field, and the type is in the + ``subject`` field. + +``True`` + There are no further fields. + +``And`` + The conjuncts are in the ``left`` and ``right`` fields.