How can I find what file a value is defined in? - coq

When I work on a Coq proof I often want to find which file a definition comes from.
E.g. I had a goal which contains list_norepet (map fst (PTree.elements ta)), and I wanted to find the file that defined list_norepet. Doing Print list_norepet. shows lots of helpful information, but not the file name. Is there any way to get Coq to print that?

You can use Locate to get the full module name. Usually this is enough to find the file, but then you can use Locate File to try to find it:
Locate eq_rect.
(* Constant Coq.Init.Logic.eq_rect *)
Locate File "Init/Logic.v".
(* /Users/tchajed/code/sw/coq-master/theories/Init/Logic.v *)
I say "try to find it" because you do need to know the remappings (with -R) to be able to translate module paths to file paths - for example, Coq's standard library is in theories but is mapped to Coq.

Related

How can I register a collections directory without clobbering the Racket's default(s)?

So, basically, I'm trying to register a local collections collects/ directory containing modules that I don't want to be global (e.g. not in /usr/share/racket/collects) for the interpeter to use.
However, in doing this, I think I've ran into a problem where I can set the path using something like: scheme_set_collects_path(scheme_make_path("/hardcoded/absolute/path/to/collects")); but then it appears to clobber the system-wide racket collections (error is encountered where it tries to interpret the module header #lang racket - I think).
I can't give an exact rundown since I've since scrapped major contextual elements of this, so all I can really ask is: is it even possible to embed Racket such that you can interpret local source files?
I've looked at the header files and seen scheme_init_collection_paths_post, which allows you to specify pre and post paths for collections. However, I don't know if these paths completely clobber any defaults, or how do pass an empty path to pre.
Currently my idea is to register a local collections path in the same folder as my executable and dynamically require functions from local collections. So I must register a path without messing up the defaults (since doing short-form #lang racket requires "racket" collection).
I'm trying to achieve this:
((dynamic-require ''mypackage/mymodule 'myfunction))
where 'myfunction is provided by 'mymodule.
I've not quite wrapped my head around how you actually use the scheme_* functions, so I've done this:
Scheme_Object* a[2];
a[0] = scheme_intern_symbol("mypackage/mymodule");
a[1] = scheme_intern_symbol("myfunction");
Scheme_Object* func = scheme_dynamic_require(2, a);
scheme_apply(func, 0, NULL);
I don't believe that's actually how you'd go about dynamically requiring functions properly but I vaguely copied it from the dynamic require Racket REPL example.
The error I'm getting now is:
standard-module-name-resolver: collection not found
for module path: mypackage/mymodule
collection: "mypackage"
in collection directories:
context...:
show-collection-err
standard-module-name-resolver
SIGSEGV MAPERR si_code 1 fault on addr 0xd8
[1] 7102 abort (core dumped) ./main
Ignore the fact I've called the collection "mypackage". This error is the most recent error. Previously, it could resolve the local collections (I've basically been doing raco pkg install --link mypackage and then copying that into a local collects/ folder - since I know installing packages compiles them) but it wouldn't be able to load any "racket" modules (such as the one required by #lang racket).
So, how do you register an extra - local - collections directory for Racket's interpreter?

how to write to a file, from coq

This is probably a strange question to ask since Coq is supposed to be a purely functional language, but Extraction exists and it clearly has side effects, so I'd assume there's probably a more basic command to just output a string or some constant to a file, something like this:
Extraction "file.txt" "hello"%string.
Is this possible? Would it require writing a custom extractor (I don't even know if that's possible) ?
The practical reason for this question is related to the motivation for the extraction mechanism that is already present in Coq, but let's say I want to output C code or something else that's not currently supported. I could still write a function in Coq extract : Expr -> string for a custom syntax that I formalize in an inductive type Expr. How can I get this string out to a file?
You can use Redirect with Eval to get close:
Require Import String.
Open Scope string_scope.
Redirect "file.txt" Eval compute in "hello".
(* file.txt.out now contains:
= "hello"
: string
*)
Alternately, write your function extract in Coq, then use the extraction mechanism to extract extract e for some e of interest, and finally write an OCaml program that imports this (string) constant and prints it. The reason to go this route is that building up strings in Coq is so slow that you might not be able to run Eval compute extract e but you might be able to run it in OCaml. You can also then (in an unverified manner) replace Coq strings with OCaml native strings so this process is actually efficient; this is easy to do by importing ExtrOcamlString in Coq before extraction.

What does the Coq command Require Import Ltac do?

When I am looking at the QuickChick project, I encountered the sentence Require Import Ltac. I don't know what this does and where the Ltac module is. I found a file plugins/ltac/Ltac.v, but this couldn't be the one since this file is empty (by the way, what is the purpose of including an empty file anyway?). I tried Locate Ltac. but I got Error: Syntax error: [constr:global] expected after 'Ltac' (in [locatable])., which is more confusing.
What does the Ltac module do, where is the Ltac.v file, and why doesn't the Loacte command work in this case? Thanks!
Require Import Ltac. is indeed Coq.ltac.Ltac, the empty file you found! I am not sure why there's an empty file there, but it was introduced when Ltac was moved to a plugin. Perhaps it serves as a placeholder for if some of the Ltac implementation were moved into Coq rather than being an OCaml plugin. In any case I think there's little reason for QuickChick to import it, unless they're anticipating some change to Coq I don't know about.
Because of a conflict with the vernacular command Locate Ltac (which is giving you a syntax error), you need to instead use Locate Module explicitly. The same goes for Print Module.
Locate Module Ltac reports Module Coq.ltac.Ltac, which tells you that you're indeed looking at theories/ltac/Ltac.v, and Print Module Ltac shows an empty module. However, that second bit is misleading, since what look like empty modules can still have notations (that's not the case here, but just FYI).

How to find the source file for an identifier in Coq

I was staring at an error for quite a while before I realized that there's both Rsqrt and sqrt defined somewhere in Coq:
Unable to unify "0 < Rsqrt ?M2352 \/ 0 = Rsqrt ?M2352" with "0 < sqrt r12 \/ 0 = sqrt r12".
Rather than asking specifically how to find where these two specific items (Rsqrt and sqrt) are defined, is there a general way to find the .v file from the standard library in which a name such as sqrt is defined?
BTW, I know Check and Print. But I need to find the relevant source files or documentation to see what lemmas are available about the object.
I am not aware of such a feature, but I think you could use Locate (or sometimes SearchAbout sqrt) to get the full path of the term, and then easily guess the corresponding .v file.
Using Emacs + ProofGeneral, after following the instructions in the Installation notes:
Install coqtags in a standard place or add /coq to your PATH.
NB: You may need to change the path to perl at the top of the file.
Generate a TAGS file for the library by running
coqtags find . -name \*.v -print
in the root directory of the library, $COQTOP/theories.
you can use M-. for going to the .v file where the identifier under the cursor was defined and you can use M-* to go back.
The emacs addon company-coq has this very convenient feature.
https://github.com/cpitclaudel/company-coq

Locating definition of a tactic in Coq proofs

In studying Coq proofs of other authors, I often encounter a tactic, lets say "inv eq Heq" or "intro_b". I want to understand such tactics.
How can I find if it is a Coq tactic or a Tactic Notation defined somewhere in my current project?
Second, is there a way to find its definition?
I used SearchAbout, Search, Locate and Print but could not find answers to the above questions.
You should be able to use
Print Ltac <tacticname>.
to print the code of a user-defined tactic (according to the documentation).
To find where it is defined... I guess you're going to need grep unfortunately, Locate does not work for tactics names it seems.
As mentioned before, Print Ltac ... prints the code of a user-defined tactic.
To locate a user-defined tactic (i.e. to know where its defined), use Locate Ltac .... It gives you the fully qualified name. Then use Locate Library to find the corresponding file.