| 1 |
From Coq Require Export Extraction. |
| 2 |
From Coq Require Export ExtrHaskellBasic. |
| 3 |
From Coq Require Export ExtrHaskellString. |
| 4 |
From Coq Require Import List. |
| 5 |
From Coq Require Import String. |
| 6 |
Require Crypto.Util.Strings.String. |
| 7 |
Require Import Crypto.CLI. |
| 8 |
Require Import Crypto.Util.Notations. |
| 9 |
Import ListNotations. Local Open Scope string_scope. |
| 10 |
|
| 11 |
Global Set Warnings Append "-extraction-opaque-accessed". |
| 12 |
Extraction Language Haskell. |
| 13 |
Global Unset Extraction Optimize. |
| 14 |
|
| 15 |
(** We pull a hack to get coqchk to not report these as axioms; for |
| 16 |
this, all we care about is that there exists a model. *) |
| 17 |
Module Type HaskellPrimitivesT. |
| 18 |
Axiom IO_unit : Set. |
| 19 |
Axiom _IO : Set -> Set. |
| 20 |
Axiom Handle : Set. |
| 21 |
Axiom stdin : Handle. |
| 22 |
Axiom stdout : Handle. |
| 23 |
Axiom stderr : Handle. |
| 24 |
Axiom hPutStr : Handle -> string -> _IO unit. |
| 25 |
Axiom getArgs : _IO (list string). |
| 26 |
Axiom getProgName : _IO string. |
| 27 |
Axiom raise_failure : string -> _IO unit. |
| 28 |
Axiom _IO_bind : forall A B, _IO A -> (A -> _IO B) -> _IO B. |
| 29 |
Axiom _IO_return : forall A : Set, A -> _IO A. |
| 30 |
Axiom cast_io : _IO unit -> IO_unit. |
| 31 |
Axiom uncast_io : IO_unit -> _IO unit. |
| 32 |
Axiom getContents : _IO string. |
| 33 |
Axiom readFile : string -> _IO string. |
| 34 |
Axiom writeFile : string (* fname *) -> string (* contents *) -> _IO unit. |
| 35 |
End HaskellPrimitivesT. |
| 36 |
|
| 37 |
Module Export HaskellPrimitives : HaskellPrimitivesT. |
| 38 |
Definition IO_unit : Set := unit. |
| 39 |
Definition _IO : Set -> Set := fun T => T. |
| 40 |
Definition Handle : Set := unit. |
| 41 |
Definition stdin : Handle := tt. |
| 42 |
Definition stdout : Handle := tt. |
| 43 |
Definition stderr : Handle := tt. |
| 44 |
Definition hPutStr : Handle -> string -> _IO unit := fun _ _ => tt. |
| 45 |
Definition getArgs : _IO (list string) := nil. |
| 46 |
Definition getProgName : _IO string := "". |
| 47 |
Definition raise_failure : string -> _IO unit := fun _ => tt. |
| 48 |
Definition _IO_bind : forall A B, _IO A -> (A -> _IO B) -> _IO B := fun A B x f => f x. |
| 49 |
Definition _IO_return : forall A : Set, A -> _IO A := fun A x => x. |
| 50 |
Definition cast_io : _IO unit -> IO_unit := fun x => x. |
| 51 |
Definition uncast_io : IO_unit -> _IO unit := fun x => x. |
| 52 |
Definition getContents : _IO string := "". |
| 53 |
Definition readFile : string -> _IO string := fun _ => "". |
| 54 |
Definition writeFile : string -> string -> _IO unit := fun _ _ => tt. |
| 55 |
End HaskellPrimitives. |
| 56 |
|
| 57 |
Extract Constant _IO "a" => "GHC.Base.IO a". |
| 58 |
Extract Inlined Constant Handle => "System.IO.Handle". |
| 59 |
Extract Inlined Constant stdin => "System.IO.stdin". |
| 60 |
Extract Inlined Constant stdout => "System.IO.stdout". |
| 61 |
Extract Inlined Constant stderr => "System.IO.stderr". |
| 62 |
Extract Inlined Constant hPutStr => "System.IO.hPutStr". |
| 63 |
Extract Inlined Constant getArgs => "System.Environment.getArgs". |
| 64 |
Extract Inlined Constant getProgName => "System.Environment.getProgName". |
| 65 |
Extract Constant raise_failure => "\x -> Prelude.error x". |
| 66 |
Extract Inlined Constant _IO_bind => "(Prelude.>>=)". |
| 67 |
Extract Inlined Constant _IO_return => "GHC.Base.return". |
| 68 |
Extract Inlined Constant IO_unit => "GHC.Base.IO ()". |
| 69 |
Extract Inlined Constant cast_io => "". |
| 70 |
Extract Inlined Constant uncast_io => "". |
| 71 |
Extract Inlined Constant getContents => "Prelude.getContents". |
| 72 |
Extract Inlined Constant readFile => "Prelude.readFile". |
| 73 |
Extract Inlined Constant writeFile => "Prelude.writeFile". |
| 74 |
(* COQBUG(https://github.com/coq/coq/issues/12258) *) |
| 75 |
Extract Inlined Constant String.eqb => "((Prelude.==) :: Prelude.String -> Prelude.String -> Prelude.Bool)". |
| 76 |
|
| 77 |
Local Notation "x <- y ; f" := (_IO_bind _ _ y (fun x => f)). |
| 78 |
|
| 79 |
Definition raise_failure (msg : list String.string) : _IO unit |
| 80 |
:= (_ <- hPutStr stdout (String.concat String.NewLine msg); |
| 81 |
raise_failure "Synthesis failed"). |
| 82 |
|
| 83 |
Global Instance HaskellIODriver : ForExtraction.IODriverAPI (_IO unit) |
| 84 |
:= { |
| 85 |
ForExtraction.error := raise_failure |
| 86 |
; ForExtraction.ret 'tt := _IO_return _ tt |
| 87 |
; ForExtraction.with_read_stdin k := |
| 88 |
(lines <- getContents; |
| 89 |
k (String.split String.NewLine lines)) |
| 90 |
; ForExtraction.write_stdout_then lines k |
| 91 |
:= (_ <- hPutStr stdout (String.concat "" lines); |
| 92 |
k tt) |
| 93 |
; ForExtraction.write_stderr_then lines k |
| 94 |
:= (_ <- hPutStr stdout (String.concat "" lines); |
| 95 |
k tt) |
| 96 |
; ForExtraction.with_read_file fname k |
| 97 |
:= (lines <- readFile fname; |
| 98 |
k (String.split String.NewLine lines)) |
| 99 |
; ForExtraction.write_file_then fname lines k |
| 100 |
:= (_ <- writeFile fname (String.concat "" lines); |
| 101 |
k tt) |
| 102 |
}. |
| 103 |
|
| 104 |
Definition main_gen |
| 105 |
{supported_languages : ForExtraction.supported_languagesT} |
| 106 |
(PipelineMain : forall (A := _) |
| 107 |
(argv : list String.string), |
| 108 |
A) |
| 109 |
: IO_unit |
| 110 |
:= cast_io |
| 111 |
(argv <- getArgs; |
| 112 |
prog <- getProgName; |
| 113 |
PipelineMain |
| 114 |
(prog::argv)). |
| 115 |
|
| 116 |
Local Existing Instance ForExtraction.default_supported_languages. |
| 117 |
|
| 118 |
Module UnsaturatedSolinas. |
| 119 |
Definition main : IO_unit |
| 120 |
:= main_gen ForExtraction.UnsaturatedSolinas.PipelineMain. |
| 121 |
End UnsaturatedSolinas. |
| 122 |
|
| 123 |
Module WordByWordMontgomery. |
| 124 |
Definition main : IO_unit |
| 125 |
:= main_gen ForExtraction.WordByWordMontgomery.PipelineMain. |
| 126 |
End WordByWordMontgomery. |
| 127 |
|
| 128 |
Module SaturatedSolinas. |
| 129 |
Definition main : IO_unit |
| 130 |
:= main_gen ForExtraction.SaturatedSolinas.PipelineMain. |
| 131 |
End SaturatedSolinas. |
| 132 |
|
| 133 |
Module DettmanMultiplication. |
| 134 |
Definition main : IO_unit |
| 135 |
:= main_gen ForExtraction.DettmanMultiplication.PipelineMain. |
| 136 |
End DettmanMultiplication. |
| 137 |
|
| 138 |
Module SolinasReduction. |
| 139 |
Definition main : IO_unit |
| 140 |
:= main_gen ForExtraction.SolinasReduction.PipelineMain. |
| 141 |
End SolinasReduction. |
| 142 |
|
| 143 |
Module BaseConversion. |
| 144 |
Definition main : IO_unit |
| 145 |
:= main_gen ForExtraction.BaseConversion.PipelineMain. |
| 146 |
End BaseConversion. |
| 147 |
|
| 148 |
Module FiatCrypto. |
| 149 |
Definition main : IO_unit |
| 150 |
:= main_gen ForExtraction.FiatCrypto.PipelineMain. |
| 151 |
End FiatCrypto. |