| 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.Util.Notations. |
| 10 |
Import ListNotations. Local Open Scope string_scope. |
| 11 |
|
| 12 |
Global Set Warnings Append "-extraction-opaque-accessed". |
| 13 |
Extraction Language OCaml. |
| 14 |
Global Unset Extraction Optimize. |
| 15 |
|
| 16 |
(** 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 *) |
| 17 |
Extraction Inline Show.ShowLevel_of_Show. |
| 18 |
|
| 19 |
Inductive int : Set := int_O | int_S (x : int). |
| 20 |
|
| 21 |
(** We pull a hack to get coqchk to not report these as axioms; for |
| 22 |
this, all we care about is that there exists a model. *) |
| 23 |
|
| 24 |
Module Type OCamlPrimitivesT. |
| 25 |
Axiom OCaml_in_channel : Set. |
| 26 |
Notation in_channel := OCaml_in_channel. |
| 27 |
Axiom OCaml_out_channel : Set. |
| 28 |
Notation out_channel := OCaml_out_channel. |
| 29 |
Axiom fprintf_char : out_channel -> Ascii.ascii -> unit. |
| 30 |
Axiom flush : out_channel -> unit. |
| 31 |
Axiom OCaml_stdin : in_channel. |
| 32 |
Notation stdin := OCaml_stdin. |
| 33 |
Axiom OCaml_stdout : out_channel. |
| 34 |
Notation stdout := OCaml_stdout. |
| 35 |
Axiom OCaml_stderr : out_channel. |
| 36 |
Notation stderr := OCaml_stderr. |
| 37 |
Axiom OCaml_string : Set. |
| 38 |
Notation string := OCaml_string. |
| 39 |
Axiom string_length : string -> int. |
| 40 |
Axiom string_get : string -> int -> Ascii.ascii. |
| 41 |
Axiom sys_argv : list string. |
| 42 |
Axiom string_init : int -> (int -> Ascii.ascii) -> string. |
| 43 |
Axiom raise_Failure : string -> unit. |
| 44 |
Axiom OCaml_open_in : string -> in_channel. |
| 45 |
Notation open_in := OCaml_open_in. |
| 46 |
Axiom OCaml_open_out : string -> out_channel. |
| 47 |
Notation open_out := OCaml_open_out. |
| 48 |
Axiom OCaml_close_in : in_channel -> unit. |
| 49 |
Notation close_in := OCaml_close_in. |
| 50 |
Axiom OCaml_close_out : out_channel -> unit. |
| 51 |
Notation close_out := OCaml_close_out. |
| 52 |
Axiom read_channel_rev : in_channel -> list string. |
| 53 |
End OCamlPrimitivesT. |
| 54 |
|
| 55 |
Module Export OCamlPrimitives : OCamlPrimitivesT. |
| 56 |
Definition OCaml_in_channel : Set := unit. |
| 57 |
Notation in_channel := OCaml_in_channel. |
| 58 |
Definition OCaml_out_channel : Set := unit. |
| 59 |
Notation out_channel := OCaml_out_channel. |
| 60 |
Definition fprintf_char : out_channel -> Ascii.ascii -> unit := fun _ _ => tt. |
| 61 |
Definition flush : out_channel -> unit := fun _ => tt. |
| 62 |
Definition OCaml_stdin : in_channel := tt. |
| 63 |
Definition OCaml_stdout : out_channel := tt. |
| 64 |
Definition OCaml_stderr : out_channel := tt. |
| 65 |
Definition OCaml_string : Set := unit. |
| 66 |
Notation string := OCaml_string. |
| 67 |
Definition string_length : string -> int := fun _ => int_O. |
| 68 |
Definition string_get : string -> int -> Ascii.ascii := fun _ _ => "000"%char. |
| 69 |
Definition sys_argv : list string := nil. |
| 70 |
Definition string_init : int -> (int -> Ascii.ascii) -> string := fun _ _ => tt. |
| 71 |
Definition raise_Failure : string -> unit := fun _ => tt. |
| 72 |
Definition OCaml_open_in : string -> in_channel := fun _ => tt. |
| 73 |
Definition OCaml_open_out : string -> out_channel := fun _ => tt. |
| 74 |
Definition OCaml_close_in : in_channel -> unit := fun _ => tt. |
| 75 |
Definition OCaml_close_out : out_channel -> unit := fun _ => tt. |
| 76 |
Definition read_channel_rev : in_channel -> list string := fun _ => nil. |
| 77 |
End OCamlPrimitives. |
| 78 |
|
| 79 |
Extract Inductive int |
| 80 |
=> "Int.t" [ "0" "(fun n -> n+1)" ] |
| 81 |
"(fun fO fS n -> if n=0 then fO () else fS (n-1))". |
| 82 |
(* We cannot inline these constants due to COQBUG(https://github.com/coq/coq/issues/16169) *) |
| 83 |
Extract (*Inlined*) Constant in_channel => "in_channel". |
| 84 |
Extract (*Inlined*) Constant out_channel => "out_channel". |
| 85 |
Extract Constant fprintf_char => |
| 86 |
"fun chan c -> Printf.fprintf chan ""%c%!"" c". |
| 87 |
Extract Constant flush => |
| 88 |
"fun chan -> Printf.fprintf chan ""%!""". |
| 89 |
Extract (*Inlined*) Constant stdin => "stdin". |
| 90 |
Extract (*Inlined*) Constant stdout => "stdout". |
| 91 |
Extract (*Inlined*) Constant stderr => "stderr". |
| 92 |
Extract (*Inlined*) Constant string => "string". |
| 93 |
Extract (*Inlined*) Constant string_length => "String.length". |
| 94 |
Extract (*Inlined*) Constant string_get => "String.get". |
| 95 |
Extract Constant sys_argv => "Array.to_list Sys.argv". |
| 96 |
Extract (*Inlined*) Constant string_init => "String.init". |
| 97 |
Extract Constant raise_Failure => "fun x -> raise (Failure x)". |
| 98 |
Extract (*Inlined*) Constant open_in => "open_in". |
| 99 |
Extract (*Inlined*) Constant open_out => "open_out". |
| 100 |
Extract (*Inlined*) Constant close_in => "close_in". |
| 101 |
Extract (*Inlined*) Constant close_out => "close_out". |
| 102 |
Extract Constant read_channel_rev |
| 103 |
=> "fun chan -> |
| 104 |
let lines = ref [] in |
| 105 |
try |
| 106 |
while true; do |
| 107 |
lines := input_line chan :: !lines |
| 108 |
done; !lines |
| 109 |
with End_of_file -> |
| 110 |
!lines". |
| 111 |
|
| 112 |
Fixpoint nat_of_int (x : int) : nat |
| 113 |
:= match x with |
| 114 |
| int_O => O |
| 115 |
| int_S x' => S (nat_of_int x') |
| 116 |
end. |
| 117 |
Fixpoint int_of_nat (x : nat) : int |
| 118 |
:= match x with |
| 119 |
| O => int_O |
| 120 |
| S x' => int_S (int_of_nat x') |
| 121 |
end. |
| 122 |
Global Set Warnings Append "-ambiguous-paths". |
| 123 |
Coercion nat_of_int : int >-> nat. |
| 124 |
Coercion int_of_nat : nat >-> int. |
| 125 |
|
| 126 |
Definition string_of_Coq_string (s : String.string) : string |
| 127 |
:= let s := String.list_ascii_of_string s in |
| 128 |
string_init |
| 129 |
(List.length s) |
| 130 |
(fun n => List.nth n s "?"%char). |
| 131 |
|
| 132 |
Definition string_to_Coq_string (s : string) : String.string |
| 133 |
:= String.string_of_list_ascii |
| 134 |
(List.map (fun n:nat => string_get s n) (List.seq 0 (string_length s))). |
| 135 |
|
| 136 |
Definition seq {A B} (x : unit -> A) (f : A -> B) : B := let y := x tt in f y. |
| 137 |
Extraction NoInline seq. |
| 138 |
(* |
| 139 |
Axiom seq : forall A B, (unit -> A) -> (A -> B) -> B. |
| 140 |
Extract Inlined Constant seq => "(fun x f => let y = x () in f y)". |
| 141 |
*) |
| 142 |
|
| 143 |
Fixpoint list_iter {A} (f : A -> unit) (ls : list A) : unit |
| 144 |
:= match ls with |
| 145 |
| cons x xs => seq (fun _ => f x) (fun _ => @list_iter A f xs) |
| 146 |
| nil => tt |
| 147 |
end. |
| 148 |
|
| 149 |
Definition fprintf_list_string (chan : out_channel) (strs : list String.string) : unit |
| 150 |
:= list_iter |
| 151 |
(fun ls |
| 152 |
=> list_iter (fprintf_char chan) (String.list_ascii_of_string ls)) |
| 153 |
strs. |
| 154 |
Definition printf_list_string (strs : list String.string) : unit |
| 155 |
:= fprintf_list_string stdout strs. |
| 156 |
Definition fprintf_list_string_with_newlines (chan : out_channel) (strs : list String.string) : unit |
| 157 |
:= match strs with |
| 158 |
| nil => fprintf_list_string chan nil |
| 159 |
| str :: strs => fprintf_list_string chan |
| 160 |
(str :: List.map (String.String Ascii.NewLine) strs |
| 161 |
++ [String.NewLine; String.NewLine])%list |
| 162 |
end. |
| 163 |
Definition printf_list_string_with_newlines (strs : list String.string) : unit |
| 164 |
:= fprintf_list_string_with_newlines stdout strs. |
| 165 |
|
| 166 |
Definition raise_failure (msg : list String.string) |
| 167 |
:= seq (fun _ => fprintf_list_string_with_newlines stdout msg) |
| 168 |
(fun _ => raise_Failure (string_of_Coq_string "Synthesis failed")). |
| 169 |
|
| 170 |
Global Instance OCamlIODriver : ForExtraction.IODriverAPI unit |
| 171 |
:= { ForExtraction.error := raise_failure |
| 172 |
; ForExtraction.ret := fun 'tt => tt |
| 173 |
; ForExtraction.with_read_stdin k |
| 174 |
:= seq (fun 'tt => read_channel_rev stdin) |
| 175 |
(fun rev_lines => k (List.map string_to_Coq_string (List.rev_append rev_lines nil))) |
| 176 |
; ForExtraction.write_stdout_then lines k |
| 177 |
:= seq (fun _ => fprintf_list_string stdout lines) |
| 178 |
k |
| 179 |
; ForExtraction.write_stderr_then lines k |
| 180 |
:= seq (fun _ => fprintf_list_string stderr lines) |
| 181 |
k |
| 182 |
; ForExtraction.with_read_file fname k |
| 183 |
:= seq (fun 'tt => open_in (string_of_Coq_string fname)) |
| 184 |
(fun chan |
| 185 |
=> seq (fun 'tt => read_channel_rev chan) |
| 186 |
(fun rev_lines => seq (fun 'tt => close_in chan) |
| 187 |
(fun 'tt => k (List.map string_to_Coq_string (List.rev_append rev_lines nil))))) |
| 188 |
; ForExtraction.write_file_then fname lines k |
| 189 |
:= seq (fun 'tt => open_out (string_of_Coq_string fname)) |
| 190 |
(fun chan |
| 191 |
=> seq (fun 'tt => fprintf_list_string chan lines) |
| 192 |
(fun 'tt => seq (fun 'tt => close_out chan) |
| 193 |
k)) |
| 194 |
}. |
| 195 |
|
| 196 |
Definition main_gen |
| 197 |
{supported_languages : ForExtraction.supported_languagesT} |
| 198 |
(PipelineMain : forall (A := _) |
| 199 |
(argv : list String.string), |
| 200 |
A) |
| 201 |
: unit |
| 202 |
:= let argv := List.map string_to_Coq_string sys_argv in |
| 203 |
PipelineMain argv. |
| 204 |
|
| 205 |
Local Existing Instance ForExtraction.default_supported_languages. |
| 206 |
|
| 207 |
Module UnsaturatedSolinas. |
| 208 |
Definition main : unit |
| 209 |
:= main_gen ForExtraction.UnsaturatedSolinas.PipelineMain. |
| 210 |
End UnsaturatedSolinas. |
| 211 |
|
| 212 |
Module WordByWordMontgomery. |
| 213 |
Definition main : unit |
| 214 |
:= main_gen ForExtraction.WordByWordMontgomery.PipelineMain. |
| 215 |
End WordByWordMontgomery. |
| 216 |
|
| 217 |
Module SaturatedSolinas. |
| 218 |
Definition main : unit |
| 219 |
:= main_gen ForExtraction.SaturatedSolinas.PipelineMain. |
| 220 |
End SaturatedSolinas. |
| 221 |
|
| 222 |
Module DettmanMultiplication. |
| 223 |
Definition main : unit |
| 224 |
:= main_gen ForExtraction.DettmanMultiplication.PipelineMain. |
| 225 |
End DettmanMultiplication. |
| 226 |
|
| 227 |
Module SolinasReduction. |
| 228 |
Definition main : unit |
| 229 |
:= main_gen ForExtraction.SolinasReduction.PipelineMain. |
| 230 |
End SolinasReduction. |
| 231 |
|
| 232 |
Module BaseConversion. |
| 233 |
Definition main : unit |
| 234 |
:= main_gen ForExtraction.BaseConversion.PipelineMain. |
| 235 |
End BaseConversion. |
| 236 |
|
| 237 |
Module FiatCrypto. |
| 238 |
Definition main : unit |
| 239 |
:= main_gen ForExtraction.FiatCrypto.PipelineMain. |
| 240 |
End FiatCrypto. |