Debian Codegraph @

engine/logic_monad.mli

raw symbols SCIP
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 (** This file implements the low-level monadic operations used by the
12 tactic monad. The monad is divided into two layers: a non-logical
13 layer which consists in operations which will not (or cannot) be
14 backtracked in case of failure (input/output or persistent state)
15 and a logical layer which handles backtracking, proof
16 manipulation, and any other effect which needs to backtrack. *)
17
18
19 (** {6 Exceptions} *)
20
21
22 (** To help distinguish between exceptions raised by the IO monad from
23 the one used natively by Rocq, the former are wrapped in
24 [Exception]. It is only used internally so that [catch] blocks of
25 the IO monad would only catch exceptions raised by the [raise]
26 function of the IO monad, and not for instance, by system
27 interrupts. Also used in [Proofview] to avoid capturing exception
28 from the IO monad ([Proofview] catches errors in its compatibility
29 layer, and when lifting goal-level expressions). *)
30 exception Exception of exn
31
32 (** This exception is used to signal abortion in [timeout] functions. *)
33 exception Tac_Timeout
34
35 (** This exception is used by the tactics to signal failure by lack of
36 successes, rather than some other exceptions (like system
37 interrupts). *)
38 exception TacticFailure of exn
39
40
41 (** {6 Non-logical layer} *)
42
43 (** The non-logical monad is a simple [unit -> 'a] (i/o) monad. The
44 operations are simple wrappers around corresponding usual
45 operations and require little documentation. *)
46 module NonLogical : sig
47
48 include Monad.S
49
50 val ignore : 'a t -> unit t
51
52 type 'a ref
53
54 val ref : 'a -> 'a ref t
55 (** [Pervasives.(:=)] *)
56
57 val (:=) : 'a ref -> 'a -> unit t
58 (** [Pervasives.(!)] *)
59
60 val (!) : 'a ref -> 'a t
61
62 val read_line : string t
63 val print_char : char -> unit t
64
65 (** Loggers. The buffer is also flushed. *)
66 val print_debug : Pp.t -> unit t
67 val print_notice : Pp.t -> unit t
68 val print_info : Pp.t -> unit t
69
70 (** [Pervasives.raise]. Except that exceptions are wrapped with
71 {!Exception}. *)
72 val raise : Exninfo.iexn -> 'a t
73
74 (** [try ... with ...] but restricted to {!Exception}. *)
75 val catch : 'a t -> (Exninfo.iexn -> 'a t) -> 'a t
76 val timeout : float -> 'a t -> ('a, Exninfo.info) result t
77
78 (** Construct a monadified side-effect. Exceptions raised by the argument are
79 wrapped with {!Exception}. *)
80 val make : (unit -> 'a) -> 'a t
81
82 (** [run] performs effects. *)
83 val run : 'a t -> 'a
84
85 end
86
87
88 (** {6 Logical layer} *)
89
90 (** The logical monad is a backtracking monad on top of which is
91 layered a state monad (which is used to implement all of read/write,
92 read only, and write only effects). The state monad being layered on
93 top of the backtracking monad makes it so that the state is
94 backtracked on failure.
95
96 Backtracking differs from regular exception in that, writing (+)
97 for exception catching and (>>=) for bind, we require the
98 following extra distributivity laws:
99
100 x+(y+z) = (x+y)+z
101
102 zero+x = x
103
104 x+zero = x
105
106 (x+y)>>=k = (x>>=k)+(y>>=k) *)
107
108 (** A view type for the logical monad, which is a form of list, hence
109 we can decompose it with as a list. *)
110 type ('a, 'b, 'e) list_view_ =
111 | Nil of 'e
112 | Cons of 'a * 'b
113
114 type ('a, 'b, 'e) list_view = ('a, 'e -> 'b, 'e) list_view_
115
116 module BackState : sig
117
118 type (+'a, -'i, +'o, 'e) t
119 val return : 'a -> ('a, 's, 's, 'e) t
120 val (>>=) : ('a, 'i, 'm, 'e) t -> ('a -> ('b, 'm, 'o, 'e) t) -> ('b, 'i, 'o, 'e) t
121 val (>>) : (unit, 'i, 'm, 'e) t -> ('b, 'm, 'o, 'e) t -> ('b, 'i, 'o, 'e) t
122 val map : ('a -> 'b) -> ('a, 'i, 'o, 'e) t -> ('b, 'i, 'o, 'e) t
123
124 val ignore : ('a, 'i, 'o, 'e) t -> (unit, 'i, 'o, 'e) t
125
126 val set : 'o -> (unit, 'i, 'o, 'e) t
127 val get : ('s, 's, 's, 'e) t
128 val modify : ('i -> 'o) -> (unit, 'i, 'o, 'e) t
129
130 val interleave : ('e1 -> 'e2) -> ('e2 -> 'e1) -> ('a, 'i, 'o, 'e1) t ->
131 ('a, 'i, 'o, 'e2) t
132 (** [interleave src dst m] adapts the exceptional content of the monad
133 according to the functions [src] and [dst]. To ensure a meaningful result,
134 those functions must form a retraction, i.e. [dst (src e1) = e1] for all
135 [e1]. This is typically the case when the type ['e1] is [unit]. *)
136
137 val zero : 'e -> ('a, 'i, 'o, 'e) t
138 val plus : ('a, 'i, 'o, 'e) t -> ('e -> ('a, 'i, 'o, 'e) t) -> ('a, 'i, 'o, 'e) t
139
140 val split : ('a, 's, 's, 'e) t ->
141 (('a, ('a, 'i, 's, 'e) t, 'e) list_view, 's, 's, 'e) t
142
143 val once : ('a, 'i, 'o, 'e) t -> ('a, 'i, 'o, 'e) t
144 val break : ('e -> 'e option) -> ('a, 'i, 'o, 'e) t -> ('a, 'i, 'o, 'e) t
145 val lift : 'a NonLogical.t -> ('a, 's, 's, 'e) t
146
147 type ('a, 'e) reified
148 type ('a, 'e) reified_
149
150 val repr : ('a, 'e) reified -> ('a, ('a, 'e) reified_, 'e) list_view_ NonLogical.t
151
152 val run : ('a, 'i, 'o, 'e) t -> 'i -> ('a * 'o, 'e) reified
153
154 end
155
156 (** The monad is parametrised in the types of state, environment and
157 writer. *)
158 module type Param = sig
159
160 (** Read only *)
161 type e
162
163 (** Write only *)
164 type w
165
166 (** [w] must be a monoid *)
167 val wunit : w
168 val wprod : w -> w -> w
169
170 (** Read-write *)
171 type s
172
173 (** Update-only. Essentially a writer on [u->u]. *)
174 type u
175
176 (** [u] must be pointed. *)
177 val uunit : u
178
179 end
180
181 module Logical (P:Param) : sig
182
183 include Monad.S
184
185 val ignore : 'a t -> unit t
186
187 val set : P.s -> unit t
188 val get : P.s t
189 val modify : (P.s -> P.s) -> unit t
190 val put : P.w -> unit t
191 val current : P.e t
192 val local : P.e -> 'a t -> 'a t
193 val update : (P.u -> P.u) -> unit t
194
195 val zero : Exninfo.iexn -> 'a t
196 val plus : 'a t -> (Exninfo.iexn -> 'a t) -> 'a t
197 val split : 'a t -> ('a, 'a t, Exninfo.iexn) list_view t
198 val once : 'a t -> 'a t
199 val break : (Exninfo.iexn -> Exninfo.iexn option) -> 'a t -> 'a t
200
201 val lift : 'a NonLogical.t -> 'a t
202
203 type 'a reified = ('a, Exninfo.iexn) BackState.reified
204 type 'a reified_ = ('a, Exninfo.iexn) BackState.reified_
205
206 val repr : 'a reified -> ('a, 'a reified_, Exninfo.iexn) list_view_ NonLogical.t
207
208 val run : 'a t -> P.e -> P.s -> ('a * P.s * P.w * P.u) reified
209
210 module Unsafe :
211 sig
212 type state = {
213 rstate : P.e;
214 ustate : P.u;
215 wstate : P.w;
216 sstate : P.s;
217 }
218
219 val make : ('a, state, state, Exninfo.iexn) BackState.t -> 'a t
220 val repr : 'a t -> ('a, state, state, Exninfo.iexn) BackState.t
221
222 end
223
224 end