how to write to a file, from coq - 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.

Related

Ease life in dependently typed programming using `Function` and `Program` in Coq

I am trying to implement a dependently typed evaluator of STLC in Coq using Program Fixpoint. Since the language does not have fixed point operator, I think the evaluator should terminate, though the termination condition is not structural.
During my development, I find one source of headache is I simply can't keep track of too many variables at the same time, and pattern matching is too nested.
If it were about a mere Fixpoint, I can just implement the body using tactics, but when using Program Fixpoint or Function, I just cannot. Is there any trick to build body using tactics in this case?
I am stuck at very end: https://gist.github.com/HuStmpHrrr/0d92e646916ae9ec7ced3ff21724ba2d
When using Program, you can simply leave underscores for parts of your term you want to fill in using proof mode. Any underscores that can be inferred will automatically be filled in and the remaining will produce obligations. For example, you can write all of run in proof mode by writing Program Fixpoint run ... {measure ...} := _. The measure will show up as an argument to run in the context.

How to do higher-order term rewriting in Coq?

This question is based on my question https://cs.stackexchange.com/questions/96533/how-to-transform-lambda-function-to-multi-argument-lambda-function-and-how-to-re There are two functions and two terms in that question:
Functions:
is: (e->t)->(e->t)
IS: e->(e->t)->t
Terms:
(is(boss))(John): t
IS(John, boss): t
My question is this: how to rewrite terms involving is with terms that have only IS? Does Coq (or third party tools) has such rewriting facilities? Does Coq have facilities to check the equality of rewrittern terms?
Maybe such rewriting can be done outside the Coq world, maybe there are other purely lambda calculus tools with syntactic manipulation only?
There is no tool that performs the kind of textual transformation of Coq code you are describing directly. Without knowing much about GrammaticalFramework, I imagine that your best bet would be to write a Sed script that looks for occurrences of is applied to arguments and replaced those occurrences by equivalent expressions with IS.
The second “IS“ form can be more easily converted to is-boss predicate, that is why I am striving to arrive at it.
I think that if you used a Sed script you could just as easily go to the IS_BOSS form directly, without using IS.

Refine and # (at) symbol in Coq 8.5pl1

In the previous version of Coq using symbol # in refine command allows me to create a prove step-by-step. (Each argument was a separate goal.)
I want to avoid implicit arguments like "?Goal0 ?Goal1". And now I can not.
What should I do obtain such possibility?
(It is very uncomfortable to me especially when I trying to deal with recursive functions.)
You are looking for simple refine.
In 8.5, goals that can be solved by unification (what you call implicit) are put in the "shelve" a hidden area, as they are considered trivial. You can unshelve them with Unshelve but it is a bit incovenient as it is a vernacular command.
simple refine behaves as 8.4 and doesn't put anything in the shelve.

How to read a simple expression in Fortran?

In the Fortran program, is it possible to read an expression including the variables ?
For example, the input file is (if necessary, we can change the input form of the expression,e.g.,the binary form),
2(a-4b)
It should be noted that the input expression has a very simple form and it only contains integer or fraction or some variables,like the following in the list,
{0,232,-2/5a,3a-b,b/9}
Here 2a means 2*a
The Fortran program is
Program test
implicit none
real(kind=8)::a,b,exp
a=10.
b=3.
! open file and read the input expression
! that is, exp=2*(a-4*b)
write(*,*) exp ! we can get exp=-4.0
end program
For the complicated expressions, it is obviously not a good idea for Fortran. I just want to know, in this simple input expression case, is it possible to find a better way ?
A site with tests of three expression evaluators in Fortran is http://www.angelfire.com/ab5/extensao/report.htm . Only the link to the "Brazilian" one works.
There is also a Sourceforge project fparser http://fparser.sourceforge.net/ .
Fortran cannot do this unless you write code that can parse the arbitrary expression, substitute and solve, which is a bit of work (see the comment below for details). Fortran is compiled and there is no way to load source on the fly, compile and run it, which is essentially what you are asking. You might look into a language such as Lisp where doing this is should be somewhat trivial. Likewise any scripted language will have facilities to evaluate code, which can do what you are asking.

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.