Debian Codegraph @

engine/ftactic.ml

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 open Proofview.Notations
12
13 (** Focussing tactics *)
14
15 type 'a focus =
16 | Uniform of 'a
17 | Depends of 'a list
18
19 (** Type of tactics potentially goal-dependent. If it contains a [Depends],
20 then the length of the inner list is guaranteed to be the number of
21 currently focused goals. Otherwise it means the tactic does not depend
22 on the current set of focused goals. *)
23 type 'a t = 'a focus Proofview.tactic
24
25 let return (x : 'a) : 'a t = Proofview.tclUNIT (Uniform x)
26
27 let bind (type a) (type b) (m : a t) (f : a -> b t) : b t = m >>= function
28 | Uniform x -> f x
29 | Depends l ->
30 let f arg = f arg >>= function
31 | Uniform x ->
32 (* We dispatch the uniform result on each goal under focus, as we know
33 that the [m] argument was actually dependent. *)
34 Proofview.Goal.goals >>= fun goals ->
35 let ans = List.map (fun g -> (g,x)) goals in
36 Proofview.tclUNIT ans
37 | Depends l ->
38 Proofview.Goal.goals >>= fun goals ->
39 Proofview.tclUNIT (List.combine goals l)
40 in
41 (* After the tactic has run, some goals which were previously
42 produced may have been solved by side effects. The values
43 attached to such goals must be discarded, otherwise the list of
44 result would not have the same length as the list of focused
45 goals, which is an invariant of the [Ftactic] module. It is the
46 reason why a goal is attached to each result above. *)
47 let filter (g,x) =
48 g >>= fun g ->
49 Proofview.Goal.unsolved g >>= function
50 | true -> Proofview.tclUNIT (Some x)
51 | false -> Proofview.tclUNIT None
52 in
53 Proofview.tclDISPATCHL (List.map f l) >>= fun l ->
54 Proofview.Monad.List.map_filter filter (List.concat l) >>= fun filtered ->
55 Proofview.tclUNIT (Depends filtered)
56
57 let goals = Proofview.Goal.goals >>= fun l -> Proofview.tclUNIT (Depends l)
58
59 let enter f =
60 bind goals
61 (fun gl -> gl >>= fun gl -> Proofview.wrap_exceptions (fun () -> f gl))
62
63 let with_env t =
64 t >>= function
65 | Uniform a ->
66 Proofview.tclENV >>= fun env -> Proofview.tclUNIT (Uniform (env,a))
67 | Depends l ->
68 Proofview.Goal.goals >>= fun gs ->
69 Proofview.Monad.(List.map (map Proofview.Goal.env) gs) >>= fun envs ->
70 Proofview.tclUNIT (Depends (List.combine envs l))
71
72 let lift (type a) (t:a Proofview.tactic) : a t =
73 Proofview.tclBIND t (fun x -> Proofview.tclUNIT (Uniform x))
74
75 (** If the tactic returns unit, we can focus on the goals if necessary. *)
76 let run m k = m >>= function
77 | Uniform v -> k v
78 | Depends l ->
79 let tacs = List.map k l in
80 Proofview.tclDISPATCH tacs
81
82 let (>>=) = bind
83
84 let (<*>) = fun m n -> bind m (fun () -> n)
85
86 module Self =
87 struct
88 type 'a t = 'a focus Proofview.tactic
89 let return = return
90 let (>>=) = bind
91 let (>>) = (<*>)
92 let map f x = x >>= fun a -> return (f a)
93 end
94
95 module Ftac = Monad.Make(Self)
96 module List = Ftac.List
97
98 module Notations =
99 struct
100 let (>>=) = bind
101 let (<*>) = fun m n -> bind m (fun () -> n)
102 end