forked from mit-plv/fiat-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStandaloneHaskellMain.v
151 lines (135 loc) · 5.71 KB
/
StandaloneHaskellMain.v
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
Require Export Coq.extraction.Extraction.
Require Export Coq.extraction.ExtrHaskellBasic.
Require Export Coq.extraction.ExtrHaskellString.
Require Import Coq.Lists.List.
Require Import Coq.Strings.String.
Require Crypto.Util.Strings.String.
Require Import Crypto.CLI.
Require Import Crypto.Util.Notations.
Import ListNotations. Local Open Scope string_scope.
Global Set Warnings Append "-extraction-opaque-accessed".
Extraction Language Haskell.
Global Unset Extraction Optimize.
(** We pull a hack to get coqchk to not report these as axioms; for
this, all we care about is that there exists a model. *)
Module Type HaskellPrimitivesT.
Axiom IO_unit : Set.
Axiom _IO : Set -> Set.
Axiom Handle : Set.
Axiom stdin : Handle.
Axiom stdout : Handle.
Axiom stderr : Handle.
Axiom hPutStr : Handle -> string -> _IO unit.
Axiom getArgs : _IO (list string).
Axiom getProgName : _IO string.
Axiom raise_failure : string -> _IO unit.
Axiom _IO_bind : forall A B, _IO A -> (A -> _IO B) -> _IO B.
Axiom _IO_return : forall A : Set, A -> _IO A.
Axiom cast_io : _IO unit -> IO_unit.
Axiom uncast_io : IO_unit -> _IO unit.
Axiom getContents : _IO string.
Axiom readFile : string -> _IO string.
Axiom writeFile : string (* fname *) -> string (* contents *) -> _IO unit.
End HaskellPrimitivesT.
Module Export HaskellPrimitives : HaskellPrimitivesT.
Definition IO_unit : Set := unit.
Definition _IO : Set -> Set := fun T => T.
Definition Handle : Set := unit.
Definition stdin : Handle := tt.
Definition stdout : Handle := tt.
Definition stderr : Handle := tt.
Definition hPutStr : Handle -> string -> _IO unit := fun _ _ => tt.
Definition getArgs : _IO (list string) := nil.
Definition getProgName : _IO string := "".
Definition raise_failure : string -> _IO unit := fun _ => tt.
Definition _IO_bind : forall A B, _IO A -> (A -> _IO B) -> _IO B := fun A B x f => f x.
Definition _IO_return : forall A : Set, A -> _IO A := fun A x => x.
Definition cast_io : _IO unit -> IO_unit := fun x => x.
Definition uncast_io : IO_unit -> _IO unit := fun x => x.
Definition getContents : _IO string := "".
Definition readFile : string -> _IO string := fun _ => "".
Definition writeFile : string -> string -> _IO unit := fun _ _ => tt.
End HaskellPrimitives.
Extract Constant _IO "a" => "GHC.Base.IO a".
Extract Inlined Constant Handle => "System.IO.Handle".
Extract Inlined Constant stdin => "System.IO.stdin".
Extract Inlined Constant stdout => "System.IO.stdout".
Extract Inlined Constant stderr => "System.IO.stderr".
Extract Inlined Constant hPutStr => "System.IO.hPutStr".
Extract Inlined Constant getArgs => "System.Environment.getArgs".
Extract Inlined Constant getProgName => "System.Environment.getProgName".
Extract Constant raise_failure => "\x -> Prelude.error x".
Extract Inlined Constant _IO_bind => "(Prelude.>>=)".
Extract Inlined Constant _IO_return => "GHC.Base.return".
Extract Inlined Constant IO_unit => "GHC.Base.IO ()".
Extract Inlined Constant cast_io => "".
Extract Inlined Constant uncast_io => "".
Extract Inlined Constant getContents => "Prelude.getContents".
Extract Inlined Constant readFile => "Prelude.readFile".
Extract Inlined Constant writeFile => "Prelude.writeFile".
(* COQBUG(https://github.com/coq/coq/issues/12258) *)
Extract Inlined Constant String.eqb => "((Prelude.==) :: Prelude.String -> Prelude.String -> Prelude.Bool)".
Local Notation "x <- y ; f" := (_IO_bind _ _ y (fun x => f)).
Definition raise_failure (msg : list String.string) : _IO unit
:= (_ <- hPutStr stdout (String.concat String.NewLine msg);
raise_failure "Synthesis failed").
Global Instance HaskellIODriver : ForExtraction.IODriverAPI (_IO unit)
:= {
ForExtraction.error := raise_failure
; ForExtraction.ret 'tt := _IO_return _ tt
; ForExtraction.with_read_stdin k :=
(lines <- getContents;
k (String.split String.NewLine lines))
; ForExtraction.write_stdout_then lines k
:= (_ <- hPutStr stdout (String.concat "" lines);
k tt)
; ForExtraction.write_stderr_then lines k
:= (_ <- hPutStr stdout (String.concat "" lines);
k tt)
; ForExtraction.with_read_file fname k
:= (lines <- readFile fname;
k (String.split String.NewLine lines))
; ForExtraction.write_file_then fname lines k
:= (_ <- writeFile fname (String.concat "" lines);
k tt)
}.
Definition main_gen
{supported_languages : ForExtraction.supported_languagesT}
(PipelineMain : forall (A := _)
(argv : list String.string),
A)
: IO_unit
:= cast_io
(argv <- getArgs;
prog <- getProgName;
PipelineMain
(prog::argv)).
Local Existing Instance ForExtraction.default_supported_languages.
Module UnsaturatedSolinas.
Definition main : IO_unit
:= main_gen ForExtraction.UnsaturatedSolinas.PipelineMain.
End UnsaturatedSolinas.
Module WordByWordMontgomery.
Definition main : IO_unit
:= main_gen ForExtraction.WordByWordMontgomery.PipelineMain.
End WordByWordMontgomery.
Module SaturatedSolinas.
Definition main : IO_unit
:= main_gen ForExtraction.SaturatedSolinas.PipelineMain.
End SaturatedSolinas.
Module DettmanMultiplication.
Definition main : IO_unit
:= main_gen ForExtraction.DettmanMultiplication.PipelineMain.
End DettmanMultiplication.
Module SolinasReduction.
Definition main : IO_unit
:= main_gen ForExtraction.SolinasReduction.PipelineMain.
End SolinasReduction.
Module BaseConversion.
Definition main : IO_unit
:= main_gen ForExtraction.BaseConversion.PipelineMain.
End BaseConversion.
Module FiatCrypto.
Definition main : IO_unit
:= main_gen ForExtraction.FiatCrypto.PipelineMain.
End FiatCrypto.