-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdefault_prompts.py
1967 lines (1788 loc) · 72.5 KB
/
default_prompts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from lang_config import LANG, ALL_LANGS
from cmdline import args
do_code_begin = args.model_host == 'huggingface' # TODO: quick hack
if LANG == "Dafny":
proof_marker = "ensures"
cheat_marker = "{:axiom}"
elif LANG == "Coq":
proof_marker = "Qed"
cheat_marker = "Admitted"
elif LANG == "Lean4":
proof_marker = "theorem"
cheat_marker = "sorry"
elif LANG == "Rust" or LANG == "Scala" or LANG == "Python":
proof_marker = None
cheat_marker = None
NO_CHECK_PROOF = lambda v: True
if proof_marker:
CHECK_PROOF = lambda v: proof_marker in v
CHECK_PROOF2 = lambda v: v.count(proof_marker) >= 2
else:
CHECK_PROOF = NO_CHECK_PROOF
CHECK_PROOF2 = NO_CHECK_PROOF
NO_CHECK_CHEAT = lambda v: False
if cheat_marker:
CHECK_CHEAT = lambda v: cheat_marker in v
else:
CHECK_CHEAT = NO_CHECK_CHEAT
problem_multistep_python = (
f"""### Spec: In {LANG}, write a function sum_unique_prime_factors(n) that returns the sum of the unique prime factors of
the natural number n. Example: if the input number was 28, the function would return 9 (2 + 7),
if the input was 45, it would return 8 (3 + 5) and if the input was 97, it would return 97, as 97 is prime.
""",
2000,
None,
5,
25,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
['Python'],
None,
{
"def sum_unique_prime_factors(n):": "test(sum_unique_prime_factors(28) == 9)\ntest(sum_unique_prime_factors(45) == 8)\ntest(sum_unique_prime_factors(60) == 10)"
}
)
problem_python_2_oneshot = (
f"""### Spec: In {LANG}, write a magic(s) function that: given a string s, it finds all the characters that repeat an odd
number of times in the string and remove them from it. Then, for every pair of the same character next to each other,
you should remove one of them. Then, it should rotate any letters in the string by the length of the resulting string
(rotate A two times would result in a C, rotating H once would result in an I and rotating F 26 times would result back in F).
It should return this final function.
""",
2000,
None,
5,
25,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
['Python'],
None,
{
"def magic(s):": "test(magic('vvbbbsszzzzcc kklllccffzzrr!!') == 'hello world!')"
}
)
problem_python_2_steps = (
f"""### Spec: In {LANG}, write a function remove_odd(s), that given a string s, it removes the characters that
appear on it an odd number of times. Then, write a function 'remove_pairs(s)', that given a string, for every
pair of the same character next to each other, it removes one of them ('hheelllloo' would become 'hello'). Then,
write another function 'rotate_letters(s, n)', that given a string and a number, it rotates the letters of the alfabet
by n (rotating A once gives B, rotating D twice gives F and so on). Then, write a final function 'magic(s)' that,
using the previous functions, removes the odd appearing characters, remove pairs of characters, and after all these
changes, it rotates the letters of this final string by the its final number of characters.
""",
2000,
None,
5,
25,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
['Python'],
None,
{
"def remove_odd(s):" : "test(remove_odd('kkkppoottaiiiattoojjj') == 'ppoottaattoo')",
"def remove_pairs(s):" : "test(remove_pairs('ppoottaattoo') == 'potato')\ntest(remove_pairs('hheelllloo') == 'hello')",
"def rotate_letters(s, n):" : "test(rotate_letters('abc', 1) == 'bcd')",
"def magic(s):" : "test(magic('vvbbbsszzzzcc kklllccffzzrr!!') == 'hello world!')"
}
)
problem_python_3_oneshot = (
f"""### Spec: In {LANG}, Write a function string_to_number(s) that given a string s, converts each of its alpha numeric characters into numbers. The convesion
is as follows: a or A is coverted to 1, b or B is converted to 2 and so on until z or Z is converted to 26. Characters that represent
digits should be converted to themselves, and all other characters should be converted to 0. The function string_to_numbers(s) should
return the sum of the converted list. Then, write a function sum_unique_prime_factors(n) that given a number n, returns the sum of the unique prime factors of n.
After, write a function magic(n) that calculates and returns the magic number of a string. The magic number of a string is obtained by
converting the string to a number using the string_to_number function, then using sum_unique_prime_factors in such number. Finally,
write a function best_string(t) that, given a text t, returns the string in such test that yields the highest magic number""",
2000,
None,
5,
25,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
['Python'],
None,
{
"def best_string(t):": "test(best_string('Hello world! A quick example with numbers 123 and special characters $%#.') == 'quick')"
}
)
problem_python_3_steps = (
f"""### Spec: In {LANG}, Write a function string_to_number(s) that given a string s, converts each of its alpha numeric characters into numbers. The convesion
is as follows: a or A is coverted to 1, b or B is converted to 2 and so on until z or Z is converted to 26. Characters that represent
digits should be converted to themselves, and all other characters should be converted to 0. The function string_to_numbers(s) should
return the sum of the converted list. Then, write a function sum_unique_prime_factors(n) that given a number n, returns the sum of the unique prime factors of n.
After, write a function magic(n) that calculates and returns the magic number of a string. The magic number of a string is obtained by
converting the string to a number using the string_to_number function, then using sum_unique_prime_factors in such number. Finally,
write a function best_string(t) that, given a text t, returns the string in such test that yields the highest magic number""",
2000,
None,
5,
25,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
['Python'],
None,
{
"def string_to_number(s):" : "test(string_to_number('quick!') == 61)",
"def sum_unique_prime_factors(n):" : "test(sum_unique_prime_factors(51) == 20)",
"def magic(s):" : "test(magic('Hello') == 15)",
"def best_string(t):" : "test(best_string('Hello world! A quick example with numbers 123 and special characters $%#.') == 'quick')"
}
)
problem_python_4_oneshot = (
f"""### Spec: In {LANG},
First, write a function palindrome(s) that checks whether a string as is a palindrome, returning True or False accordingly. Then,
write a function find_palindrome(t), that given a text, returns the last palindrome word of the text. If no palindromes are found, it
should simply return the last word of the text. Then, write a function vowel_count(s) that given a word, count the number of vowels in it (vowels are only
a, e, i, o, u). Then, write a function find_char(s, n), that given a string s and a number n, finds the nth character of the string modulo
the length. Finally, write function process_palindrome(t) using the previous functions that given a text t, returns the nth character of
the last palindrome of a text where n is the number of vowels in such palindrome.""",
2000,
None,
5,
25,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
['Python'],
None,
{
"def process_palindrome(t):": "test(process_palindrome('Madam, Anna went to the market. She also met Civic in Nevada.') == 'v')"
}
)
problem_python_4_steps = (
f"""### Spec: In {LANG},
First, write a function palindrome(s) that checks whether a string as is a palindrome, returning True or False accordingly. Then,
write a function find_palindrome(t), that given a text, returns the last palindrome word of the text. If no palindromes are found, it
should simply return the last word of the text. Then, write a function vowel_count(s) that given a word, count the number of vowels in it (vowels are only
a, e, i, o, u). Then, write a function find_char(s, n), that given a string s and a number n, finds the nth character of the string modulo
the length. Finally, write function process_palindrome(t) using the previous functions that given a text t, returns the nth character of
the last palindrome of a text where n is the number of vowels in such palindrome.""",
2000,
None,
5,
25,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
['Python'],
None,
{
"def palindrome(s):" : "test(palindrome('Anna') == True) \ntest(palindrome('hannah') == True \npalindrome(trevor) == False)",
"def find_palindrome(t):" : "test(find_palindrome('Madam, Anna went to the market. She also met Civic in Nevada.') == 'Civic')\n test(find_palindrome('Madam, Anna went to the market. She also met Civic in Nevada.') == 'Civic')",
"def vowel_count(s):" : "test(vowel_count(anna) == 2) \ntest(vowel_count(PotAto) == 3)",
"def find_char(s, n):" : "find_char('Civic', 2) == 'v'",
"def process_palindrome(t):" : "test(process_palindrome('Madam, Anna went to the market. She also met Civic in Nevada.') == 'v')"
}
)
problem_parser_res = (
f"""### Spec: In {LANG}, write a parser function for arithmetic expressions that contain only addition.
The function should take a string that contains a sequence of integers and '+' symbols, and return its result.
No characters other than digits and '+' may be in the string.
Example: "1" evaluates to 1. "1+1" evaluates to 2. "100+3" evaluates to 103. "1+2+3+4+5+8+12" evaluates to 35.
""",
2000,
None,
5,
25,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
ALL_LANGS,
None,
None
)
problem_parser_myfib = (
f"""### Spec: In {LANG}, write a function called fib that generates the number of factors for the n-th Fibonacci number,
where n is the input. The function should take an integer n and return its result. Fibonacci numbers are defined
as the first term being 0, the second term being 1, and the third term being the sum of the first two terms, which
is 1. The fourth term is the sum of the two prior terms, which equals 2. The fifth term is the sum of the third and
fourth term, which is 3. Every succeeding term is equal to the sum of the two preceding terms. Let the first term
in the series be 0, and let the second term in the series be 1.
Example: The function should take in 3, which gives Fibonacci number 1, and evaluate to 1.
7 evaluates to 4. 9 evaluates to 4. 144 evaluates to 15.
""",
2000,
None,
5,
25,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
ALL_LANGS,
None,
None
)
problem_parser_race = (
f"""### Spec: In {LANG}, write a function that takes can solve the problem: if I came in the n-th person to finish a race
how many people crossed the finish line before me?
Example: If I came in 5th place, 4 people crossed the finished line before me. So the function takes in 5 and returns 4. If I
come in 100th place, the function takes in 100, and it returns 99.
""",
2000,
None,
2,
25,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
ALL_LANGS,
None,
None
)
problem_parser_quad = (
f"""### Spec: In {LANG}, write a function that calculates the roots of a quadratic equation ax^2 + bx + c,
where the coefficients a, b, c, are provided by the user.
Example: if the inputs are 1, 2, 1, then the function's result is -1. If the inputs are 1, 1, -6, then the
function's results are -3 and 2.
""",
2000,
None,
5,
25,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
ALL_LANGS,
None,
None
)
problem_parser_word = (
f"""### Spec: In {LANG}, write a function that extracts the top N most frequent words in the paragraph. The function
should take in a paragraph and an integer N, and return the N words that appear the most in the paragraph and their
respective counts.
Example: "Natural language processing (NLP) is a subfield of linguistics, computer science, and artificial intelligence
concerned with the interactions between computers and human language, in particular how to program computers
to process and analyze large amounts of natural language data." and 5 results in "language: 2, natural: 2, processing: 1, nlp: 1, is: 1".
""",
2000,
None,
5,
25,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
ALL_LANGS,
None,
None
)
problem_parser_data = (
f"""### Spec: In {LANG}, write a parser function for arithmetic expressions that contain only addition and multiplication.
First, create a data structure that stores expressions.
Then write a function takes a string that contains a sequence of integers and '+', '*' symbols, and return the expression in the data structure you defined earlier.
Both operators have the same precedence. There are no parentheses.
No characters other than digits and '+', '*' may be in the string.
""",
2000,
None,
5,
25,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
ALL_LANGS,
None,
None,
)
problem_parser_interest = (
f"""### Spec: Tom decides to invest some money in a bank. He deposits D dollars into the account, which pays an annual interest rate of r%.
However, he also plans to deposit an additional A dollars every year into the same account. The interest is compounded annually.
In {LANG}, write a function that will calculate the total amount of money Tom will have in his account after n years.
Example: if the initial deposit is 5000 dollars, the annual interest rate is 5%, and the yearly deposit is 1200 dollars, then after 10 years,
Tom will have approximately $23,580.79 in his account. This means that the function takes in its inputs 5000 and 5 and 1200 and 10 and returns
23,580.79.
""",
2000,
None,
5,
25,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
ALL_LANGS,
None,
None
)
problem_fact = (
f"""### Spec: In {LANG}, write a factorial function and prove that the factorial is always strictly positive.
{'''### Hint: Use a plain function, NOT a function method.
### Hint: Use a nat, NOT an int.
''' if LANG=='Dafny' else ''
}{'''### Hint: Don't forget to import the Arith module.
### Hint: use `Nat.lt_0_1` in the base case of the proof.
### Hint: use `Nat.lt_lt_add_r` in the inductive case of the proof.
''' if LANG=='Coq' else ''
}""",
500,
None,
5,
15,
CHECK_PROOF, CHECK_CHEAT,
ALL_LANGS,
None,
None
)
problem_fact_dafny_check = (
f"""### Spec: In {LANG}, write a factorial function, called `fac`, and prove (in a lemma called `FacPositive(n: nat)`) that the factorial is always strictly positive.
{'''### Hint: Use a plain function, NOT a function method.
### Hint: Use a nat, NOT an int.
''' if LANG=='Dafny' else ''
}{'''### Hint: Don't forget to import the Arith module.
### Hint: use `Nat.lt_0_1` in the base case of the proof.
### Hint: use `Nat.lt_lt_add_r` in the inductive case of the proof.
''' if LANG=='Coq' else ''
}""",
500,
None,
5,
15,
CHECK_PROOF, CHECK_CHEAT,
['Dafny'],
"""
lemma CHECK_FacPositive(n: nat) ensures fac(n) > 0 { FacPositive(n); }
""",
None
)
problem_fact_coq_check = (
f"""### Spec: In {LANG}, write a factorial function, called `fac`, and prove (in a lemma `FacPositive: forall (n: nat), fac n > 0`) that the factorial is always strictly positive.
{'''### Hint: Use a plain function, NOT a function method.
### Hint: Use a nat, NOT an int.
''' if LANG=='Dafny' else ''
}{'''### Hint: Don't forget to import the Arith module.
### Hint: use `Nat.lt_0_1` in the base case of the proof.
### Hint: use `Nat.lt_lt_add_r` in the inductive case of the proof.
''' if LANG=='Coq' else ''
}""",
500,
None,
5,
15,
CHECK_PROOF, CHECK_CHEAT,
['Coq'],
"""
Lemma CHECK_FacPositive: forall (n: nat), fac n > 0. Proof. intros. apply FacPositive; eauto. Qed.
""",
None
)
problem_mult_proof_coq = (
"""
```Coq
Require Import Arith.
Theorem mult_lemma6 : forall a b n : nat, n <> 0 -> n * a = n * b -> a = b.
Proof.
""",
1000,
None,
4,
40,
CHECK_PROOF, CHECK_CHEAT,
['Coq'],
None,
None
)
problem_1_divides_n_proof_coq = (
"""
```Coq
Require Import Arith.
Require Import Peano_dec.
Require Import Compare_dec.
Require Import Wf_nat.
Inductive divides : nat -> nat -> Prop :=
dividesDef : forall a b q : nat, b = q * a -> divides a b.
Lemma SO_divides_all : forall n : nat, divides 1 n.
Proof.
""",
1000,
None,
22,
40,
CHECK_PROOF, CHECK_CHEAT,
['Coq'],
None,
None)
problem_partial_maps_proof_coq = (
"""
```Coq
From Coq Require Import Arith.Arith.
From Coq Require Import Bool.Bool.
Require Import Coq.Strings.String.
From Coq Require Import Logic.FunctionalExtensionality.
From Coq Require Import Lists.List.
Import ListNotations.
Set Default Goal Selector "!".
Definition total_map (A : Type) := string -> A.
Definition t_empty {A : Type} (v : A) : total_map A :=
(fun _ => v).
Definition t_update {A : Type} (m : total_map A)
(x : string) (v : A) :=
fun x' => if String.eqb x x' then v else m x'.
Notation "'_' '!->' v" := (t_empty v)
(at level 100, right associativity).
Notation "x '!->' v ';' m" := (t_update m x v)
(at level 100, v at next level, right associativity).
Definition examplemap' :=
( "bar" !-> true;
"foo" !-> true;
_ !-> false
).
Definition partial_map (A : Type) := total_map (option A).
Definition empty {A : Type} : partial_map A :=
t_empty None.
Definition update {A : Type} (m : partial_map A)
(x : string) (v : A) :=
(x !-> Some v ; m).
Notation "x '|->' v ';' m" := (update m x v)
(at level 100, v at next level, right associativity).
Notation "x '|->' v" := (update empty x v)
(at level 100).
Definition examplepmap :=
("Church" |-> true ; "Turing" |-> false).
Definition includedin {A : Type} (m m' : partial_map A) :=
forall x v, m x = Some v -> m' x = Some v.
Theorem includedin_update : forall (A : Type) (m m' : partial_map A)
(x : string) (vx : A),
includedin m m' ->
includedin (x |-> vx ; m) (x |-> vx ; m').
Proof.
""",
1000,
None,
22,
40,
CHECK_PROOF, CHECK_CHEAT,
['Coq'],
None,
None)
problem_opt0_proof_coq = (
"""
```Coq
Require Import Coq.Strings.String.
Require Import Arith.
Inductive Expr : Type :=
| Const : nat -> Expr
| Var : string -> Expr
| Add : Expr -> Expr -> Expr.
Fixpoint Eval (e : Expr) (env : string -> nat) : nat :=
match e with
| Const n => n
| Var x => env x
| Add e1 e2 => Eval e1 env + Eval e2 env
end.
Fixpoint Optimize (e : Expr) : Expr :=
match e with
| Const n => e
| Var x => e
| Add e1 e2 =>
match e1, e2 with
| Const 0, _ => Optimize e2
| _, Const 0 => Optimize e1
| _, _ => Add (Optimize e1) (Optimize e2)
end
end.
Theorem Optimizer_preserves_semantics :
forall e env, Eval (Optimize e) env = Eval e env.
Proof.
""",
1000,
None,
22,
40,
CHECK_PROOF, CHECK_CHEAT,
['Coq'],
None,
None)
problem_opt0_coq_proof_hints = """
### Hint: For the proof, do `induction e.`. Do NOT name the hypotheses with `as`.
### Hint: The simple cases are by `simpl. reflexivity.`.
### Hint: The addition case is by `simpl. rewrite <- IHe1. rewrite <- IHe2. destruct (optimize e1); destruct (optimize e2); try destruct n; try destruct n0; eauto using PeanoNat.Nat.add_0_r.`.
### Hint: You'll need `Require Import Arith`.
"""
EXTRA_CONSTANT_FOLDING = " and performs all additions by constants"
EXTRA_CONSTANT_FOLDING = ""
hint_match_dafny = '''### Hint: Recall that in Dafny, pattern match takes the form
match e
case Foo(x, y) => 1
case Bar(x) => 2
case _ => 3
''' if LANG=='Dafny' else ''
hint_match_dafny = hint_match_dafny if args.show_hint_match_dafny else ''
problem_opt0 = (
f"""### Spec: In {LANG}, write an ADT for arithmetic expressions comprising constants, variables and binary additions. Then write an evaluator taking an expression and an environment (a function that takes a variable name and returns a number) and returning the number resulting from evaluation. Then write an optimizer taking an expression and returning an expression with all additions by 0 removed{EXTRA_CONSTANT_FOLDING}. Then prove that the optimizer preserves the semantics as defined by the evaluation function.
{hint_match_dafny}### Hint: In the optimizer, recursively optimize the sub-expressions.
{'''### Hint: For the proof, just do a simple pattern match (match not if) and call the lemma recursively without adding asserts.
''' if LANG=='Dafny' else ''
}{'''### Hint: You can import the `string` datatype with the line `Require Import Coq.Strings.String.`.
### Hint: Use Fixpoint instead of Definition for recursive functions.
### Hint: With tactics like `induction` and `destruct`, _avoid_ naming with `as` and let Coq pick the names for you. For example, use `induction e.` but _not_ `induction e as [...]`.
''' + problem_opt0_coq_proof_hints if LANG=='Coq' else ''
}""",
1000,
None,
22,
40,
CHECK_PROOF, CHECK_CHEAT,
ALL_LANGS,
None,
None
)
problem_opt0_python = (
f"""### Spec: In {LANG}, write an ADT for arithmetic expressions comprising constants, variables and binary additions. Then write an evaluator taking an expression and an environment (a function that takes a variable name and returns a number) and returning the number resulting from evaluation. Then write an optimizer taking an expression and returning an expression with all additions by 0 removed{EXTRA_CONSTANT_FOLDING}. Then prove that the optimizer preserves the semantics as defined by the evaluation function.
{hint_match_dafny}### Hint: In the optimizer, recursively optimize the sub-expressions.
{'''### Hint: For the proof, just do a simple pattern match (match not if) and call the lemma recursively without adding asserts.
''' if LANG=='Dafny' else ''
}{'''### Hint: You can import the `string` datatype with the line `Require Import Coq.Strings.String.`.
### Hint: Use Fixpoint instead of Definition for recursive functions.
### Hint: With tactics like `induction` and `destruct`, _avoid_ naming with `as` and let Coq pick the names for you. For example, use `induction e.` but _not_ `induction e as [...]`.
''' + problem_opt0_coq_proof_hints if LANG=='Coq' else ''
}""",
1000,
None,
22,
40,
CHECK_PROOF, CHECK_CHEAT,
["Python"],
None,
{
"\ndef optimize": "test(optimize(Const(5)).evaluate() == 5)"
}
)
problem_opt0_dafny_check = (
f"""### Spec: In {LANG}, write an ADT for arithmetic expressions (called `Expr`) comprising constants, variables and binary additions. Then write an evaluator (called `Eval`) taking an expression and an environment (a function that takes a variable name and returns a number) and returning the number resulting from evaluation. Then write an optimizer (called `Optimize`) taking an expression and returning an expression with all additions by 0 removed{EXTRA_CONSTANT_FOLDING}. Then prove that the optimizer preserves the semantics as defined by the evaluation function. Do so by proving the lemma `OptimizePreservesSemantics(e: Expr, env: string -> int) ensures Eval(Optimize(e), env) == Eval(e, env)`.
{hint_match_dafny}### Hint: In the optimizer, recursively optimize the sub-expressions.
{'''### Hint: For the proof, just do a simple pattern match (match not if) and call the lemma recursively without adding asserts.
''' if LANG=='Dafny' else ''
}{'''### Hint: You can import the `string` datatype with the line `Require Import Coq.Strings.String.`.
### Hint: Use Fixpoint instead of Definition for recursive functions.
### Hint: With tactics like `induction` and `destruct`, _avoid_ naming with `as` and let Coq pick the names for you. For example, use `induction e.` but _not_ `induction e as [...]`.
''' + problem_opt0_coq_proof_hints if LANG=='Coq' else ''
}""",
1000,
None,
22,
40,
CHECK_PROOF, CHECK_CHEAT,
["Dafny"],
"""
lemma CHECK_OPS(e: Expr, env: string -> int)
requires true
ensures Eval(Optimize(e), env) == Eval(e, env)
{
OptimizePreservesSemantics(e, env);
}
""",
None
)
problem_opt0_coq_check = (
f"""### Spec: In {LANG}, write an ADT for arithmetic expressions (called `Expr`) comprising constants, variables and binary additions. Then write an evaluator (called `Eval`) taking an expression and an environment (a function that takes a variable name and returns a number) and returning the number resulting from evaluation. Then write an optimizer (called `Optimize`) taking an expression and returning an expression with all additions by 0 removed{EXTRA_CONSTANT_FOLDING}. Then prove that the optimizer preserves the semantics as defined by the evaluation function. Do so by proving the lemma `OptimizePreservesSemantics: forall (e: Expr) (env: string -> nat), Eval(Optimize(e), env) = Eval(e, env)`.
{hint_match_dafny}### Hint: In the optimizer, recursively optimize the sub-expressions.
{'''### Hint: For the proof, just do a simple pattern match (match not if) and call the lemma recursively without adding asserts.
''' if LANG=='Dafny' else ''
}{'''### Hint: You can import the `string` datatype with the line `Require Import Coq.Strings.String.`.
### Hint: Use Fixpoint instead of Definition for recursive functions.
### Hint: With tactics like `induction` and `destruct`, _avoid_ naming with `as` and let Coq pick the names for you. For example, use `induction e.` but _not_ `induction e as [...]`.
''' + problem_opt0_coq_proof_hints if LANG=='Coq' else ''
}""",
1000,
None,
22,
40,
CHECK_PROOF, CHECK_CHEAT,
["Coq"],
"""
Lemma CHECK_OPS: forall (e: Expr) (env: string -> nat), Eval (Optimize e) env = Eval e env.
Proof.
intros.
apply OptimizePreservesSemantics; eauto.
Qed.
""",
None
)
problem_opt0_opt_dafny = ("""### Spec: In Dafny, write an ADT for arithmetic expressions comprising constants, variables and binary addition. Then write a predicate `optimal` that holds on an expression if it has no additions by 0. Then write an optimizer `optimize` that removes all additions by 0. Then write a lemma `OptimizerOptimal` that ensures `optimal(optimize(e))` for all expressions `e`.
### Hint: Don't use the same structure for `optimize` as for `optimal`. Instead, follow the next hint.
### Hint: In the addition case, the `optimize` function should recursively optimize the sub-expressions and then match on the optimized sub-expressions.
### Hint: Do NOT use `requires` anywhere.
### Hint: Write the lemma as
lemma OptimizerOptimal(e: Expr)
ensures optimal(optimize(e))
### Hint: For the proof, just do a simple pattern match (match not if) and call the lemma recursively without adding asserts.
```dafny
datatype Expr = Const(i: int) | Var(x: string) | Add(e1: Expr, e2: Expr)
predicate optimal(e: Expr) {
match e
case Add(Const(0), _) => false
case Add(_, Const(0)) => false
case Add(e1, e2) => optimal(e1) && optimal(e2)
case _ => true
}
""",
1000,
None,
22,
40,
CHECK_PROOF, CHECK_CHEAT,# if LANG != 'Dafny' else (lambda v: CHECK_CHEAT(v) or "requires" not in v or "==>" not in v),
['Dafny'],
None,
None
)
problem_pattern_match_train_dafny = (("""In Dafny, we have the following ADT:
```dafny
datatype Foo = Bar(n: nat) | Baz(a: Foo, b: Foo)
```
Take the denotation of a Foo to be 2 times n for a Bar, and sum of the denotations of its Foos for a Baz.
```dafny
function denotation(foo: Foo): nat
{
""", [
"""
```dafny
lemma ex1()
ensures denotation(Baz(Bar(2), Bar(1))) == 6
{}
lemma denotationAlwaysEven(foo: Foo)
ensures denotation(foo) % 2 == 0
{
match foo
case Bar(n) =>
case Baz(a, b) =>
denotationAlwaysEven(a);
denotationAlwaysEven(b);
}
```
""",
"""
A lemma that proves that the denotation of a Foo is always even.
```dafny
lemma denotationAlwaysEven(foo: Foo)
ensures denotation(foo) % 2 == 0
{
"""]),
500,
None,
5,
15,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
['Dafny'],
None,
None
)
problem_pattern_match_train_dafny2 = (("""In Dafny, we have the following ADT:
```dafny
datatype Foo = Bar(n: nat) | Baz(x: Foo)
```
Take the denotation of a Foo to be 2 times n for a Bar, and the denotations of its Foo for a Baz.
```dafny
function denotation(foo: Foo): nat
{
""", [
"""
```dafny
lemma ex1()
ensures denotation(Baz(Bar(2))) == 4
{}
lemma denotationAlwaysEven(foo: Foo)
ensures denotation(foo) % 2 == 0
{
match foo
case Bar(n) =>
case Baz(x) =>
denotationAlwaysEven(x);
}
```
""",
"""
A lemma that proves that the denotation of a Foo is always even.
```dafny
lemma denotationAlwaysEven(foo: Foo)
ensures denotation(foo) % 2 == 0
{
"""]),
500,
None,
5,
15,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
['Dafny'],
None,
None
)
problem_opt0_opt_dafny_sanity_check = (("""### Spec: In Dafny, write an ADT for arithmetic expressions comprising constants, variables and binary addition. Then write an optimizer `optimize` that removes all additions by 0.
### Hint: In the addition case, the `optimize` function should recursively optimize the sub-expressions and then match on the optimized sub-expressions.
"""+hint_match_dafny+"""
```dafny
datatype Expr = Const(i: int) | Var(x: string) | Add(e1: Expr, e2: Expr)
function optimize(e: Expr): Expr
{
""", ["""
```dafny
predicate optimal(e: Expr) {
match e
case Add(Const(0), _) => false
case Add(_, Const(0)) => false
case Add(e1, e2) => optimal(e1) && optimal(e2)
case _ => true
}
lemma OptimizerOptimal(e: Expr)
ensures optimal(optimize(e))
{
match e
case Add(e1, e2) =>
OptimizerOptimal(e1);
OptimizerOptimal(e2);
case _ =>
}
```
"""]),
500,
None,
5,
15,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
['Dafny'],
None,
None
)
problem_opt0_dafny_sanity_check = (("""In Dafny, an ADT for arithmetic expressions comprising constants, variables and binary addition.
```dafny
datatype Expr = Const(i: int) | Var(x: string) | Add(e1: Expr, e2: Expr)
```
An evaluator `eval` taking an expression and an environment (a function that takes a variable name and returns a number) and returning the number resulting from evaluation.
```dafny
function eval(e: Expr, env: string -> int): int
{
"""
, ["",
"""
An optimizer taking an expression and returning an expression with all additions by 0 removed.
In the addition case, the `optimize` function first recursively optimize the sub-expressions, then matches against the the optimized sub-expressions to check for constants 0.
```dafny
function optimize(e: Expr): Expr
{
""", """
```dafny
predicate optimal(e: Expr) {
match e
case Add(Const(0), _) => false
case Add(_, Const(0)) => false
case Add(e1, e2) => optimal(e1) && optimal(e2)
case _ => true
}
lemma OptimizerOptimal(e: Expr)
ensures optimal(optimize(e))
{
match e
case Add(e1, e2) =>
OptimizerOptimal(e1);
OptimizerOptimal(e2);
case _ =>
}
```
""", """
A lemma proving that the optimizer preserves the semantics.
By structural induction on e.
Pattern match against e, and simply call the lemma recursively on the sub-expressions.
```dafny
lemma OptimizerPreservesSemantics(e: Expr, env: string -> int)
ensures eval(optimize(e), env) == eval(e, env)
{
"""
]),
500,
None,
5,
15,
NO_CHECK_PROOF, NO_CHECK_CHEAT,
['Dafny'],
None,
None
)
problem_opt0_opt = (f"""### Spec: In {LANG}, write an ADT for arithmetic expressions comprising constants, variables and binary addition. Then write a predicate `optimal` that holds on an expression if it has no additions by 0. Then write an optimizer `optimize` that removes all additions by 0. Then write a lemma `OptimizerOptimal` that ensures `optimal(optimize(e))` for all expressions `e`.
{'''### Hint: This is the definiton of the `optimal` predicate:
predicate optimal(e: Expr) {
match e
case Add(Const(0), _) => false
case Add(_, Const(0)) => false
case Add(e1, e2) => optimal(e1) && optimal(e2)
case _ => true
}
### Hint: Don't use the same structure for `optimize` as for `optimal`. Instead, follow the next hint.
''' if LANG=='Dafny' else ''
}### Hint: In the addition case, the `optimize` function should recursively optimize the sub-expressions and then match on the optimized sub-expressions.
{'''### Hint: Do NOT use `requires` anywhere.
''' if LANG=='Dafny' else ''
}{'''### Hint: Write the lemma as
lemma OptimizerOptimal(e: Expr)
ensures optimal(optimize(e))
''' if LANG=='Dafny' else ''
}{hint_match_dafny
}{'''### Hint: For the proof, just do a simple pattern match (match not if) and call the lemma recursively without adding asserts.
''' if LANG=='Dafny' else ''
}{'''### Hint: You can import the `string` datatype with the line `Require Import Coq.Strings.String.`
### Hint: Use Fixpoint instead of Definition for recursive functions.
### Hint: If you do induction on `e` with sub-expressions `e1` and `e2`, the two inductive hypotheses are called `IHe1` and `IHe2`.
''' if LANG=='Coq' else ''
}""",
1000,
None,
22,
40,
CHECK_PROOF, CHECK_CHEAT,# if LANG != 'Dafny' else (lambda v: CHECK_CHEAT(v) or "requires" not in v or "==>" not in v),
ALL_LANGS,
None,
None
)
problem_opt0_opt_dafny_check = (f"""### Spec: In {LANG}, write an ADT `Expr` for arithmetic expressions comprising constants, variables and binary addition. Then write a predicate `optimal` that holds on an expression if it has no additions by 0. Then write an optimizer `optimize` that removes all additions by 0. Then write a lemma `OptimizerOptimal` that ensures `optimal(optimize(e))` for all expressions `e`.
{'''### Hint: This is the definiton of the `optimal` predicate:
predicate optimal(e: Expr) {
match e
case Add(Const(0), _) => false
case Add(_, Const(0)) => false
case Add(e1, e2) => optimal(e1) && optimal(e2)
case _ => true
}
### Hint: Don't use the same structure for `optimize` as for `optimal`. Instead, follow the next hint.
''' if LANG=='Dafny' else ''
}### Hint: In the addition case, the `optimize` function should recursively optimize the sub-expressions and then match on the optimized sub-expressions.
{'''### Hint: Do NOT use `requires` anywhere.
''' if LANG=='Dafny' else ''
}{'''### Hint: Write the lemma as
lemma OptimizerOptimal(e: Expr)
ensures optimal(optimize(e))
''' if LANG=='Dafny' else ''
}{hint_match_dafny
}{'''### Hint: For the proof, just do a simple pattern match (match not if) and call the lemma recursively without adding asserts.
''' if LANG=='Dafny' else ''
}{'''### Hint: You can import the `string` datatype with the line `Require Import Coq.Strings.String.`
### Hint: Use Fixpoint instead of Definition for recursive functions.
### Hint: If you do induction on `e` with sub-expressions `e1` and `e2`, the two inductive hypotheses are called `IHe1` and `IHe2`.
''' if LANG=='Coq' else ''
}""",
1000,
None,
22,
40,
CHECK_PROOF, CHECK_CHEAT,# if LANG != 'Dafny' else (lambda v: CHECK_CHEAT(v) or "requires" not in v or "==>" not in v),
["Dafny"],
"lemma CHECK_OptimizerOptimal(e: Expr) ensures optimal(optimize(e)) { OptimizerOptimal(e); }",
None
)
problem_sorting_dafny_check = (f"""### Spec: In {LANG}, write a function `sort` that takes a sequence of integers `seq<int>` and returns a sorted sequence with the same elements. Then write a lemma `SortSorted` that ensures `sorted(sort(s))` for all sequences of integers `s`.
```dafny
predicate sorted(s: seq<int>)
{{
forall i,j :: 0 <= i < j < |s| ==> s[i] <= s[j]
}}
""",
1000,
None,
22,
40,
CHECK_PROOF, CHECK_CHEAT,
["Dafny"],
"lemma CHECK_SortSorted(s: seq<int>) ensures sorted(sort(s)) { SortSorted(s); }",
None
)
problem_insertion_sort_dafny_check = (f"""### Spec: In {LANG},
(1) Write a recursive function `insert` that takes an `int` element and a sorted `seq<int>` and returns a `seq<int>` like the given list with additionally the given element inserted in its sorted place.
(2) Using the function `insert`, implement `sort`, a function that takes a `seq<int>` and sorts it.
(3) Prove the lemma `insert_sorted` that states that given `sorted(s)` then `sorted(insert(a,s))` for all `int` element `a` and all `seq<int>` s.
(4) Prove the lemma `sort_sorted` that states that `sorted(sort(s))` for all `seq<int>` s.
```dafny
predicate sorted(s: seq<int>)
{{
forall i,j :: 0 <= i < j < |s| ==> s[i] <= s[j]
}}
""",
1000,
None,
22,
40,
CHECK_PROOF, CHECK_CHEAT,
["Dafny"],
"lemma CHECK_insert_sorted(a: int, s: seq<int>) requires sorted(s) ensures sorted(insert(a, s)) { insert_sorted(a, s); }\n" +
"lemma CHECK_sort_sorted(s: seq<int>) ensures sorted(sort(s)) { sort_sorted(s); }",
None
)
problem_insertion_sort_dafny_full_spec_check = (f"""### Spec: In {LANG},
(1) Write a recursive function `insert` that takes an `int` element and a sorted `seq<int>` and returns a `seq<int>` like the given list with additionally the given element inserted in its sorted place.
(2) Prove the lemma `element_inserted` that states that `a in insert(a, s)` for all `int` element `a` and all `seq<int>` s.
(3) Prove the lemma `insert_increases_size` that states that `|insert(a, s)| == |s| + 1` for all `int` element `a` and all `seq<int>` s.
(4) Prove the lemma `insert_sorted` that states that given `sorted(s)` then `sorted(insert(a ,s))` for all `int` element `a` and all `seq<int>` s.
(5) Using the function `insert`, implement `sort`, a function that takes a `seq<int>` and sorts it.
(6) Prove the lemma `sort_sorted` that states that `sorted(sort(s))` for all `seq<int>` s. Use the lemma `insert_sorted` in the recursive case.
(7) Prove the lemma `sort_permutation` that states that `permutation(sort(s), s)` for all `seq<int>` s.
```dafny
predicate sorted(s: seq<int>)
{{
forall i,j :: 0 <= i < j < |s| ==> s[i] <= s[j]
}}
predicate permutation(s: seq<int>, t: seq<int>) {{
multiset(s) == multiset(t)
}}
""",
1000,
None,
22,