| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
module Trace = struct |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
type 'a forest = 'a tree list |
| 22 |
and 'a tree = Seq of 'a * 'a forest |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
type 'a incr = { head:'a forest ; opened: 'a tree list } |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
let empty_incr = { head=[] ; opened=[] } |
| 40 |
let opn a { head ; opened } = { head ; opened = Seq(a,[])::opened } |
| 41 |
let close { head ; opened } = |
| 42 |
match opened with |
| 43 |
| [a] -> { head = a::head ; opened=[] } |
| 44 |
| a::Seq(b,f)::opened -> { head ; opened=Seq(b,a::f)::opened } |
| 45 |
| [] -> assert false |
| 46 |
let leaf a s = close (opn a s) |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
let rec mirror f = List.rev_map mirror_tree f |
| 53 |
and mirror_tree (Seq(a,f)) = Seq(a,mirror f) |
| 54 |
|
| 55 |
let to_tree = function |
| 56 |
| { head ; opened=[] } -> mirror head |
| 57 |
| { head ; opened=_::_} -> assert false |
| 58 |
|
| 59 |
end |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
type lazy_msg = unit -> Pp.t |
| 68 |
|
| 69 |
|
| 70 |
module Info = struct |
| 71 |
|
| 72 |
|
| 73 |
type tag = |
| 74 |
| Msg of lazy_msg |
| 75 |
| Tactic of lazy_msg |
| 76 |
| Dispatch |
| 77 |
| DBranch |
| 78 |
|
| 79 |
type state = tag Trace.incr |
| 80 |
type tree = tag Trace.forest |
| 81 |
|
| 82 |
let pr_in_comments m = Pp.(str"(* "++ m () ++str" *)") |
| 83 |
|
| 84 |
let unbranch = function |
| 85 |
| Trace.Seq (DBranch,brs) -> brs |
| 86 |
| _ -> assert false |
| 87 |
|
| 88 |
|
| 89 |
let is_empty_branch = let open Trace in function |
| 90 |
| Seq(DBranch,[]) -> true |
| 91 |
| _ -> false |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
let dispatch brs = |
| 97 |
let open Trace in |
| 98 |
if CList.for_all is_empty_branch brs then None |
| 99 |
else Some (Seq(Dispatch,brs)) |
| 100 |
|
| 101 |
let constr = let open Trace in function |
| 102 |
| Dispatch -> dispatch |
| 103 |
| t -> fun br -> Some (Seq(t,br)) |
| 104 |
|
| 105 |
let rec compress_tree = let open Trace in function |
| 106 |
| Seq(t,f) -> constr t (compress f) |
| 107 |
and compress f = |
| 108 |
CList.map_filter compress_tree f |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
let rec pr_tree with_sep = let open Trace in function |
| 113 |
| Seq (Msg m,[]) -> pr_in_comments m |
| 114 |
| Seq (Tactic m,_) -> |
| 115 |
let tail = if with_sep then Pp.str";" else Pp.mt () in |
| 116 |
Pp.(m () ++ tail) |
| 117 |
| Seq (Dispatch,brs) -> |
| 118 |
let tail = if with_sep then Pp.str";" else Pp.mt () in |
| 119 |
Pp.(pr_dispatch brs++tail) |
| 120 |
| Seq (Msg _,_::_) | Seq (DBranch,_) -> assert false |
| 121 |
and pr_dispatch brs = |
| 122 |
let open Pp in |
| 123 |
let brs = List.map unbranch brs in |
| 124 |
match brs with |
| 125 |
| [br] -> pr_forest br |
| 126 |
| _ -> |
| 127 |
let sep () = spc()++str"|"++spc() in |
| 128 |
let branches = prlist_with_sep sep pr_forest brs in |
| 129 |
str"[>"++spc()++branches++spc()++str"]" |
| 130 |
and pr_forest = function |
| 131 |
| [] -> Pp.mt () |
| 132 |
| [tr] -> pr_tree false tr |
| 133 |
| tr::l -> Pp.(pr_tree true tr ++ pr_forest l) |
| 134 |
|
| 135 |
let print _env _sigma f = |
| 136 |
pr_forest (compress f) |
| 137 |
|
| 138 |
let rec collapse_tree n t = |
| 139 |
let open Trace in |
| 140 |
match n , t with |
| 141 |
| 0 , t -> [t] |
| 142 |
| _ , (Seq(Tactic _,[]) as t) -> [t] |
| 143 |
| n , Seq(Tactic _,f) -> collapse (pred n) f |
| 144 |
| n , Seq(Dispatch,brs) -> [Seq(Dispatch, (collapse n brs))] |
| 145 |
| n , Seq(DBranch,br) -> [Seq(DBranch, (collapse n br))] |
| 146 |
| _ , (Seq(Msg _,_) as t) -> [t] |
| 147 |
and collapse n f = |
| 148 |
CList.map_append (collapse_tree n) f |
| 149 |
end |
| 150 |
|
| 151 |
module StateStore = Store.Make() |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
type goal = Evar.t |
| 156 |
type goal_with_state = Evar.t * StateStore.t |
| 157 |
|
| 158 |
let drop_state = fst |
| 159 |
let get_state = snd |
| 160 |
let goal_with_state g s = (g, s) |
| 161 |
let with_empty_state g = (g, StateStore.empty) |
| 162 |
let map_goal_with_state f (g, s) = (f g, s) |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
type proofview = { |
| 167 |
solution : Evd.evar_map; |
| 168 |
comb : goal_with_state list; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
module P = struct |
| 175 |
|
| 176 |
type s = proofview * Environ.env |
| 177 |
|
| 178 |
|
| 179 |
type e = { trace: bool; name : Names.Id.t; poly : PolyFlags.t } |
| 180 |
|
| 181 |
|
| 182 |
type w = bool |
| 183 |
|
| 184 |
let wunit = true |
| 185 |
let wprod b1 b2 = b1 && b2 |
| 186 |
|
| 187 |
type u = Info.state |
| 188 |
|
| 189 |
let uunit = Trace.empty_incr |
| 190 |
|
| 191 |
end |
| 192 |
|
| 193 |
module Logical = Logic_monad.Logical(P) |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
module type State = sig |
| 199 |
type t |
| 200 |
val get : t Logical.t |
| 201 |
val set : t -> unit Logical.t |
| 202 |
val modify : (t->t) -> unit Logical.t |
| 203 |
end |
| 204 |
|
| 205 |
module type Reader = sig |
| 206 |
type t |
| 207 |
val get : t Logical.t |
| 208 |
end |
| 209 |
|
| 210 |
module type Writer = sig |
| 211 |
type t |
| 212 |
val put : t -> unit Logical.t |
| 213 |
end |
| 214 |
|
| 215 |
module Pv : State with type t := proofview = struct |
| 216 |
let get = Logical.(map fst get) |
| 217 |
let set p = Logical.modify (fun (_,e) -> (p,e)) |
| 218 |
let modify f= Logical.modify (fun (p,e) -> (f p,e)) |
| 219 |
end |
| 220 |
|
| 221 |
module Solution : State with type t := Evd.evar_map = struct |
| 222 |
let get = Logical.map (fun {solution} -> solution) Pv.get |
| 223 |
let set s = Pv.modify (fun pv -> { pv with solution = s }) |
| 224 |
let modify f = Pv.modify (fun pv -> { pv with solution = f pv.solution }) |
| 225 |
end |
| 226 |
|
| 227 |
module Comb : State with type t = goal_with_state list = struct |
| 228 |
|
| 229 |
type t = goal_with_state list |
| 230 |
let get = Logical.map (fun {comb} -> comb) Pv.get |
| 231 |
let set c = Pv.modify (fun pv -> { pv with comb = c }) |
| 232 |
let modify f = Pv.modify (fun pv -> { pv with comb = f pv.comb }) |
| 233 |
end |
| 234 |
|
| 235 |
module Env : State with type t := Environ.env = struct |
| 236 |
let get = Logical.(map snd get) |
| 237 |
let set e = Logical.modify (fun (p,_) -> (p,e)) |
| 238 |
let modify f = Logical.modify (fun (p,e) -> (p,f e)) |
| 239 |
end |
| 240 |
|
| 241 |
module Status : Writer with type t := bool = struct |
| 242 |
let put s = Logical.put s |
| 243 |
end |
| 244 |
|
| 245 |
|
| 246 |
module InfoL = struct |
| 247 |
let recording = Logical.(map (fun {P.trace} -> trace) current) |
| 248 |
let if_recording t = |
| 249 |
let open Logical in |
| 250 |
recording >>= fun r -> |
| 251 |
if r then t else return () |
| 252 |
|
| 253 |
let record_trace t = |
| 254 |
Logical.( |
| 255 |
current >>= fun s -> |
| 256 |
local {s with P.trace = true} t) |
| 257 |
|
| 258 |
let raw_update = Logical.update |
| 259 |
let update f = if_recording (raw_update f) |
| 260 |
let opn a = update (Trace.opn a) |
| 261 |
let close = update Trace.close |
| 262 |
let leaf a = update (Trace.leaf a) |
| 263 |
|
| 264 |
let tag a t = |
| 265 |
let open Logical in |
| 266 |
recording >>= fun r -> |
| 267 |
if r then begin |
| 268 |
raw_update (Trace.opn a) >> |
| 269 |
t >>= fun a -> |
| 270 |
raw_update Trace.close >> |
| 271 |
return a |
| 272 |
end else |
| 273 |
t |
| 274 |
end |