-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.ml
More file actions
935 lines (827 loc) · 25.6 KB
/
main.ml
File metadata and controls
935 lines (827 loc) · 25.6 KB
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
open Smyth
(* Parameters *)
type show_type =
| ShowTop1
| ShowTop1R
| ShowTop3
let show_type : show_type ref =
ref ShowTop1
let suite_test_n : int ref =
ref 10
type test_criterion =
| TestTop1
| TestTop1R
let test_criterion : test_criterion ref =
ref TestTop1
type output_mode =
| OutputString
| OutputJson
let output_mode : output_mode ref =
ref OutputString
(* Information *)
let title = "
-_-/ , ,,
(_ / || ||
(_ --_ \\\\/\\\\/\\\\ '\\\\/\\\\ =||= ||/\\\\
--_ ) || || || || ;' || || ||
_/ )) || || || ||/ || || ||
(_-_- \\\\ \\\\ \\\\ |/ \\\\, \\\\ |/
( _/
-_-
"
let name =
"Smyth"
let description =
"Programming-by-example in a typed functional language with sketches."
(* Helpers *)
let divider =
"--------------------------------------------------------------------------------"
let arg_format : string -> string =
fun s ->
"<" ^ s ^ ">"
let summarize : Endpoint.test_result list -> Endpoint.test_result option =
fun test_results ->
let open Option2.Syntax in
let open Endpoint in
let* head =
List2.hd_opt test_results
in
let* _ =
Option2.guard @@
List.for_all
( fun (tr : test_result) ->
{ tr with time_taken = 0.0 } = { head with time_taken = 0.0 }
)
test_results
in
let+ average_time_taken =
test_results
|> List.map (fun (tr : test_result) -> tr.time_taken)
|> List2.average
in
{ head with
time_taken = average_time_taken
}
let ratio : int -> int -> float =
fun x y ->
(float_of_int x) /. (float_of_int y)
(* Commands *)
type command =
| Solve
| Test
| SuiteTest
| Fuzz
| PolyFuzz
| AssertionExport
| GenerateSpec
| GeneratePolySpec
let commands : command list =
if Compilation2.is_js then
[ Solve
]
else
[ Solve
; Test
; SuiteTest
; Fuzz
; PolyFuzz
; AssertionExport
; GenerateSpec
; GeneratePolySpec
]
let command_name : command -> string =
function
| Solve -> "forge"
| Test -> "test"
| SuiteTest -> "suite-test"
| Fuzz -> "fuzz"
| PolyFuzz -> "poly-fuzz"
| AssertionExport -> "export-assertions"
| GenerateSpec -> "generate-spec"
| GeneratePolySpec -> "generate-poly-spec"
let command_from_name : string -> command option =
fun name ->
List.find_opt
( fun cmd ->
String.equal (command_name cmd) name
)
commands
let command_description : command -> string =
function
| Solve ->
"Complete a program sketch"
| Test ->
"Test a solution against a specification"
| SuiteTest ->
"Test multiple solutions against specifications"
| Fuzz ->
"Stress-test a program sketch with examples fuzzed from a built-in "
^ "function"
| PolyFuzz ->
"Stress-test a polymorphic program sketch with examples fuzzed from a "
^ "built-in function"
| AssertionExport ->
"Export a set of assertions to Python code"
| GenerateSpec ->
"Generate an example specification of a built-in function"
| GeneratePolySpec ->
"Generate a polymorphic example specification of a built-in function"
let command_arguments : command -> (string * string) list =
function
| Solve ->
[ ( "sketch"
, "The path to the sketch to be completed"
)
]
| Test ->
[ ( "specification"
, "The path to the specification"
)
; ( "sketch"
, "The path to the sketch to be tested WITHOUT any examples"
)
; ( "examples"
, "The path to the examples for " ^ arg_format "sketch"
)
]
| SuiteTest ->
[ ( "suite"
, "The path to the suite to be tested"
)
]
| Fuzz
| PolyFuzz ->
[ ( "trial-count"
, "The number of trials to run for each size of example set"
)
; ( "built-in-func"
, "The built-in function to use as a reference"
)
; ( "specification"
, "The path to the specification"
)
; ( "sketch"
, "The path to the sketch to be fuzzed WITHOUT any examples"
)
]
| AssertionExport ->
[ ( "specification"
, "The path to the specification to be exported"
)
; ( "examples"
, "The path to the examples to be exported"
)
]
| GenerateSpec
| GeneratePolySpec ->
[ ( "built-in-func"
, "The built-in function to generate an example specification for"
)
]
(* Options *)
type prog_option =
| Debug
| MaxTotalTime
| Replications
| TestAlert
| Show
| OutputMode
let prog_options : prog_option list =
[ Debug; MaxTotalTime; Replications; TestAlert; Show; OutputMode ]
let prog_option_name : prog_option -> string =
function
| Debug -> "debug"
| MaxTotalTime -> "timeout"
| Replications -> "replications"
| TestAlert -> "test-alert"
| Show -> "show"
| OutputMode -> "format"
let prog_option_from_name : string -> prog_option option =
fun name ->
List.find_opt
( fun p ->
String.equal (prog_option_name p) name
)
prog_options
let prog_option_info : prog_option -> string * string * string list =
function
| Debug ->
( "Include debug logging"
, "off"
, ["off"; "on"]
)
| MaxTotalTime ->
( "Set maximum total time allowed per synthesis request"
, Float2.to_string !Params.max_total_time
, ["<positive-float>"]
)
| Replications ->
( "Set the number of replications for a suite test"
, string_of_int !suite_test_n
, ["<positive-integer>"]
)
| TestAlert ->
( "Set the criterion to alert a failure for suite tests"
, "top1"
, ["<top1|top1r>"]
)
| Show ->
( "Set the display method for forge results"
, "top1"
, ["<top1|top1r|top3>"]
)
| OutputMode ->
( "Set the output format for forge results"
, "string"
, ["<string|json>"]
)
let prog_option_action : prog_option -> string -> bool =
fun prog_option value ->
match prog_option with
| Debug ->
begin match value with
| "off" -> (Params.debug_mode := false; true)
| "on" -> (Params.debug_mode := true; true)
| _ -> false
end
| MaxTotalTime ->
begin match float_of_string_opt value with
| Some timeout ->
if timeout > 0.0 then
(Params.max_total_time := timeout; true)
else
false
| None -> false
end
| Replications ->
begin match int_of_string_opt value with
| Some replications ->
if replications > 0 then
(suite_test_n := replications; true)
else
false
| None -> false
end
| TestAlert ->
begin match value with
| "top1" -> (test_criterion := TestTop1; true)
| "top1r" -> (test_criterion := TestTop1R; true)
| _ -> false
end
| Show ->
begin match value with
| "top1" -> (show_type := ShowTop1; true)
| "top1r" -> (show_type := ShowTop1R; true)
| "top3" -> (show_type := ShowTop3; true)
| _ -> false
end
| OutputMode ->
begin match value with
| "string" -> (output_mode := OutputString; true)
| "json" -> (output_mode := OutputJson; true)
| _ -> false
end
(* Help *)
let exec_name =
Sys.argv.(0)
let usage_prefix =
"Usage: " ^ exec_name
let option_schema =
"[--option=value]*"
let command_help : command -> string =
fun command ->
let arguments =
command_arguments command
in
usage_prefix ^ " " ^
command_name command ^ " " ^
String.concat " " (List.map (fun (name, _) -> arg_format name) arguments) ^
" " ^ option_schema ^
"\n\nArguments:\n" ^
( arguments
|> List.map (fun (name, desc) -> Printf.sprintf " %-22s%s" name desc)
|> String.concat "\n"
)
let available_commands =
"Available commands:\n\n" ^
( commands
|> List.map
( fun command ->
Printf.sprintf
" %-20s%s"
(command_name command)
(command_description command)
)
|> String.concat "\n"
)
let available_options =
"Available options:\n\n" ^
( prog_options
|> List.map
( fun prog_option ->
let name =
prog_option_name prog_option
in
let (description, default, possibles) =
prog_option_info prog_option
in
let possible_string =
String.concat "|" possibles
in
Printf.sprintf
" %-20s(%s) %s. Default: %s"
name
possible_string
description
default
)
|> String.concat "\n"
)
let javascript_string =
if Compilation2.is_js then
" (JavaScript version)"
else
""
let help =
title ^ "\n" ^
name ^ " v" ^ Params.version ^ javascript_string ^ "\n" ^
description ^ "\n\n" ^
usage_prefix ^ " <command> <args> " ^ option_schema ^ "\n\n" ^
available_commands ^
"\n\n" ^
available_options ^
"\n\nFor more specific information, run:\n\n " ^
exec_name ^ " <command> --help"
let command_not_found attempt =
"Could not find command '" ^ attempt ^ "'.\n\n" ^
available_commands
(* Option handling *)
let handle_prog_option : string -> bool =
fun arg ->
let len =
String.length arg
in
if len < 2 || arg.[0] <> '-' || arg.[1] <> '-' then
false
else
match
String.sub arg 2 (len - 2)
|> String.split_on_char '='
with
| [name; value] ->
begin match prog_option_from_name name with
| Some prog_option ->
prog_option_action prog_option value
| None ->
false
end
| _ ->
false
let rec handle_prog_options : int -> string array -> unit =
fun n prog_options ->
if n >= Array.length prog_options then
()
else
begin
if handle_prog_option prog_options.(n) then
handle_prog_options (n + 1) prog_options
else
begin
prerr_endline help;
exit 1
end
end
(* Forge Output *)
let newline_regexp =
Str.regexp "\n"
let encode_json_string : string -> string =
Str.global_replace newline_regexp "\\n"
let show_hf : (Lang.hole_name * Lang.exp) list -> string =
fun hole_filling ->
let sorted_hole_filling =
hole_filling
|> List.sort (fun (h1, _) (h2, _) -> Int.compare h1 h2)
in
match !output_mode with
| OutputString ->
sorted_hole_filling
|> List.map
( fun (hole_name, exp) ->
"??"
^ string_of_int hole_name
^ ": \n\n"
^ Pretty.exp exp
)
|> String.concat "\n\n"
| OutputJson ->
let inner =
sorted_hole_filling
|> List.map
( fun (hole_name, exp) ->
"\""
^ string_of_int hole_name
^ "\": \""
^ encode_json_string (Pretty.exp exp)
^ "\""
)
|> String.concat "\n, "
in
"{ " ^ inner ^ "\n}"
let show_hfs : (Lang.hole_name * Lang.exp) list list -> string =
fun hole_fillings ->
match !output_mode with
| OutputString ->
hole_fillings
|> List.mapi
( fun i_ hole_filling ->
String.concat "\n\n"
[ "Solution #"
^ (string_of_int (i_ + 1))
^ " (rank "
^ string_of_int (Rank.rank hole_filling)
^ "):"
; show_hf hole_filling
]
)
|> String.concat ("\n" ^ divider ^ "\n")
| OutputJson ->
let inner =
hole_fillings
|> List.map
( fun hole_filling ->
show_hf hole_filling
)
|> String.concat "\n,\n"
in
"[\n" ^ inner ^ "\n]"
(* Main *)
let solve : sketch:string -> (string, string) result =
fun ~sketch ->
match Endpoint.solve ~sketch with
| Error e ->
Error (Show.error e)
| Ok solve_result ->
let ranked_hole_fillings =
solve_result.Endpoint.hole_fillings
|> Rank.sort
in
begin match !show_type with
| ShowTop1 ->
begin match ranked_hole_fillings with
| top :: _ ->
Ok (show_hf top)
| _ ->
Error "No solutions."
end
| ShowTop1R ->
begin match Rank.first_recursive ranked_hole_fillings with
| Some top_r ->
let prefix =
begin match !output_mode with
| OutputString ->
"Top recursive solution:\n\n"
| OutputJson ->
""
end
in
Ok
( prefix
^ show_hf top_r
)
| _ ->
Error "No recursive solutions."
end
| ShowTop3 ->
if ranked_hole_fillings = [] then
Error "No solutions."
else
Ok
( ranked_hole_fillings
|> List2.take 3
|> show_hfs
)
end
let () =
if Compilation2.is_js then
()
(* begin
let open Js_of_ocaml in
output_mode := OutputJson;
let js_forge js_sketch =
let sketch =
Js.to_string js_sketch
in
let res =
solve ~sketch
|> Result2.unwrap identity identity
in
Js.string res
in
Js.export "Smyth"
( object%js
method forge js_sketch =
js_forge js_sketch
end
)
end *)
else
begin
Random.self_init ();
let argv_length =
Array.length Sys.argv
in
begin
if argv_length < 2 then
begin
print_endline help;
exit 0
end
else
()
end;
let command =
match command_from_name Sys.argv.(1) with
| Some cmd ->
cmd
| None ->
begin
prerr_endline (command_not_found Sys.argv.(1));
exit 1
end
in
begin
if argv_length = 3 && Sys.argv.(2) = "--help" then
begin
print_endline (command_help command);
exit 0
end
else
()
end;
let correct_arg_count =
List.length (command_arguments command)
in
begin
if argv_length - 2 < correct_arg_count then
begin
prerr_endline (command_help command);
exit 1
end
else
()
end;
handle_prog_options (correct_arg_count + 2) Sys.argv;
begin match command with
| Solve ->
begin match
solve
~sketch:(Io2.read_file Sys.argv.(2))
with
| Ok s ->
print_endline s
| Error s ->
prerr_endline s;
exit 1
end
| Test ->
begin match
Endpoint.test
~specification:(Io2.read_file Sys.argv.(2))
~sketch:(Io2.read_file Sys.argv.(3))
~examples:(Io2.read_file Sys.argv.(4))
with
| Error e ->
prerr_endline (Show.error e);
exit 1
| Ok test_result ->
test_result
|> Show.test_result
|> print_endline
end
| SuiteTest ->
let suite_path =
Sys.argv.(2)
in
let benchmark_names =
Io2.visible_files (Io2.path [suite_path ; "sketches"])
in
print_endline ("% Replications = " ^ string_of_int !suite_test_n);
benchmark_names
|> List.iter
( fun name ->
let output =
begin match
Result2.sequence @@
List.init !suite_test_n
( fun _ ->
Endpoint.test
~specification:
( Io2.read_path
[suite_path; "specifications"; name]
)
~sketch:
( Io2.read_path
[suite_path; "sketches"; name]
)
~examples:
( Io2.read_path
[suite_path; "examples"; name]
)
)
with
| Error e ->
"? error (" ^ name ^ "): " ^ Show.error e
| Ok test_results ->
match summarize test_results with
| Some test_result ->
let prefix =
let open Endpoint in
if
( !test_criterion = TestTop1 &&
not test_result.top_success
) ||
( !test_criterion = TestTop1R &&
not test_result.top_recursive_success
)
then
"% ! failure: "
else
""
in
prefix
^ name
^ ","
^ Show.test_result test_result
| None ->
"? inconsistent test: " ^ name
end
in
print_endline output
)
| Fuzz
| PolyFuzz ->
let trial_count =
match int_of_string_opt Sys.argv.(2) with
| Some n when n > 0 ->
n
| _ ->
prerr_endline "Trial count must be a positive integer.\n";
prerr_endline (command_help command);
exit 1
in
let builtin =
Sys.argv.(3)
in
let spec_path =
Sys.argv.(4)
in
let sketch_path =
Sys.argv.(5)
in
let poly =
command = PolyFuzz
in
let benchmark : (Lang.exp * Lang.exp) list list list =
match
List.assoc_opt
builtin
(References.all (Fuzz.experiment_proj ~poly ~n:trial_count))
with
| Some benchmark_thunk ->
benchmark_thunk ()
| None ->
prerr_endline
( "Unknown built-in function '" ^ builtin ^ "'."
);
exit 1
in
let results : (int * int) list =
List.map
( fun trials ->
let (top_successes, top_recursive_successes) =
trials
|> List.map
( fun assertions ->
let open Endpoint in
match
Endpoint.test_assertions
~specification:(Io2.read_path [spec_path])
~sketch:(Io2.read_path [sketch_path])
~assertions
with
| Ok
{ top_success
; top_recursive_success
; _
} ->
(top_success, top_recursive_success)
| Error Endpoint.TimedOut _
| Error Endpoint.NoSolutions ->
(false, false)
| Error e ->
prerr_endline
( "error for '"
^ sketch_path
^ "': "
^ Show.error e
);
exit 1
)
|> List.split
in
( List2.count identity top_successes
, List2.count identity top_recursive_successes
)
)
benchmark
in
let result_string =
builtin
^ "\ntimeout,"
^ Float2.to_string !Params.max_total_time
^ "\ntrial count,"
^ string_of_int trial_count
^ "\n"
^ "example count,top success percent,"
^ "top recursive success percent"
^ "\n"
^ String.concat "\n"
( List.mapi
( fun k (top_successes, top_recursive_successes) ->
( Printf.sprintf "%d,%.4f,%.4f"
k
( ratio
top_successes
trial_count
)
( ratio
top_recursive_successes
trial_count
)
)
)
results
)
in
print_endline result_string
| AssertionExport ->
begin match
Endpoint.assertion_info
~specification:(Io2.read_file Sys.argv.(2))
~assertions:(Io2.read_file Sys.argv.(3))
with
| Error e ->
print_endline
( "? error ("
^ name
^ "): "
^ Show.error e
)
| Ok info ->
let insides =
info
|> List.map
( fun (in_partial, inputs, output) ->
"("
^ String.concat ", "
[ if in_partial then
"True"
else
"False"
; Python_denotation.exp_collection "[" "]"
inputs
; Python_denotation.exp
output
]
^ ")"
)
in
print_endline
( " [ "
^ String.concat "\n , " insides
^ "\n ]"
)
end
| GenerateSpec
| GeneratePolySpec ->
let builtin =
Sys.argv.(2)
in
let poly =
command = GeneratePolySpec
in
begin match
List.assoc_opt
builtin
(References.all (Fuzz.specification_proj ~poly))
with
| Some spec ->
print_endline spec
| None ->
prerr_endline
( "Unknown built-in function '" ^ builtin ^ "'."
);
exit 1
end
end;
end