Coq program wf
->>>> Click Here to Download <<<<<<<-
Due to differences between Coq and ML type systems, some extracted programs are not directly typable in ML. We now solve this problem at least in OCaml by adding when needed some unsafe casting Obj. First, if some part of the program is very polymorphic, there may be no ML type for it.
In that case the extraction to ML works alright but the generated code may be refused by the ML type checker. A very well known example is the distr-pair function:. Secondly, some Coq definitions may have no counterpart in ML. This happens when there is a quantification over types inside the type of a constructor; for example:.
Even with those unsafe castings, you should never get error like segmentation fault. In fact even if your program may seem ill-typed to the OCaml type checker, it can't go wrong : it comes from a Coq well-typed terms, so for example inductive types will always have the correct number of arguments, etc. Of course, when launching manually some extracted function, you should apply it to arguments of the right shape from the Coq point-of-view. More details about the correctness of the extracted programs can be found in [Let02].
We have to say, though, that in most "realistic" programs, these problems do not occur. For example all the programs of Coq library are accepted by the OCaml type checker without any Obj. We present here two examples of extraction, taken from the Coq Standard Library. We choose OCaml as the target language, but everything, with slight modifications, can also be done in the other languages supported by extraction.
We then indicate where to find other examples and tests of extraction. The file Euclid contains the proof of Euclidean division. The natural numbers used here are unary, represented by the type nat , which is defined by two constructors O and S. We can now extract this program to OCaml:. It only enhances readability of extracted code.
You can then copy-paste the output to a file euclid. This file ExtrOcamlIntConv. Several of the Coq Users' Contributions use extraction to produce certified programs. In particular the following ones have an automatic extraction test:. Note that continuations and multiplier are a bit particular.
They are examples of developments where Obj. This is probably due to a heavy use of impredicativity. After compilation, those two examples run nonetheless, thanks to the correction of the extraction [Let02]. Coq 8. The variables map Is it automatic? Concrete usage in Coq Adding a ring structure How does it work? Sections Lists. Require Extraction. Inlining and printing of a constant declaration: The user can explicitly ask for a constant to be extracted by two means: by mentioning it on the extraction command line by extracting the whole Coq module of this constant.
The number of type variables is checked by the system. Caution It is the responsibility of the user to ensure that the ML terms given to realize the axioms do have the expected types. Axiom X : Set. If I use this proof, the resulting function behaves normally. Eval compute in log24 But if I want to do the proof myself, I do not always get this behaviour. First, if I end the proof with Qed instead of Defined , the result of the computation even on small numbers is a complex expression and not a single number.
So I use Defined and try to use only transparent lemmas. Here, lemma1 is a proof of the well-founded induction on the natural numbers. The first one does not work, it seems this is because it is opaque. The three others work. This gives:. With this proof and some variants of it , log2 has the same strange behaviour. And this proof seems to use only transparent objects, so maybe the problem is not there.
How can I define a Function that returns understandable results on specific values? I've managed to pin-point the place that causes troubles: it's inversion H2. It turns out we don't need that case-analysis and intuition can finish the proof it doesn't pattern-match on H2 :. If we use lemma1 with this proof, the computation of log2 10 results in 3. I believe the Using Coq's evaluation mechanisms in anger blog post by Xavier Leroy explains this kind of behavior.
In our case we eliminate the proof of inequality inversion H2. And the reason inversion H1. Well, almost everywhere -- computations with large nat s can result in stack overflow or segmentation fault, as Coq warns us:. Warning: Stack overflow or segmentation fault happens when working with large numbers in nat observed threshold may vary from to depending on your system limits and on the command executed.
If we look at the term produced by Eval in a case where evaluation fails to produce a nat , we'll see something along these lines:. It's easy to see that x0 : Prop , so it gets erased when extracting the functional program log2 into, say OCaml, but Coq's internal evaluation mechanism have to use it to ensure termination.
The reduction behavior of functions defined by well-founded recursion in Coq is generally not very good, even when you declare your proofs to be transparent. The reason for this is that arguments of well-foundedness usually need to be done with complicated proof terms.
Since these proofs terms end up appearing in well-founded recursive definitions, "simplifying" your function will make all of those proof terms appear, as you noticed. It is easier to rely on custom tactics and lemmas to reduce functions defined this way. TyanL Failed Tests.
Fork Discuss 0. Fork Discuss 5 Fixture. Fork Discuss 1 Fixture. Test; import static org. You have received 2 names.
The interpretation process may produce some proof obligations which need to be resolved to create the final term. If we go from T to the subset of T verifying property P , we must prove that the object under consideration verifies it. Russell will generate an obligation for every such coercion. In the other direction, Russell will automatically insert a projection.
Another distinction is the treatment of pattern matching. Apart from the following differences, it is equivalent to the standard match operation see Extended pattern matching. Generation of equalities. A match expression is always generalized by the corresponding equality.
As an example, the expression:. This permits to get the proper equalities in the context of proof obligations inside clauses, without which reasoning is very limited. Generation of disequalities. If a pattern intersects with a previous one, a disequality is added in the context of the second branch. If the object being matched is coercible to an inductive type, the corresponding coercion will be automatically inserted.
This also works with the previous mechanism. This flag controls the special treatment of pattern matching generating equalities and disequalities when using Program it is on by default. All pattern-matches and let-patterns are handled using the standard algorithm of Coq see Extended pattern matching when this flag is deactivated.
This flag controls the coercion of general inductive types when using Program the flag is on by default. Coercion of subset types and pairs is still active in this case. This flag enables the program mode, in which 1 typechecking allows subset coercions and 2 the elaboration of pattern matching of Fixpoint and Definition acts as if the program attribute has been used, generating obligations if there are unresolved holes after typechecking. This boolean attribute allows using or disabling the Program mode on a specific definition.
An alternative and commonly used syntax is to use the legacy Program prefix cf. Likewise, the if construct is not treated specially by Program so boolean tests in the code are not automatically reflected in the obligations.