Source file Support.ml

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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
(******************************************************************************)
(*                                                                            *)
(*                                  Monolith                                  *)
(*                                                                            *)
(*                              François Pottier                              *)
(*                                                                            *)
(*  Copyright Inria. All rights reserved. This file is distributed under the  *)
(*  terms of the GNU Lesser General Public License as published by the Free   *)
(*  Software Foundation, either version 3 of the License, or (at your         *)
(*  option) any later version, as described in the file LICENSE.              *)
(*                                                                            *)
(******************************************************************************)

(* This module offers utility functions that can play a role in scenarios.
   This means that we must make them available to the user. A simple-minded
   approach would be to print their definitions as part of error scenarios.
   It seems preferable to just make them available as part of the Monolith
   API, so the user can type [#require "monolith";;] in the OCaml REPL and
   will have access to all functions in [Monolith.Support]. *)

(* We define [Sup] as a short name for [Monolith.Support] at the beginning
   of every scenario. This is done in [Engine.main]. *)

let support name =
  Code.constant ("Sup." ^ name)

(* -------------------------------------------------------------------------- *)

module Fun = struct

  let id x = x

  module Id = struct

    let appearance =
      support "Fun.id"

    (* The following is an optional optimization: when [id] is applied to at
       least one argument, we can perform beta-reduction on the fly, so that
       the application of [id] becomes invisible. *)

    let appearance =
      Code.custom (fun actuals ->
        match actuals with
        | x :: more ->
            Print.apply x more
        | _ ->
            Code.apply appearance actuals
      )

    let code =
      id, appearance

  end

  let rot2 f y x =
    f x y

  module Rot2 = struct

    let appearance =
      support "Fun.rot2"

    (* The following is an optional optimization: when [rot2] is applied to at
       least three actual arguments, we can perform beta-reduction on the fly,
       so that the application of [rot2] becomes invisible. This is permitted,
       even though the actual arguments are not necessarily values, because
       [rot2] uses its arguments linearly and because we can assume that at
       most of one the arguments raises an exception. It is definitely a bit
       fragile. *)

    let appearance =
      Code.custom (fun actuals ->
        match actuals with
        | f :: y :: x :: more ->
            (* Someone who is crazy about detail will note that since [f]
               moves from argument position back to head position, it no
               longer needs to be parenthesized; but we have no way of
               removing parentheses. *)
            Print.apply f (x :: y :: more)
        | _ ->
            (* We have fewer than three actual arguments; revert to the normal
               appearance. *)
            Code.apply appearance actuals
      )

    let code =
      rot2, appearance

  end

  let rot3 f z x y =
    f x y z

  module Rot3 = struct

    let appearance =
      support "Fun.rot3"

    let appearance =
      Code.custom (fun actuals ->
        match actuals with
        | f :: z :: x :: y :: more ->
            Print.apply f (x :: y :: z :: more)
        | _ ->
            Code.apply appearance actuals
      )

    let code =
      rot3, appearance

  end

  let curry f x y =
    f (x, y)

  module Curry = struct

    let appearance =
      support "Fun.curry"

    let appearance =
      Code.custom (fun actuals ->
        match actuals with
        | f :: x :: y :: more ->
            Print.apply f (PPrint.OCaml.tuple [ x; y ] :: more)
        | _ ->
            Code.apply appearance actuals
      )

    let code =
      curry, appearance

  end

  let uncurry f (x, y) =
    f x y

  module Uncurry = struct

    let appearance =
      support "Fun.uncurry"

    let code =
      uncurry, appearance

  end

end

(* -------------------------------------------------------------------------- *)

module List = struct

  (* Testing two lists for equality. *)

  let equal eq xs ys =
    List.length xs = List.length ys &&
    List.for_all2 eq xs ys

  (* [List.to_seq] appears in OCaml 4.07. *)

  let rec to_seq xs =
    fun () ->
      match xs with
      | [] ->
          Seq.Nil
      | x :: xs ->
          Seq.Cons (x, to_seq xs)

  module ToSeq = struct

    let appearance =
      support "List.to_seq"

    let code =
      to_seq, appearance

  end

end

(* -------------------------------------------------------------------------- *)

module Exn = struct

  (* Catching all exceptions. *)

  let handle f x =
    try Ok (f x) with
    | Engine.PleaseBackOff -> raise Engine.PleaseBackOff
    | e -> Error e

  module Handle = struct

    let appearance =
      support "Exn.handle"

    let code =
      handle, appearance

  end

end

(* -------------------------------------------------------------------------- *)

module Seq = struct

  include Seq

  (* One-shot functions. *)

  exception ForcedTwice

  let oneshot f =
    let forced = ref false in
    fun x ->
      if !forced then raise ForcedTwice;
      forced := true;
      f x

  (* Affine sequences. *)

  open Seq

  let rec affine xs =
    oneshot (fun () ->
      match xs() with
      | Nil ->
          Nil
      | Cons (x, xs) ->
          Cons (x, affine xs)
    )

  let to_option xs =
    match xs() with
    | Nil ->
        None
    | Cons (x, xs) ->
        Some (x, xs)

  (* The composition [affine . List.to_seq]. *)

  let rec list_to_affine_seq (xs : 'a list) : 'a t =
    oneshot (fun () ->
      match xs with
      | [] ->
          Nil
      | x :: xs ->
          Cons (x, list_to_affine_seq xs)
    )

  module ListToAffineSeq = struct

    let appearance =
      support "Seq.list_to_affine_seq"

    let code =
      list_to_affine_seq, appearance

  end

end

(* -------------------------------------------------------------------------- *)

(* This is a variant of affine sequences where it is possible to test at
   runtime whether a sequence is valid (i.e., can still be forced). *)

module VSeq = struct

  type 'a t = {
    force: unit -> 'a node;
    valid: unit -> bool
  }

  and 'a node =
  | Nil
  | Cons of 'a * 'a t

  let valid xs =
    xs.valid()

  exception ForcedTwice

  let oneshot f =
    let forced = ref false in
    let force x =
      if !forced then raise ForcedTwice;
      forced := true;
      f x
    and valid () =
      not !forced
    in
    { force; valid }

  let rec affine (xs : 'a Seq.t) : 'a t =
    oneshot (fun () ->
      match xs() with
      | Seq.Nil ->
          Nil
      | Seq.Cons (x, xs) ->
          Cons (x, affine xs)
    )

  let to_option xs =
    match xs.force() with
    | Nil ->
        None
    | Cons (x, xs) ->
        Some (x, xs)

  let rec forget (xs : 'a t) : 'a Seq.t =
    fun () ->
      match xs.force() with
      | Nil ->
          Seq.Nil
      | Cons (x, xs) ->
          Seq.Cons (x, forget xs)

end