-
Notifications
You must be signed in to change notification settings - Fork 141
Add Coq #425
Comments
If I'm not misunderstanding, Coq is very different kind of language. Any specific examples of exercises? How can it be tested? http://coq.inria.fr/ |
There are quite a few here, for example:
The Software Foundations book comes with grading scripts that cover some of the exercises. Here is one. |
How do you test that submitted solution has valid proof for (* Define an inductive proposition `subseq` on `list nat` *)
(* Inductive subseq : list nat -> list nat -> Prop := . *)
(* Prove that subsequence is reflexive. Any list is a subsequence of itself. *)
Theorem subseq_refl :
1 + 1 = 2. (* irrelevant fact *)
Proof.
reflexivity.
Qed. Supporting something like the following (first exercise from the book) is more straight forward, but less interesting: (* Define `nandb` which returns `true` if either or both of its inputs are `false`. *)
Inductive bool : Type :=
| true : bool
| false : bool.
Definition negb (b:bool) : bool :=
match b with
| true => false
| false => true
end.
Definition nandb (b1:bool) (b2:bool) : bool :=
match b1 with
| true => (negb b2)
| false => true
end.
(* compile-time tests? *)
Definition test_nandb1 : (nandb true false) = true :=
eq_refl.
Definition test_nandb2 : (nandb false false) = true :=
eq_refl.
Definition test_nandb3 : (nandb false true) = true :=
eq_refl.
Definition test_nandb4 : (nandb true true) = false :=
eq_refl. Also, we need to output test results in Codewars' format. |
The way proofmarket used to do it is use a file specifically for checking which resembles something like this: Require Import Definitions.
Require Import Solutions.
Theorem subseq_refl_check : (* the exact statement *)
Proof.
exact subseq_refl
Qed. If the user has changed the type of |
Thanks @gallais :) (* Solution.v *)
Theorem identity : 1 * 1 = 1.
Proof.
reflexivity.
Qed.
(* Check.v *)
Theorem identity_check : forall A, A -> A.
Proof.
exact identity.
Qed.
(* Definition.v *)
Definition identity_def := forall A, A -> A.
(*
Error:
The term "identity" has type "1 * 1 = 1" while it is expected to have type
"identity_def".
*) |
This comment has been minimized.
This comment has been minimized.
A potential issue I just discovered: in Coq it is possible to defer a proof using tactic (* Solution.v *)
Theorem solution : 1 + 1 = 2.
Proof.
admit.
Admitted. Even if I then use this admitted "proof" in another proof as follows: (* TestSuite.v *)
Require Import Solution.
Theorem test_suite : 1 + 1 = 2.
Proof.
exact solution.
Qed. ... both files can still be successfully compiled using |
Do you really get a |
Yes - copying/pasting the files as-is and executing |
This comment has been minimized.
This comment has been minimized.
@DonaldKellett Print Assumptions my_definition. |
opam install coq work well for me on linux. |
I think I found how to do that. Using the same example
The exit code is still zero and I don't think there's a way to make it considered error. Also found this:
I'll put Coq on hold for now. For theorem proving, we now have Agda (and Idris). I'm pretty happy with how Agda turned out and excited to see it opening up possibilities for new challenges. Agda was easy to work with and it seems to fit Codewars pretty well. I was hoping Coq to be similar, but I have a feeling that it'll be significantly more difficult and I'm honestly not really motivated. |
I would say that using coqchk is probably not really needed for this application. Running |
Thanks for your input @Zimmi48 , glad to see that this language request for Codewars has attracted the attention of a official Coq maintainer 🎉 With regards to Coq exercises involving customized allowed axioms, there's currently no feasible way to tell the CW runner which axioms are allowed on a kata-to-kata basis since the output testing is not done within Coq itself. Perhaps a suitable tradeoff would be to let a Coq solution pass if and only if the entire suite (preloaded, solution, tests) compiles and the output message from @kazk What do you think? |
I'm fine with that. We might find a better way later too. Can anyone provide some examples similar to the Agda one (#719 (comment))? I'd like to experiment with some variations and see how the output looks like. More examples will be better. Also, Coq doesn't have module declaration right? So I guess we'll just have fixed file names like |
I'm not exactly sure how you're planning to use it but Coq does have a module system. Example:
|
Some updates... Coq module works little bit different from what I imagined from @Zimmi48's comment.
Only considering passed when the output is exactly Software Foundations book OP posted got some updates and it's now a series (maybe it was before, but I don't remember seeing other volumes). The volume 4 is called QuickChick: Property-Based Testing in Coq. Also, the original book's grading script contains something like From Coq Require Export String.
Parameter MISSING: Type.
Module Check.
Ltac check_type A B :=
match type of A with
| context[MISSING] => idtac "Missing:" A
| ?T => first [unify T B; idtac "Type: ok" | idtac "Type: wrong - should be (" B ")"]
end.
End Check. Maybe we can have our version of something like this? It might help with having a better output. |
Sorry about not being fully clear earlier. Files define modules and the filename is the name of the module. Explicit module declarations are only used to define nested modules. The |
@Zimmi48 Thanks for clarifying. Also, no need to apologize, I really appreciate your help. Are the error messages in certain format? If it is, I can do some postprocessing to make it display better on Codewars. I was able to do that for Agda because it has a consistent format using a pretty printer. So far, I've seen errors like
written to stderr. It'll be helpful if I can see other possible errors. I think we can do
|
Coq error messages will just be printed like you saw, with location information and a leading "Error: ". Warnings will be printed with a leading "Warning: ". I can show you the file with the printing business but the error messages themselves are not gathered in a single place like in Agda but all over the code. We have documented many error and warning messages in https://coq.inria.fr/refman/coq-exnindex.html. However, the list is not exhaustive yet, especially regarding warnings. |
For simple type-checking tests, the following code will work:
Command: Result:
We'll need post-processing for context summary part to convert it to a CW pass/fail. For errors, only the first error is displayed when a source file has multiple issues, so I guess "printing a huge wall of text" is not very likely. From my experience, error/warning messages start with |
@Bubbler-4 I like that. We can have a module with code like
Can you post some example outputs and what should be considered a failure? |
Any global axioms like the following
are shown at the
Ending with |
Is there some kind of caching like Agda? It doesn't seem like it because running the command again produces the same output and takes as long. |
@kazk I couldn't find a way around it either. Then we should go back to
Now the problem is how to process it. My idea:
This regex will probably work. |
It is possible to use the |
I think we're almost there. Errors and warnings written to stderr are wrapped and appended to stdout output. I don't know if we'll ever have multiple errors and warnings at the same time, but it should be supported. I'm using this pattern to extract. Not trying to perfect this and will adjust as we find edge cases. |
If there's an |
Yeah, |
I'm preparing for alpha release and need a super basic (probably not worth making a kata) example. Can anyone provide a Coq version of #719 (comment)? It doesn't need to be exact. Failing version, working version, and some check. Solution is written to |
Here is one (not tested though). Initial solution:
or (this will make the compiler complain about open goal)
Complete solution:
Test:
|
Maybe something like this? Initial Solution ( Theorem eq_trans : forall a b c : Type,
a = b -> b = c -> a = c.
Proof.
(* TODO: Prove it *)
Admitted. Complete Solution ( Theorem eq_trans : forall a b c : Type,
a = b -> b = c -> a = c.
Proof.
intros a b c eq1 eq2.
rewrite -> eq1.
rewrite -> eq2.
reflexivity.
Qed. Test Cases ( Add LoadPath "/Users/donaldl/Desktop/".
Require Import Solution.
Theorem eq_trans_test : forall a b c : Type,
a = b -> b = c -> a = c.
Proof.
exact eq_trans.
Qed.
Print Assumptions eq_trans_test. Running
And with the final solution:
|
An example with Preloaded ( Definition preloaded_def (n : nat) := n + n. Initial solution ( Require Import Preloaded.
From Coq Require Import Arith.
Lemma preloaded_lemma (n : nat) : preloaded_def n = n * 2.
Proof. Admitted. Complete solution ( Require Import Preloaded.
From Coq Require Import Arith.
Lemma preloaded_lemma (n : nat) : preloaded_def n = n * 2.
Proof.
unfold preloaded_def; rewrite -> mult_comm; simpl.
rewrite <- plus_n_O; reflexivity.
Qed. Tests ( Require Preloaded.
Require Import Check Solution.
Goal True.
check_type preloaded_lemma (forall n, Preloaded.preloaded_def n = n * 2).
Abort.
Print Assumptions preloaded_lemma. Attempt to cheat ( Require Import Preloaded.
Definition preloaded_def (n : nat) := n * 2.
Lemma preloaded_lemma n : preloaded_def n = n * 2.
Proof. reflexivity. Qed. It fails with the message |
Thanks! I'll try releasing tonight after work. |
Coq is now available on Codewars! Thanks for all the help. Have fun and please let me know if you find any issues. |
Where can I find Coq on codewars? I don't see it on the main page of https://www.codewars.com , and can't find code for it in this repo... |
https://www.codewars.com/kata/search/coq Currently there's 7 kata. |
There are at least 8 Coq katas. For some reason, this kata is not displayed in the search results when Coq is selected. |
@monadius sometimes Codewars fails to update the search index. I just did it manually and it now shows 8. |
And there are three more after some translations get approved |
@kazk could you change the default test code to the following: Require Solution.
Theorem example_test : forall a b c : Type,
a = b -> b = c -> a = c.
Proof.
exact Solution.example.
Qed.
Print Assumptions example_test. The main issue with |
Updated the default test code and fixed the output of pending proofs. |
Coq is a programming language for dependently-typed programming. While it is popular in certain niches, there are few snippets of idiomatic Coq code across the web. Adding Coq support to Code Wars would help remediate that.
There are tons of Coq exercises available online which could serve as katas, for example in the MIT-licensed Software Foundations.
From #720
The following are optional, but will help us add the language:
coqc solution.v
to compile the user solutionsolution.v
. AFAIK only 1 file can be compiled at a time so the entire suite (preloaded, solution, test cases) may need 3 separate calls tocoqc
. There is alsocoqchk
which checks the compiled.v
files, e.g.coqchk solution.vo
wheresolution.vo
was compiled fromcoqc solution.v
believe_me
orassert_total
.👍 reaction might help. Special mentions: @ice1000 , @MarisaKirisame
coq.js
from ejgallego/CodeMirror)The text was updated successfully, but these errors were encountered: