| 1 |
From Coq Require Import List. |
| 2 |
From Coq Require Import Ascii. |
| 3 |
From Coq Require Import String. |
| 4 |
Require Crypto.Util.Strings.String. |
| 5 |
Require Import Crypto.CLI. |
| 6 |
Require Import Crypto.Util.Notations. |
| 7 |
Import ListNotations. Local Open Scope string_scope. |
| 8 |
|
| 9 |
Inductive trace : Set := |
| 10 |
| finished |
| 11 |
| err (msg : list string) |
| 12 |
| read_stdin (k : list string -> trace) |
| 13 |
| write_stdout (msg : list string) (k : trace) |
| 14 |
| write_stderr (msg : list string) (k : trace) |
| 15 |
| read_file (name : string) (k : list string -> trace) |
| 16 |
| write_file (name : string) (contents : list string) (k : trace) |
| 17 |
. |
| 18 |
|
| 19 |
Definition IODriverTrace : ForExtraction.IODriverAPI trace |
| 20 |
:= {| ForExtraction.error := err |
| 21 |
; ForExtraction.ret := fun 'tt => finished |
| 22 |
; ForExtraction.with_read_stdin := read_stdin |
| 23 |
; ForExtraction.write_stdout_then msg k := write_stdout msg (k tt) |
| 24 |
; ForExtraction.write_stderr_then msg k := write_stderr msg (k tt) |
| 25 |
; ForExtraction.with_read_file := read_file |
| 26 |
; ForExtraction.write_file_then name contents k := write_file name contents (k tt) |
| 27 |
|}. |
| 28 |
|
| 29 |
Fixpoint fuse_stdout_stderr (ls : list (list string + list string)) : list string |
| 30 |
:= match ls with |
| 31 |
| [] => [] |
| 32 |
| inl msg :: ls | inr msg :: ls => msg ++ fuse_stdout_stderr ls |
| 33 |
end%list. |
| 34 |
|
| 35 |
Fixpoint split_stdout_stderr (ls : list (list string + list string)) : list string * list string |
| 36 |
:= match ls with |
| 37 |
| [] => ([], []) |
| 38 |
| inl stdout :: ls |
| 39 |
=> let '(stdouts, stderrs) := split_stdout_stderr ls in |
| 40 |
(stdout ++ stdouts, stderrs) |
| 41 |
| inr stderr :: ls |
| 42 |
=> let '(stdouts, stderrs) := split_stdout_stderr ls in |
| 43 |
(stdouts, stderr ++ stderrs) |
| 44 |
end%list. |
| 45 |
|
| 46 |
Fixpoint eval' {A} (t : trace) (stdin : list (list string)) (files : list (string * list string)) (fuse_stdout_stderr : list (list string + list string) -> A) (stdout_stderr_r_acc : list (list string + list string)) (new_files : list (string * list string)) : option (list string) (* return *) * A (* stdout / stderr *) * list (string * list string) (* new files *) |
| 47 |
:= match t with |
| 48 |
| finished |
| 49 |
=> (None, fuse_stdout_stderr (List.rev' stdout_stderr_r_acc), new_files) |
| 50 |
| err msg |
| 51 |
=> (Some msg, fuse_stdout_stderr (List.rev' stdout_stderr_r_acc), new_files) |
| 52 |
| read_stdin k |
| 53 |
=> let '(inp, stdin) := match stdin with |
| 54 |
| inp :: stdin => (inp, stdin) |
| 55 |
| [] => ([], []) |
| 56 |
end in |
| 57 |
eval' (k inp) stdin files fuse_stdout_stderr stdout_stderr_r_acc new_files |
| 58 |
| write_stdout msg k |
| 59 |
=> eval' k stdin files fuse_stdout_stderr (inl msg :: stdout_stderr_r_acc) new_files |
| 60 |
| write_stderr msg k |
| 61 |
=> eval' k stdin files fuse_stdout_stderr (inr msg :: stdout_stderr_r_acc) new_files |
| 62 |
| read_file name k |
| 63 |
=> let contents := match List.find (fun '(fname, contents) => (fname =? name)%string) (new_files ++ files) with |
| 64 |
| Some (_, contents) => contents |
| 65 |
| None => [] |
| 66 |
end in |
| 67 |
eval' (k contents) stdin files fuse_stdout_stderr stdout_stderr_r_acc new_files |
| 68 |
| write_file name contents k |
| 69 |
=> eval' k stdin files fuse_stdout_stderr stdout_stderr_r_acc ((name, contents) :: new_files) |
| 70 |
end. |
| 71 |
Definition eval_trace {A} (t : trace) (stdin : list (list string)) (files : list (string * list string)) (fuse_stdout_stderr : list (list string + list string) -> A) : option (list string) (* return *) * A (* stdout / stderr *) * list (string * list string) (* new files *) |
| 72 |
:= eval' t stdin files fuse_stdout_stderr [] []. |