Debian Codegraph @

src/StandaloneJsOfOCamlMain.v

raw
1 From Coq Require Export Extraction.
2 From Coq Require Export ExtrOcamlBasic.
3 From Coq Require Export ExtrOcamlString.
4 From Coq Require Import List.
5 From Coq Require Import Ascii.
6 From Coq Require Import String.
7 Require Crypto.Util.Strings.String.
8 Require Import Crypto.CLI.
9 Require Import Crypto.StandaloneMonadicUtils.
10 Require Import Crypto.Util.Notations.
11 Import ListNotations. Local Open Scope string_scope.
12
13 Global Set Warnings Append "-extraction-opaque-accessed".
14 Extraction Language OCaml.
15 Global Unset Extraction Optimize.
16
17 (** Work around COQBUG(https://github.com/coq/coq/issues/4875) / COQBUG(https://github.com/coq/coq/issues/7954) / COQBUG(https://github.com/coq/coq/issues/7954) / https://discuss.ocaml.org/t/why-wont-ocaml-specialize-weak-type-variables-in-dead-code/7776 *)
18 Extraction Inline Show.ShowLevel_of_Show.
19
20 Inductive int : Set := int_O | int_S (x : int).
21
22 (** We pull a hack to get coqchk to not report these as axioms; for
23 this, all we care about is that there exists a model. *)
24 Module Type OCamlPrimitivesT.
25 Axiom OCaml_string : Set.
26 Notation string := OCaml_string.
27 Axiom string_length : string -> int.
28 Axiom string_get : string -> int -> Ascii.ascii.
29 Axiom string_init : int -> (int -> Ascii.ascii) -> string.
30 (*Axiom raise_Failure : string -> unit.*)
31 (*Axiom exn : Set.
32 Axiom Failure : string -> exn.*)
33 Axiom OCaml_array : Set -> Set.
34 Notation array := OCaml_array.
35 Axiom Array_to_list : forall {a : Set}, array a -> list a.
36 Axiom Array_of_list : forall {a : Set}, list a -> array a.
37 End OCamlPrimitivesT.
38
39 Module Export OCamlPrimitives : OCamlPrimitivesT.
40 Definition OCaml_string : Set := unit.
41 Notation string := OCaml_string.
42 Definition string_length : string -> int := fun _ => int_O.
43 Definition string_get : string -> int -> Ascii.ascii := fun _ _ => "000"%char.
44 Definition string_init : int -> (int -> Ascii.ascii) -> string := fun _ _ => tt.
45 (*Definition raise_Failure : string -> unit := fun _ => tt.*)
46 (*Definition exn : Set := unit.
47 Definition Failure : string -> exn := fun _ => tt.*)
48 Definition OCaml_array (A : Set) := list A.
49 Notation array := OCaml_array.
50 Definition Array_to_list {a : Set} : array a -> list a := fun x => x.
51 Definition Array_of_list {a : Set} : list a -> array a := fun x => x.
52 End OCamlPrimitives.
53
54 Module Type Js_of_ocamlPrimitivesT.
55 Axiom Js_t : Set -> Set.
56 Axiom js_string : Set.
57 Axiom js_array : Set -> Set.
58 Axiom js_to_array : forall {a}, Js_t (js_array a) -> array a.
59 Axiom js_to_string : Js_t js_string -> string.
60 Axiom js_of_array : forall {a}, array a -> Js_t (js_array a).
61 Axiom js_of_string : string -> Js_t js_string.
62 Axiom js_to_bool : Js_t bool -> bool.
63 Axiom js_of_bool : bool -> Js_t bool.
64 Axiom Js_Unsafe_any : Set.
65 Axiom Js_Unsafe_inject : forall {a : Set}, a -> Js_Unsafe_any.
66 Axiom Js_export : forall {a : Set}, string -> a -> unit.
67 Axiom js_callback : Set -> Set.
68 Axiom js_wrap_callback : forall {a b : Set}, (a -> b) -> js_callback (a -> b).
69 End Js_of_ocamlPrimitivesT.
70
71 Module Import Js_of_ocamlPrimitives : Js_of_ocamlPrimitivesT.
72 Definition Js_t : Set -> Set := fun t => t.
73 Definition js_string : Set := string.
74 Definition js_array : Set -> Set := array.
75 Definition js_to_array : forall {a}, Js_t (js_array a) -> array a := fun _ x => x.
76 Definition js_to_string : Js_t js_string -> string := fun x => x.
77 Definition js_of_array : forall {a}, array a -> Js_t (js_array a) := fun _ x => x.
78 Definition js_of_string : string -> Js_t js_string := fun x => x.
79 Definition js_to_bool : Js_t bool -> bool := fun x => x.
80 Definition js_of_bool : bool -> Js_t bool := fun x => x.
81 Definition Js_Unsafe_any : Set := unit.
82 Definition Js_Unsafe_inject : forall {a : Set}, a -> Js_Unsafe_any := fun _ _ => tt.
83 Definition Js_export : forall {a : Set}, string -> a -> unit := fun _ _ _ => tt.
84 Definition js_callback : Set -> Set := fun a => a.
85 Definition js_wrap_callback : forall {a b : Set}, (a -> b) -> js_callback (a -> b) := fun _ _ f => f.
86 End Js_of_ocamlPrimitives.
87
88 Extract Inductive int
89 => "Int.t" [ "0" "(fun n -> n+1)" ]
90 "(fun fO fS n -> if n=0 then fO () else fS (n-1))".
91 (* We cannot inline these constants due to COQBUG(https://github.com/coq/coq/issues/16169) *)
92 Extract (*Inlined*) Constant string => "string".
93 Extract (*Inlined*) Constant string_length => "String.length".
94 Extract (*Inlined*) Constant string_get => "String.get".
95 Extract (*Inlined*) Constant string_init => "String.init".
96 Extract (*Inlined*) Constant array "'a" => "'a array".
97 Extract (*Inlined*) Constant Array_to_list => "Array.to_list".
98 Extract (*Inlined*) Constant Array_of_list => "Array.of_list".
99
100 Extract (*Inlined*) Constant Js_t "'a" => "'a Js_of_ocaml.Js.t".
101 Extract (*Inlined*) Constant js_string => "Js_of_ocaml.Js.js_string".
102 Extract (*Inlined*) Constant js_array "'a" => "'a Js_of_ocaml.Js.js_array".
103 Extract (*Inlined*) Constant js_to_array => "Js_of_ocaml.Js.to_array".
104 Extract (*Inlined*) Constant js_of_array => "Js_of_ocaml.Js.array".
105 Extract (*Inlined*) Constant js_to_string => "Js_of_ocaml.Js.to_string".
106 Extract (*Inlined*) Constant js_of_string => "Js_of_ocaml.Js.string".
107 Extract (*Inlined*) Constant js_to_bool => "Js_of_ocaml.Js.to_bool".
108 Extract (*Inlined*) Constant js_of_bool => "Js_of_ocaml.Js.bool".
109 Extract (*Inlined*) Constant Js_Unsafe_any => "Js_of_ocaml.Js.Unsafe.any".
110 Extract (*Inlined*) Constant Js_Unsafe_inject => "Js_of_ocaml.Js.Unsafe.inject".
111 Extract (*Inlined*) Constant Js_export => "Js_of_ocaml.Js.export".
112 Extract (*Inlined*) Constant js_callback "'a" => "'a Js_of_ocaml.Js.callback".
113 Extract (*Inlined*) Constant js_wrap_callback => "Js_of_ocaml.Js.wrap_callback".
114
115 Fixpoint nat_of_int (x : int) : nat
116 := match x with
117 | int_O => O
118 | int_S x' => S (nat_of_int x')
119 end.
120 Fixpoint int_of_nat (x : nat) : int
121 := match x with
122 | O => int_O
123 | S x' => int_S (int_of_nat x')
124 end.
125 Global Set Warnings Append "-ambiguous-paths".
126 Coercion nat_of_int : int >-> nat.
127 Coercion int_of_nat : nat >-> int.
128
129 Definition string_of_Coq_string (s : String.string) : string
130 := let s := String.list_ascii_of_string s in
131 string_init
132 (List.length s)
133 (fun n => List.nth n s "?"%char).
134
135 Definition string_to_Coq_string (s : string) : String.string
136 := String.string_of_list_ascii
137 (List.map (fun n:nat => string_get s n) (List.seq 0 (string_length s))).
138
139 Definition js_to_list_map {A : Set} {B} (f : A -> B) (a : Js_t (js_array A)) : list B
140 := List.map f (Array_to_list (js_to_array a)).
141
142 Definition js_to_Coq_string (s : Js_t js_string) : String.string
143 := string_to_Coq_string (js_to_string s).
144
145 Definition valid_synthesis_kinds_list : list string
146 := List.map string_of_Coq_string (List.map fst ForExtraction.parse_SynthesisKind_list).
147
148 (** js_of_ocaml doesn't support product types very well (or at least I wasn't able to find them), so we kludge together a unified input of list [(list (list string))]
149 We assume input of the form [[argv]; stdin; (filename1 :: file1_contents); (filename2 :: file2_contents); ...] *)
150 Fixpoint split_files {A} (l : list (list A)) : (list (A * list A)) + (nat -> list String.string) :=
151 match l with
152 | [] => inl []
153 | [] :: _ => inr (fun n => ["Anomaly: file " ++ Show.show n ++ " has no name"]%string)
154 | (name :: contents) :: ls =>
155 match split_files ls with
156 | inl files => inl ((name, contents) :: files)
157 | inr errs_fn => inr (fun n => errs_fn (S n))
158 end
159 end%list.
160
161 Definition split_unified_input {A} {show_A : Show.Show A} (l : list (list (list A))) : (list A * list (list A) * list (A * list A)) + list String.string :=
162 match l with
163 | [ [argv] ; stdin ; files ] =>
164 match split_files files with
165 | inl files => inl (argv, stdin, files)
166 | inr errs_fn => inr (errs_fn O)
167 end
168 | [argv ; _stdin ; _files] => inr ["Anomaly: argv should be a singleton list of strings, not " ++ Show.show argv ++ " (" ++ Show.show l ++ ")"]%string
169 | [_argv ; _stdin ] => inr ["Anomaly: missing files, got only " ++ Show.show l]%string
170 | [argv] => inr ["Anomaly: missing stdin, got only " ++ Show.show argv]%string
171 | [] => inr ["Anomaly: empty input"]%string
172 | _argv :: _stdin :: _files :: extra => inr ["Anomaly: got more than three arguments: " ++ Show.show extra ++ " (" ++ Show.show l ++ ")"]%string
173 end%list.
174
175 Global Existing Instance IODriverTrace.
176
177 Definition main_gen
178 (PipelineMain : forall (A := _)
179 (argv : list String.string),
180 A)
181 : unit
182 := let js_of_Coq_string s := js_of_string (string_of_Coq_string s) in
183 let js_of_list_string ls := js_of_array (Array_of_list (List.map js_of_Coq_string ls)) in
184 let synthesize : js_callback (Js_t (js_array (Js_t (js_array (Js_t (js_array (Js_t js_string)))))) -> Js_t (js_array Js_Unsafe_any))
185 := js_wrap_callback
186 (fun argv_stdin_files =>
187 let argv_stdin_files := js_to_list_map (js_to_list_map (js_to_list_map js_to_Coq_string)) argv_stdin_files in
188 let '(result, (stdout, stderr), new_files) :=
189 match split_unified_input argv_stdin_files with
190 | inl (argv, stdin, files) =>
191 eval_trace (PipelineMain argv) stdin files split_stdout_stderr
192 | inr errs => (None, ([], errs), [])
193 end in
194 js_of_array
195 (Array_of_list
196 [Js_Unsafe_inject (js_of_bool (Option.is_None result))
197 ; Js_Unsafe_inject (js_of_list_string match result with Some msg => msg | None => [] end)
198 ; Js_Unsafe_inject (js_of_list_string stdout)
199 ; Js_Unsafe_inject (js_of_list_string stderr)
200 ; Js_Unsafe_inject
201 (js_of_array
202 (Array_of_list
203 (List.map
204 (fun '(name, contents)
205 => js_of_array
206 (Array_of_list
207 [Js_Unsafe_inject (js_of_Coq_string name)
208 ; Js_Unsafe_inject (js_of_list_string contents)]))
209 new_files)))
210 ]))
211 in
212 let valid_synthesis_kinds : Js_t (js_array (Js_t js_string))
213 := js_of_list_string (List.map fst ForExtraction.parse_SynthesisKind_list) in
214 let 'tt := Js_export (string_of_Coq_string "synthesize") synthesize in
215 let 'tt := Js_export (string_of_Coq_string "valid_synthesis_kinds") valid_synthesis_kinds in
216 tt.
217
218 Local Existing Instance ForExtraction.default_supported_languages.
219
220 Module FiatCrypto.
221 Definition main : unit
222 := main_gen ForExtraction.FiatCrypto.PipelineMain.
223 End FiatCrypto.