aboutsummaryrefslogtreecommitdiffstats
path: root/correct.v
blob: 89f5894d65d2073a323d052d28e06ce03db34b70 (plain) (blame)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
Require Import Coq.Strings.String.
From stdpp Require Import gmap.
From mininix Require Import
  expr relations sem interpreter complete sound shared.

Theorem correct e v' : (∃ n, eval n false e = Some v') ↔ e -->* v'.
Proof.
  split.
  - intros [n Heval]. by apply (eval_sound_strong n false).
  - intros Heval. by apply eval_complete.
Qed.

(* Top-level program reduction/evaluation:
     we make the prelude available here. *)

Definition with_prelude (e : expr) : expr :=
  subst (closed prelude) e.

Definition tl_reds (e e' : expr) :=
  with_prelude e -->* e'.

Definition tl_eval (n : nat) (e : expr) : option value :=
  eval n false (subst (closed prelude) e).

Definition tl_evals e e' := ∃ n, tl_eval n e = Some e'.

(* Macros *)

Definition μ_neq e1 e2 := X_Cond (X_Op O_Eq e1 e2) false true.
Definition μ_or e1 e2 := X_Cond e1 true e2.
Definition μ_and e1 e2 := X_Cond e1 e2 false.
Definition μ_impl e1 e2 := X_Cond e1 e2 true.
Definition μ_neg e := X_Cond e false true.

Definition μ_le n m := μ_or (X_Op O_Eq n m) (X_Op O_Lt n m).
Definition μ_gt n m := X_Op O_Lt m n.
Definition μ_ge n m := μ_or (X_Op O_Eq n m) (μ_gt n m).

(* Tests/examples *)

Definition ex1 := X_LetBinding {[ "a" := B_Rec 1%Z ]} "a".

(* [let a = 1; in a] gives 1 *)
Theorem ex1_step : tl_reds ex1 1%Z.
Proof.
  unfold ex1.
  eapply rtc_l.
  - by eapply E_Ctx with (E := id).
  - simplify_option_eq.
    eapply rtc_once.
    by eapply E_Ctx with (E := id).
Qed.

Example ex1_eval : tl_evals ex1 (V_Int 1).
Proof. by exists 3. Qed.

(* Definition ex2 := <{ let a = 1 in with { a = 2 }; a }>. *)
Definition ex2 := X_LetBinding {[ "a" := B_Rec 1%Z ]}
  (X_Incl (V_Attrset {[ "a" := X_V 2%Z ]}) "a").

(* [let a = 1; in with { a = 2; }; a] gives 1 *)
Theorem ex2_step : tl_reds ex2 1%Z.
Proof.
  unfold ex2.
  eapply rtc_l.
  - by eapply E_Ctx with (E := id).
  - simplify_option_eq.
    eapply rtc_l.
    + by eapply E_Ctx with (E := id).
    + simpl. eapply rtc_once.
      by eapply E_Ctx with (E := id).
Qed.

Example ex2_eval : tl_evals ex2 (V_Int 1).
Proof. by exists 4. Qed.

(* [with { a = 1; }; with { a = 2; }; a] gives 2 *)
Definition ex3 :=
  X_Incl (V_Attrset {[ "a" := X_V 1%Z ]})
    (X_Incl (V_Attrset {[ "a" := X_V 2%Z ]}) "a").

Theorem ex3_step : tl_reds ex3 2%Z.
Proof.
  unfold ex3.
  eapply rtc_l.
  - eapply E_Ctx with (E := id); [done | apply E_With].
  - simpl. eapply rtc_l.
    + by eapply E_Ctx with (E := id).
    + simplify_option_eq.
      eapply rtc_once.
      by eapply E_Ctx with (E := id).
Qed.

Example ex3_eval : tl_evals ex3 (V_Int 2).
Proof. by exists 4. Qed.

(* [({ x, y ? x } : y) { x = 1; }] gives 1 *)
Definition ex4 :=
  X_Apply
    (V_AttrsetFn
       (M_Matcher
          {[ "x" := M_Mandatory;
             "y" := M_Optional "x"
          ]}
          true)
       "y")
    (V_Attrset {[ "x" := X_V 1%Z ]}).

Example ex4_eval : tl_evals ex4 (V_Int 1).
Proof. by exists 7. Qed.

(* [({ x ? y, y ? x } : y) { x = 1; }] gives 1 *)
Definition ex5 :=
  X_Apply
    (V_AttrsetFn
       (M_Matcher
          {[ "x" := M_Optional "y";
             "y" := M_Optional "x"
          ]}
          true)
       "y")
    (V_Attrset {[ "x" := X_V 1%Z ]}).

Example ex5_eval : tl_evals ex5 (V_Int 1).
Proof. by exists 7. Qed.

(* [let binToString = n:
          if n == 0
          then "0"
          else if n == 1
               then "1"
               else binToString (n / 2) + (if isEven n then "0" else "1");
        isEven = n: n != 1 && (n = 0 || isEven (n - 2));
        test = { x, y ? attrs.x, ... } @ attrs:
          "x: " + x + ", y: " + y + ", z: " + attrs.z or "(no z)"
    in test { x = binToString 6; }] gives "x: 110, y: 110, z: (no z)" *)
Definition ex6 :=
  X_LetBinding
    {[ "binToString" := B_Rec $ V_Fn "n" $
         X_Cond
           (X_Op O_Eq "n" 0%Z)
           (V_Str "0")
           (X_Cond
             (X_Op O_Eq "n" 1%Z)
             (V_Str "1")
             (X_Op O_Plus
               (X_Apply
                 "binToString"
                 (X_Op O_Div "n" 2%Z))
               (X_Cond
                 (X_Apply "isEven" "n")
                 (V_Str "0")
                 (V_Str "1"))));
       "isEven" := B_Rec $ V_Fn "n" $
         μ_and
           (μ_neq "n" 1%Z)
           (μ_or
             (X_Op O_Eq "n" 0%Z)
             (X_Apply "isEven" (X_Op O_Min "n" 2%Z)));
       "test" := B_Rec $ V_Fn "attrs" $
         X_Apply
           (V_AttrsetFn
             (M_Matcher
               {[ "x" := M_Mandatory;
                  "y" := M_Optional
                    (X_Select "attrs"
                    (nonempty_singleton "x"))
               ]} false)
             (X_Op O_Plus
               (X_Op O_Plus
                 (X_Op O_Plus
                   (X_Op O_Plus
                     (X_Op O_Plus
                       (V_Str "x: ")
                       "x")
                     (V_Str ", y: "))
                   "y")
                 (V_Str ", z: "))
               (X_SelectOr
                 "attrs"
                 (nonempty_singleton "z")
                 (V_Str "(no z)"))))
           "attrs"
    ]}
    (X_Apply "test" $ V_Attrset
       {[ "x" := X_Apply "binToString" 6%Z ]}).

Example ex6_eval : tl_evals ex6 (V_Str "x: 110, y: 110, z: (no z)").
Proof. by exists 37. Qed.

(* Important check of if placeholders work correctly:
   [with { x = 1; }; let foo = y: let x = 2; in y; foo x]
   gives 1 *)
Definition ex7 := X_Incl
  (V_Attrset {[ "x" := X_V 1%Z ]})
  (X_LetBinding
    {[ "foo" := B_Rec $ V_Fn "y" $
         X_LetBinding {[ "x" := B_Rec 2%Z ]} "y"
    ]}
    (X_Apply "foo" "x")).

Example ex7_eval : tl_evals ex7 (V_Int 1).
Proof. by exists 7. Qed.

Definition ex8 :=
  X_LetBinding
    {[ "divide" := B_Rec $ V_Fn "a" $ V_Fn "b" $
         X_Assert
           (μ_and (μ_ge "a" 0%Z) (μ_gt "b" 0%Z))
           (X_Cond
             (X_Op O_Lt "a" "b")
             0
             (X_Op
               O_Plus
               (X_Apply
                 (X_Apply
                   "divide"
                   (X_Op O_Min "a" "b"))
                 "b")
               1));
       "divider" := B_Rec $ X_Attrset
         {[ "__functor" := B_Nonrec $ V_Fn "self" $ V_Fn "x" $
              X_Op
                O_Upd
                "self"
                (X_Attrset
                  {[ "value" := B_Nonrec $
                       X_Apply
                         (X_Apply
                           "divide"
                           (X_Select "self" $ nonempty_singleton "value"))
                         "x"
                  ]})
         ]};
       "mkDivider" := B_Rec $ V_Fn "value" $
         X_Op
           O_Upd
           "divider"
           (X_Attrset {[ "value" := B_Nonrec "value" ]})
    ]}%Z
    (X_Select
      (X_Apply (X_Apply (X_Apply "mkDivider" 100) 5) 4)
      (nonempty_singleton "value"))%Z.

Example ex8_eval : tl_evals ex8 (V_Int 5).
Proof. by exists 170. Qed.

Example ex9 :=
  X_Apply
    (X_Apply
       (V_Attrset
         {[ "__functor" := X_V $ V_Fn "self" $ V_Fn "f" $
              X_Apply "f" (X_Apply "self" "f")
         ]})
       (V_Fn "go" $ V_Fn "n" $
         X_Cond
           (μ_le "n" 1)
           "n"
           (X_Op
             O_Plus
             (X_Apply "go" (X_Op O_Min "n" 1))
             (X_Apply "go" (X_Op O_Min "n" 2)))))%Z
    15%Z.

Example ex9_eval : tl_evals ex9 (V_Int 610).
Proof. by exists 78. Qed.

Example ex10 :=
  X_LetBinding
    {[ "true" := B_Rec 42 ]}%Z
    (X_Op O_Eq "true" 42)%Z.

Example ex10_eval : tl_evals ex10 (V_Bool true).
Proof. by exists 4. Qed.

Definition ex11 :=
  X_LetBinding
    {[ "x" := B_Rec "y" ]}%Z
    (X_LetBinding {[ "y" := B_Rec 10 ]} "x")%Z.

Example ex11_eval : tl_eval 1000 ex11 = None.
Proof. done. Qed.

Definition ex12 :=
  X_LetBinding
    {[ "pkgs" := B_Rec $ V_Attrset
          {[ "x" := X_Incl (V_Attrset {[ "y" := X_V 1%Z ]}) "y" ]}
    ]}
    (X_Select
      (X_Attrset
        {[ "x" := B_Rec $ X_Select "pkgs" (nonempty_singleton "x");
           "y" := B_Rec 3%Z
        ]})
      (nonempty_singleton "x")).

Example ex12_eval : tl_eval 1000 ex12 = Some (V_Int 1).
Proof. done. Qed.

(* Aio, quantitas magna frumentorum est. *)