Debian Codegraph @

theories/Ltac2/Lazy.v

raw
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 Require Import Ltac2.Init.
12 Require Ltac2.Ref.
13 Require Ltac2.Control.
14
15 Ltac2 Type 'a lazy_data := [ Value ('a) | Thunk (unit -> 'a) ].
16
17 (** Type of a lazy cell, similar to OCaml's ['a Lazy.t] type. The functions of
18 this module do not have any specific backtracking support, so any function
19 passed to primitives of this module is handled as if it had one success at
20 most (potential other successes are ignored). *)
21 Ltac2 Type 'a t := 'a lazy_data Ref.ref.
22
23 (** [from_val v] creates a new lazy cell storing (already-computed) value [v].
24 Forcing (i.e., using the [force] function on) the produced cell gives back
25 value [v], and never gives an exception. *)
26 Ltac2 from_val (v : 'a) : 'a t :=
27 Ref.ref (Value v).
28
29 (** [from_fun f] creates a new lazy cell from the given thunk [f]. There is no
30 specific support for backtracking in the [Lazy] module, so if [f] has more
31 than one success, only the first one will be considered. *)
32 Ltac2 from_fun (f : unit -> 'a) : 'a t :=
33 Ref.ref (Thunk f).
34
35 (** [is_val r] indicates whether the given lazy cell [r] holds a forced value.
36 In particular, [is_val r] always returns [true] if [r] was created via the
37 [from_val] function. If [r] was created using [from_fun], then [true] will
38 only be returned if the value of [r] was previously forced (e.g., with the
39 [force] function), and if no exception was produced by said forcing. *)
40 Ltac2 is_val (r : 'a t) : bool :=
41 match Ref.get r with
42 | Value _ => true
43 | Thunk _ => false
44 end.
45
46 (** Exception raised in case of a "cyclic" lazy cell. *)
47 Ltac2 Type exn ::= [ Undefined ].
48
49 (** [force r] gives the value represented by the lazy cell [r], which requires
50 forcing a thunk and updating [r] to the produced value if [r] does not yet
51 have a value. Note that if forcing produces an exception, subsequent calls
52 to [force] will immediately yield the same exception (without re-computing
53 the whole thunk). Additionally, the [Undefined] exception is produced (and
54 set to be produced by [r] on subsequent calls to [force]) if [r] relies on
55 its own value for its definition (i.e., if [r] is "cyclic"). *)
56 Ltac2 force (r : 'a t) : 'a :=
57 match Ref.get r with
58 | Value v => v
59 | Thunk f =>
60 Ref.set r (Thunk (fun () => Control.throw Undefined));
61 match Control.case f with
62 | Val (v, _) =>
63 Ref.set r (Value v);
64 v
65 | Err e =>
66 Ref.set r (Thunk (fun () => Control.zero e));
67 Control.zero e
68 end
69 end.
70
71 (** [map f r] is equivalent to [from_fun (fun () => f (force r))]. *)
72 Ltac2 map (f : 'a -> 'b) (r : 'a t) : 'b t :=
73 from_fun (fun () => f (force r)).
74
75 (** [map_val f r] is similar to [map f r], but the function [f] is immediately
76 applied if [r] contains a forced value. If the immediate application gives
77 an exception, then any subsequent forcing of produced lazy cell will raise
78 the same exception. *)
79 Ltac2 map_val (f : 'a -> 'b) (r : 'a t) : 'b t :=
80 match Ref.get r with
81 | Value v =>
82 match Control.case (fun () => f v) with
83 | Val (v, _) => from_val v
84 | Err e => from_fun (fun () => Control.zero e)
85 end
86 | Thunk t => from_fun (fun () => f (t ()))
87 end.
88
89 Module Export Notations.
90 Ltac2 Notation "lazy!" f(thunk(self)) := from_fun f.
91 End Notations.