Skip to content

Commit

Permalink
Indexing and search for LambdaPi (Deducteam#982)
Browse files Browse the repository at this point in the history
This PR adds:
- indexing and search commands in the CLI, as queries and provided by a webserver
- a query language to compose basic queries
- new options --no-sr-check, --rules, --port
- the record type Eval.config is extended with a new field allowing to specify which dtree to use for each symbol

The last two modifications allow to later implement something similar to [dkmeta](https://github.com/Deducteam/dkmeta) in lambdapi.

TODO:
- [x] CI fails with ocaml < 4.14.1
- [x] update CHANGES.md
- [x] update AUTHORS.md
- [x] @fblanqui  test locate/search/search-query commands (VSCode is broken again for me)
- [x] update emacs and vscode
- [x] doc --no-sr-check
- [ ] @fblanqui reorganize options.rst with a section for each command ?
- [x] @sacerdot add examples in doc/query_language.rst
- [x] update comments of modified functions
- [x] make sanity_check
- [x] update BNF
- [x] update doc of command line arguments
- [x] update doc of lambdapi queries
- [x] add comment in doc that results of search may be not well typed
- [x] fix hard-coded string "LPSearch" in parse_term_string in lambdapi.ml: I have nothing better to put there, it stays like that
- [x] fix hard-coded Indexing.dbpath and meta-rules file
- [x] add option --meta-rules for reading file LPSearch.lp
- [x] rename command resolve by locate
- [x] put keywords in alphabetic order
- [x] replace assert false by fatal whenever possible
- [x] in lambdapi.ml, locate_cmd should take an identifier and not a string
- [x] replace LPSearch.dk by LPSearch.lp
- [x] when reading LPSearch.lp, fail if an identifier is not qualified
- [x] allow the search query in the middle of a proof, using the variables of the proof context; turn metavariables into patterns?
- [x]  be sure to quote all dedukti/lambdapi snippets that are pasted to the web page
- [x] add cli/query command for the query language
- [x] remove "search" in favour of the query language? (maybe yes) same for locate? (probably not)
- [x] allow to start the webserver on a different port
- [x] rename the webserver to websearch
- [x] branch the query language doc to the main doc
- [x] test indexing and search on lambdapi files (in particular test path restriction of the query language)

index file:
- define a default place for the index files, for instance ~/.lambdapi/LPSearch.db and ~/.lambdapi/LPSearch.lp
- better: use dune site for use opam compatible places

future work (not for this PR):
- [ ] remove fix in pos.ml for handling Deducteam#1001
- [ ] add rules to undo HOL encoding
- [x] add a query language
- [ ] more WHEN/HOW specifications? (e.g. in conclusion or spine)
- [x] more precise matching information (in particular: in conclusion/hypothesis)
- [ ] even more precise information: in conclusion of hypothesis
- [x] for more precise results, allow a variant of index-matching where an IHOLE can be matched only by an IVar; this "undoes" the "modus ponens" step used during indexing
- [ ] order search results according to precision of the matching (larger patterns) or other criteria

-----------------------------------------
commits:

* Scoping functions generalized over symbol identifiers

* First index/search cli-commands implemented

- lambdapi index FILES
  creates a LPSearch.db index file in current
  dir
- lambdapi resolve name
  resolves the name using the index
- lambdapi search
  interactively asks for queries resolved
  using the index; the query are term patterns
  (use _ for holes)

* New Dedukti queries search and resolve

To be fixed:
- where/when to write LPSearch.db

* DB made lazy and loaded from disk when needed

The path "LPSearch.db" in the current dir is
still hard-coded

* Debugging code removed

* TO BE REVISED: added parser for terms

- used for "lambdapi search term"
- a functor would be more appropriate to
  allow to parse more grammar entries
  uniformly

* Interface minimized

* Preliminary work to index more

E.g. to index the body, to index subterms,
etc.

* added is_vari

* Indexing subterms of types of symbols

* Bug fixed: "lambdapi index" was adding to the index

The fix empties the index just before.

* fix line lengths for make sanity_check to pass

* add option --no-sr-check

* Ready to index rules as well

* Implemented meta-rewriting rules

- for now used only on the type of
  constants to be indexed
- the rules can be untyped and be defined
  on symbols not declare yet

* Lhs/Rhs of rules indexed

* Code refactoring

- Pure has no references
- DB manages the global reference
- Code outside is high level code to
  index signatures and process queries

* Disable warnings during compilation

* Semantics of matching during indexing changed

- patterns $_.[...] are now the placeholders
  both in queries and in types/rules
- when going under a pi, variables are
  turned into placeholders

KNOWN BUG: variables should be turned into
placeholders only in spines

* BUG FIXED: replace vars with placeholders in spine only

* dbpath/rwpath hardcoded

* lambdapi index --add

adds the file to the index

WARNING: it does not check if the files was already added

* resolve renamed to locate everywhere

* warning improved

* sanity

* update doc and bnf

* Do not expand during normalization + better flexibility check

* LPSearch.dk to undo the encoding

* Line commented out by mistake

* Duplicated code generalized

* Code simplified and ready to be generalized

- subterms_to_index now returns pairs where/what
- used also to index the whole term

* item type refined to have more informations on the location

* Locate/search queries now return a set in place of a list

This is a preliminary step to define a query language based on operations
on sets.

* BUG fixed

- introduced by last commit
- find_sym must be the identity of qualified names, as it used to be

* New option holes_in_index to search (defaults to false)

- when holes_in_index is false, search behaves as if the indexed symbols
  where applied to prove the current goal, i.e. pi-quantifications in the
  spine are eliminated using holes (~metavariables)

* For lhs/rhs it makes no sense to handle them as spines

- pi-generalization is disabled for lhs/rhs
- the position information degenerates to Exact/Inside

* Doc fixed

* Added V# as a pattern that matches only variables.

* Check that all assert false must remain so + code cleanup

* Restore alphabetic order

* lamdapi cli: accept only non-qualified identifiers for locate

* LPSearch.dk => LPSearch.lp (with change of syntax)

* Allow to use variables in scope in searches during a proof.

* Allow meta-variables in search patterns

WARNING: meta-variables seem to be bugged/unusable in lambdapi at the moment.
This feature cannot be tested

* Removed unused field problem

This is some hand-made cherry-picking from the main branch, to avoid
several conflicts

* lambdapi webserver implemented

* Nice printing of snippets in HTML

* Localize symbol declarations

In case of definitions, only the declaration part is localized

* webserver documented

* added dream and lwt_ppx dependencies

* Fix --help for locate/search

I finally understood how to have mandatory positional arguments with cmdliner

* deref generalized to accomodate plain text

* Enable code snippets for textual search as well

* Painful code factorization between txt/html queries

* hide function

* code cleanup

* - rename holes_in_index in generalize
- update BNF script and grammar
- add temporary fix for Deducteam#1001
- fix code in tests/kernel.ml and tests/rewriting.ml

* details

* remove version constraints on lwt_ppx and dream

* Definition of a basic query language

* Add path to Xhs

* reformatted

* syntax fixed

* Code refactoring (big change)

An ItemSet is now a map from items (i.e. sym_name * pos) to a
list of positions. The base query involved in the position is not
recorded yet. The next step is to add it in order to understand the
output in case of union/intersection.

* fix doc

* WiP: syntax for the query language

- name: id
- XXX: pattern
- "," and ";" for conjunction/disjunction
- "|" for filtering

* Bug fixed: arguments swapped in search query filtering

* Bug fix + WiP in query language

- WiP: more syntax allowed
- Bug fix: filtering was expecting only one position after base query,
  which is not an invariant

* New syntax for search queries:  :/=/~ for the position to match

* Only "type:" makes sense: "type=" and "type~" don't

* Record the base queries in the answer

* Pretty-print terms without fully qualifying identifiers on demand.

Used to print query patterns

* allow to specify generalization in base queries

* print when the term occurs potentially generalized

* Link to the github

* document the query language

* more checks that make sense

* doc improved

* form improved

* search-query cli command added and documented

* search-query query added

Untested: VSCode is broken again for me

Warning: I am following a non-orthodox approach to avoid polluting
LambdaPI code everywhere. The query takes a string that is later
scoped/handled by my code

* query commands documented

* BNF updated automatically

* add doc for --no-sr-check

* update editors

* SEARCHQUERY -> SEARCH_QUERY

* update auhors

* HTML-escaping of snippets

* Complete and improved html escaping

It uses an higher-order polymorphic pretty-printer

* More readable output

* webserver renamed to websearch

* Add --port option to websearch

* deref -> print_file_contents

* detail

* fix index_cmd

* --rules option to index

* Escaping fixed

* Keep the query to remember what you asked for

* Allow qualified path prefixes

* document --rules

* Show something when the list of results is empty

* Use =, >, >= and its unicode variant for positions

* Improved error reporting in websearch

* a few examples added to the doc

* fix LPSearch db file name

* update doc queries

* Bug fixed: locs are in UTF8 codepoint, not chars!

* Improved error message

* Queries with overloaded symbols now fail

The previous behavior was to pick a random interpretation and
issue a warning about that. Now we fail and tell the user to use
locate to disambiguate the symbol by hand

* Red color for all query types

* handle_exception used for all commands

* Avoid blank lines in HTML output

* one end-line was missing

* only search_query left, renamed to search

* One reference to locate was still there

* match ==> anywhere

* update editors and doc

* update bnf

* update CHANGES.md

* pos.ml: comment assert String.is_valid_utf_8

* update emacs files

* update vscode syntax file

* add utf8 string functions missing in ocaml < 4.14.1

* update doc for --rules

* indentation

* add missing functions in Bytes for OCaml < 4.14.1

* Check for no implicit terms in rhs of indexing rules

* detail

* doc: add example of index rules

* fix doc

* add uchar.ml

* update vscode json files

---------

Co-authored-by: Frédéric Blanqui <frederic.blanqui@inria.fr>
  • Loading branch information
sacerdot and fblanqui authored Jul 28, 2023
1 parent a675a29 commit e168936
Show file tree
Hide file tree
Showing 54 changed files with 3,885 additions and 249 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Active Contributors (in alphabetical order)

- Frédéric Blanqui (coordinator)
- Alessio Coltellacci (2023-)
- Claudio Sacerdoti Coen (2023-)

Past Contributors (in alphabetical order)
=========================================
Expand Down
13 changes: 12 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
### Added

- Several options for export -o stt_coq.
- Tactic remove
- Tactic remove.
- Option --no-sr-check to disable subject reduction checking.
- CLI command index to build ~/.LPSearch.db.
- Indexed terms can be normalized wrt rules with the option --rules
(note that this new option --rules could be used to implement the equivalent
of dkmeta later).
- CLI command search and LP command search to send queries to the index.
- Query language.
- CLI command websearch to run a webserver that can answer search queries.
- Option --port to specify the port to use.

### Improved

Expand All @@ -17,6 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
### Changed

- Private definitions are not kept in memory and in lpo files anymore.
- The record type Eval.config is extended with a new field allowing to specify
which dtree to use for each symbol.

## 2.3.1 (2023-03-13)

Expand Down
16 changes: 9 additions & 7 deletions doc/Makefile.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,42 @@ lambdapi.bnf: Makefile.bnf ../src/parsing/lpParser.mly
-e 's/INDUCTIVE/"inductive"/g' \
-e 's/INFIX/"infix"/g' \
-e 's/INJECTIVE/"injective"/g' \
-e 's/PRINT/"print"/g' \
-e 's/REFINE/"refine"/g' \
-e 's/REMOVE/"remove"/g' \
-e 's/STRINGLIT/<stringlit>/g' \
-e 's/NAT/<nat>/g' \
-e 's/IN/"in"/g' \
-e 's/LET/"let"/g' \
-e 's/NAT/<nat>/g' \
-e 's/NOTATION/"notation"/g' \
-e 's/OPEN/"open"/g' \
-e 's/OPAQUE/"opaque"/g' \
-e 's/POSTFIX/"postfix"/g' \
-e 's/PREFIX/"prefix"/g' \
-e 's/PRINT/"print"/g' \
-e 's/PRIVATE/"private"/g' \
-e 's/PROOFTERM/"proofterm"/g' \
-e 's/PROTECTED/"protected"/g' \
-e 's/PROVER_TIMEOUT/"prover_timeout"/g' \
-e 's/PROVER/"prover"/g' \
-e 's/QUANTIFIER/"quantifier"/g' \
-e 's/REFLEXIVITY/"reflexivity"/g' \
-e 's/REFINE/"refine"/g' \
-e 's/REMOVE/"remove"/g' \
-e 's/REQUIRE/"require"/g' \
-e 's/RESOLVE/"resolve"/g' \
-e 's/REWRITE/"rewrite"/g' \
-e 's/SIDE/<side>/g' \
-e 's/UNIF_RULE/"unif_rule"/g' \
-e 's/RULE/"rule"/g' \
-e 's/SIDE/<side>/g' \
-e 's/SEARCH/"search"/g' \
-e 's/SEQUENTIAL/"sequential"/g' \
-e 's/SIMPLIFY/"simplify"/g' \
-e 's/SOLVE/"solve"/g' \
-e 's/STRINGLIT/<stringlit>/g' \
-e 's/SYMBOL/"symbol"/g' \
-e 's/SYMMETRY/"symmetry"/g' \
-e 's/TYPE_QUERY/"type"/g' \
-e 's/TYPE_TERM/"TYPE"/g' \
-e 's/VERBOSE/"verbose"/g' \
-e 's/WHY3/"why3"/g' \
-e 's/WITH/"with"/g' \
-e 's/IN/"in"/g' \
-e 's/FLOAT/<float>/g' \
-e 's/HOOK_ARROW/"↪"/g' \
-e 's/ARROW/"→"/g' \
Expand Down
1 change: 1 addition & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Examples of developments made with Lambdapi:
commands.rst
tactics.rst
queries.rst
query_language.rst
dedukti.rst
latex.rst

Expand Down
23 changes: 23 additions & 0 deletions doc/lambdapi.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ QID ::= [UID "."]+ UID
<assert> ::= "assert"
| "assertnot"

<term_alone> ::= <term> EOF

<qid_alone> ::= <qid> EOF

<search_query_alone> ::= <search_query> EOF

<command> ::= "require" "open" <path>* ";"
| "require" <path>* ";"
| "require" <path> "as" <uid> ";"
Expand Down Expand Up @@ -41,6 +47,7 @@ QID ::= [UID "."]+ UID
| "prover_timeout" <nat>
| "verbose" <nat>
| "type" <term>
| "search" <stringlit>

<path> ::= UID
| QID
Expand Down Expand Up @@ -169,4 +176,20 @@ QID ::= [UID "."]+ UID
<float_or_nat> ::= <float>
| <nat>

<maybe_generalize> ::= ["generalize"]

<where> ::= UID <maybe_generalize>

<asearch_query> ::= "type" <where> <aterm>
| "rule" <where> <aterm>
| UID <where> <aterm>
| "(" <search_query> ")"

<csearch_query> ::= <asearch_query> ("," <asearch_query>)*

<ssearch_query> ::= <csearch_query> (";" <csearch_query>)*

<search_query> ::= <ssearch_query>
| <search_query> "|" <qid>


46 changes: 38 additions & 8 deletions doc/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,57 @@ The available commands are:
* ``decision-tree``: output the decision tree of a symbol as a Dot graph (see :doc:`dtrees`)
* ``export``: translate the input file to other formats.
* ``help``: display the main help message.
* ``index``: create an index of symbols and rules of input files.
* ``init``: create a new Lambdapi package (see :doc:`getting_started`).
* ``install``: install the specified files according to package configuration.
* ``lsp``: run the Lambdapi LSP server.
* ``parse``: parse the input files.
* ``search``: runs a search query against the index.
* ``uninstall``: uninstalls the specified package.
* ``version``: give the current version of Lambdapi.
* ``websearch``: starts a webserver to search the library.

The ``parse`` and ``export`` commands can trigger the
The commands ``parse``, ``export`` and ``index`` can trigger the
compilation of dependencies if the required object files (``.lpo``
extension) are not present.

**Input files:**

The commands ``check``, ``parse`` and ``export`` expect input files
The commands ``check``, ``parse``, ``export`` and ``index`` expect input files
with either the ``.lp`` extension or the ``.dk`` extension.
The appropriate parser is selected automatically.

The ``export`` command outputs the translation of all the input files
on the standard output.
The appropriate parser is selected automatically. The ``export`` command accept only one file as argument.

If a command takes several files as argument, the files are
handled independently in the order they are given. The program
immediately stops on the first failure, without going to the next file
(if any).

**index:**

The ``index`` command generates the file ``~/.LPSearch.db``. This file contains an indexation of all the symbols and rules occurring in the dk/lp files given in argument. By default, the file ``~/.LPSearch.db`` is erased first. To append new symbols and rules, use the option ``--add``. It is also possile to normalize terms wrt some rules before indexation by using ``--rules`` options.

**search:**

The command ``search`` takes as argument a query and runs it against the index file ``~/.LPSearch.db``. See :doc:`query_language` for the specification of the query language.

**Common flags:**

The commands ``check``, ``decision-tree``, ``export``, ``parse``,
``lsp`` all support the following command line arguments and flags.

* ``-v <NUM>``, ``--verbose=<NUM>`` sets the verbosity level to the given natural number (the default value is 1). A value of 0 should not print anything, and the higher values print more and more information.
* ``--debug=<FLAGS>`` enables the debugging modes specified by every character of ``FLAGS``. Details on available character flags are obtained using ``--help``.

* ``--lib-root=<DIR>`` sets the library root, that is, the folder corresponding to the entry point of the Lambdapi package system. This is the folder under which every package is installed, and a default value is only known if the program has been installed. In development mode, ``--lib-root lib`` must be given (assuming Lambdapi is run at the root of the repository).

* ``--map-dir=<MOD>:<DIR>`` maps an arbitrary directory ``DIR`` under a module path ``MOD`` (relative to the root directory). This option is mainly useful during the development of a package (before it has been installed). However it can also be accessed using a package configuration file (``lambdapi.pkg``) at the root of the library’s source tree. More information on that is given in the section about the module system.

* ``--debug=<FLAGS>`` enables the debugging modes specified by every character of ``FLAGS``. Details on available character flags are obtained using ``--help``.
* ``--no-sr-check`` disables subject reduction checking.

* ``--timeout=<NUM>`` gives up type-checking after the given number of seconds. Note that the timeout is reset between each file, and that the parameter of the command is expected to be a natural number.

* ``-v <NUM>``, ``--verbose=<NUM>`` sets the verbosity level to the given natural number (the default value is 1). A value of 0 should not print anything, and the higher values print more and more information.


check
-----

Expand Down Expand Up @@ -126,6 +137,25 @@ Examples of libraries exported to Coq:
- In the Lambdapi sources, see how to export the Holide Dedukti library obtained from OpenTheory in `README.md <https://github.com/Deducteam/lambdapi/blob/master/libraries/README.md>`__.
- See in `hol2dk <https://github.com/Deducteam/hol2dk>`__ how to export the Lambdapi library obtained from HOL-Light.

index
-----

* ``--add`` tells lambdapi to not erase ``~/.LPSearch.db`` before adding new symbols and rules.

* ``--rules <LPSearch.lp>`` tells lambdapi to normalize terms using the rules given in the file ``<LPSearch.lp>`` before indexing. Several files can be specified by using several ``--rules`` options. In these files, symbols must be fully qualified but no ``require`` command is needed. Moreover, the rules do not need to preserve typing. On the other hand, right hand-side of rules must contain implicit arguments.

For instance, to index the Matita library, you can use the following rules:

::

rule cic.Term _ $x ↪ $x;
rule cic.lift _ _ $x ↪ $x;

websearch
---------

* ``--port=<N>`` specifies the port number to use (default is 8080).

lsp
-------

Expand Down
93 changes: 53 additions & 40 deletions doc/queries.rst
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
Queries
=======

.. _print:

``print``
---------

When called with a symbol identifier as argument, displays information
(type, notation, rules, etc.) about that symbol. Without argument,
displays the list of current goals (in proof mode only).

.. _proofterm:

``proofterm``
-------------

Outputs the current proof term (in proof mode only).

.. _type:

``type``
--------

Returns the type of a term.

.. _compute:

``compute``
-----------

Computes the normal form of a term.

.. _assert:
.. _assertnot:

Expand All @@ -49,18 +19,12 @@ negative tests.
assertnot ⊢ zero ≡ succ zero;
assertnot ⊢ succ : Nat;

.. _verbose:

``verbose``
-----------

Takes as argument a non-negative integer. Higher is the verbose
level, more details are printed. At the beginning, the verbose is set
to 1.
.. _compute:

::
``compute``
-----------

verbose 3;
Computes the normal form of a term.

.. _debug:

Expand Down Expand Up @@ -96,6 +60,22 @@ rewrite engine by reducing η-redexes.
flag "print_domains" on; // default is off
flag "print_meta_types" on; // default is off

.. _print:

``print``
---------

When called with a symbol identifier as argument, displays information
(type, notation, rules, etc.) about that symbol. Without argument,
displays the list of current goals (in proof mode only).

.. _proofterm:

``proofterm``
-------------

Outputs the current proof term (in proof mode only).

.. _prover:

``prover``
Expand All @@ -119,3 +99,36 @@ beginning, the timeout is set to 2s.
::

prover_timeout 60;

.. _search_cmd:

``search``
------------------

Runs a query between double quotes against the index file
``~/.LPSearch.db``. See :doc:`query_language` for the query language
specification.

::

search "spine: (nat → nat) , hyp: bool";

.. _type:

``type``
--------

Returns the type of a term.

.. _verbose:

``verbose``
-----------

Takes as argument a non-negative integer. Higher is the verbose
level, more details are printed. At the beginning, the verbose is set
to 1.

::

verbose 3;
Loading

0 comments on commit e168936

Please sign in to comment.