ProverScript.ml 17.6 KB
Newer Older
Loïc Correnson's avatar
Loïc Correnson committed
1
2
3
4
(**************************************************************************)
(*                                                                        *)
(*  This file is part of WP plug-in of Frama-C.                           *)
(*                                                                        *)
Andre Maroneze's avatar
Andre Maroneze committed
5
(*  Copyright (C) 2007-2020                                               *)
Loïc Correnson's avatar
Loïc Correnson committed
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
(*    CEA (Commissariat a l'energie atomique et aux energies              *)
(*         alternatives)                                                  *)
(*                                                                        *)
(*  you can redistribute it and/or modify it under the terms of the GNU   *)
(*  Lesser General Public License as published by the Free Software       *)
(*  Foundation, version 2.1.                                              *)
(*                                                                        *)
(*  It is distributed in the hope that it will be useful,                 *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *)
(*  GNU Lesser General Public License for more details.                   *)
(*                                                                        *)
(*  See the GNU Lesser General Public License version 2.1                 *)
(*  for more details (enclosed in the file licenses/LGPLv2.1).            *)
(*                                                                        *)
(**************************************************************************)

open Tactical
open ProofScript

(* -------------------------------------------------------------------------- *)
(* --- Alternatives Ordering                                              --- *)
(* -------------------------------------------------------------------------- *)

module Priority =
struct
  open VCS

  let stage = function
    | Prover( Qed , { verdict = Valid } ) -> 0
François Bobot's avatar
François Bobot committed
36
37
    | Prover( (NativeAltErgo | Why3 _) , { verdict = Valid } ) -> 1
    | Prover( NativeCoq , { verdict = Valid } ) -> 2
Loïc Correnson's avatar
Loïc Correnson committed
38
39
40
41
42
43
44
45
46
47
48
49
    | Tactic _ -> 3
    | Prover _ -> 4
    | Error _ -> 5

  let time = function
    | Tactic _ | Error _ -> 0.0
    | Prover( _ , r ) -> r.prover_time +. r.solver_time

  let compare a b =
    let sa = stage a in
    let sb = stage b in
    if sa = sb
50
    then Stdlib.compare (time a) (time b)
Loïc Correnson's avatar
Loïc Correnson committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    else sa - sb

  let sort script = List.stable_sort compare script

end

(* -------------------------------------------------------------------------- *)
(* --- Running Json-Tactical                                              --- *)
(* -------------------------------------------------------------------------- *)

let jconfigure (console : #Tactical.feedback) jtactic goal =
  let _ , sequent = Wpo.compute goal in
  match ProofScript.configure jtactic sequent with
  | None -> None
  | Some(tactical,selection) ->
      console#set_title "%s" tactical#title ;
      let verdict =
68
        try Lang.local ~pool:console#pool (tactical#select console) selection
Loïc Correnson's avatar
Loïc Correnson committed
69
70
71
72
73
74
75
76
77
78
79
80
        with Not_found | Exit -> Not_applicable
      in
      begin
        match verdict with
        | Applicable process when not console#has_error ->
            let title = tactical#title in
            let script = ProofScript.jtactic ~title tactical selection in
            Some (script , process)
        | _ -> None
      end

let jfork tree ?node jtactic =
81
82
83
  let console = new ProofScript.console
    ~pool:(ProofEngine.pool tree)
    ~title:jtactic.header in
Loïc Correnson's avatar
Loïc Correnson committed
84
85
86
  try
    let anchor = ProofEngine.anchor tree ?node () in
    let goal = ProofEngine.goal anchor in
Loïc Correnson's avatar
Loïc Correnson committed
87
88
    let ctxt = ProofEngine.node_context anchor in
    match WpContext.on_context ctxt (jconfigure console jtactic) goal with
Loïc Correnson's avatar
Loïc Correnson committed
89
90
91
92
    | None -> None
    | Some (script,process) ->
        Some (ProofEngine.fork tree ~anchor script process)
  with
93
  | Exit | Not_found | Invalid_argument _ ->
Loïc Correnson's avatar
Loïc Correnson committed
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
      console#set_error "Can not configure tactic" ; None
  | e ->
      console#set_error "Exception <%s>" (Printexc.to_string e) ;
      raise e

(* -------------------------------------------------------------------------- *)
(* --- Running Alternatives                                               --- *)
(* -------------------------------------------------------------------------- *)

open Task

module Env =
struct

  type t = {
    tree : ProofEngine.tree ;
    valid : bool ; (* play valid provers *)
    failed : bool ; (* play failed provers *)
    provers : VCS.prover list ;
    progress : Wpo.t -> string -> unit ;
    result : Wpo.t -> VCS.prover -> VCS.result -> unit ;
    success : Wpo.t -> VCS.prover option -> unit ;
    depth : int ;
    width : int ;
    auto : Strategy.heuristic list ;
    mutable signaled : bool ;
    mutable backtrack : int ;
    mutable backtracking : backtracking option ;
  }
Loïc Correnson's avatar
Loïc Correnson committed
123

Loïc Correnson's avatar
Loïc Correnson committed
124
125
126
127
128
129
130
131
132
  and backtracking = {
    bk_node : ProofEngine.node ;
    bk_depth : int ; (* depth of search *)
    mutable bk_best : int ;    (* best index, (-1) for none *)
    mutable bk_pending : int ; (* best pending, max_int when none *)
  }

  let tree env = env.tree

133
134
  let play env prv res =
    List.mem prv env.provers &&
Loïc Correnson's avatar
Loïc Correnson committed
135
136
137
138
139
140
141
    if VCS.is_valid res then env.valid else env.failed

  let progress env msg = env.progress (ProofEngine.main env.tree) msg

  let stuck env =
    if not env.signaled then
      begin
142
        ProofEngine.validate ~incomplete:true env.tree ;
Loïc Correnson's avatar
Loïc Correnson committed
143
144
145
146
147
        env.success (ProofEngine.main env.tree) None ;
        env.signaled <- true ;
      end

  let validate ?(finalize=false) env =
148
    ProofEngine.validate ~incomplete:true env.tree ;
Loïc Correnson's avatar
Loïc Correnson committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    if not env.signaled then
      let wpo = ProofEngine.main env.tree in
      let proved = Wpo.is_proved wpo in
      if proved || finalize then
        begin
          env.signaled <- true ;
          List.iter
            (fun (prv,res) -> env.result wpo prv res)
            (Wpo.get_results wpo) ;
          env.success wpo (if proved then Some VCS.Tactical else None)
        end

  let goal env = function
    | Some n -> ProofEngine.goal n
    | None -> ProofEngine.main env.tree

  let prove env wpo ?config prover =
166
    Prover.prove wpo ?config ~mode:VCS.Batch
Loïc Correnson's avatar
Loïc Correnson committed
167
168
      ~progress:env.progress prover

169
  let backtracking env =
Loïc Correnson's avatar
Loïc Correnson committed
170
    match ProofEngine.status env.tree with
171
172
    | `Unproved | `Invalid | `Proved | `Passed -> 0
    | `Pending n | `StillResist n -> n
Loïc Correnson's avatar
Loïc Correnson committed
173
174
175
176
177
178
179
180
181
182
183
184
185

  let setup_backtrack env node depth =
    if env.backtrack > 0 then
      let is_nearer = match env.backtracking with
        | None -> true
        | Some { bk_depth } -> depth < bk_depth in
      if is_nearer then
        let _,hs = ProofEngine.get_strategies node in
        if Array.length hs > 1 then
          env.backtracking <- Some {
              bk_node = node ;
              bk_best = (-1) ;
              bk_depth = depth ;
186
              bk_pending = backtracking env ;
Loïc Correnson's avatar
Loïc Correnson committed
187
            }
Loïc Correnson's avatar
Loïc Correnson committed
188

Loïc Correnson's avatar
Loïc Correnson committed
189
  let search env node ~depth =
190
    if env.auto <> [] && depth < env.depth && backtracking env < env.width
Loïc Correnson's avatar
Loïc Correnson committed
191
192
193
194
195
196
197
198
199
200
201
    then
      match ProverSearch.search env.tree ~anchor:node env.auto with
      | None -> None
      | Some _ as fork -> setup_backtrack env node depth ; fork
    else None

  let backtrack env =
    if env.backtrack <= 0 then None else
      match env.backtracking with
      | None -> None
      | Some point ->
202
          let n = backtracking env in
Loïc Correnson's avatar
Loïc Correnson committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
          let anchor = point.bk_node in
          if n < point.bk_pending then
            begin
              point.bk_best <- fst (ProofEngine.get_strategies anchor) ;
              point.bk_pending <- n ;
            end ;
          match ProverSearch.backtrack env.tree ~anchor ~loop:false () with
          | Some fork ->
              env.backtracking <- None ; Some (point.bk_depth,fork)
          | None -> (* end of backtrack *)
              env.backtracking <- None ;
              match ProverSearch.index env.tree ~anchor ~index:point.bk_best
              with None -> None | Some fork -> Some (point.bk_depth,fork)

  let provers env = env.provers
Loïc Correnson's avatar
Loïc Correnson committed
218

Loïc Correnson's avatar
Loïc Correnson committed
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
  let make tree
      ~valid ~failed ~provers
      ~depth ~width ~backtrack ~auto
      ~progress ~result ~success =
    { tree ; valid ; failed ; provers ;
      depth ; width ; backtrack ; auto ;
      progress ; result ; success ;
      backtracking = None ;
      signaled = false }

end

(* -------------------------------------------------------------------------- *)
(* --- Choosing Alternatives                                              --- *)
(* -------------------------------------------------------------------------- *)

let fst_order _ _ = 0
let key_order (a,_) (b,_) = String.compare a b

let rec zip order nodes scripts =
  match nodes , scripts with
  | _ , [] | [] , _ -> (*TODO: saveback forgiven scripts *) ()
  | node :: o_nodes , script :: o_scripts ->
      let cmp = order node script in
      if cmp < 0 then zip order o_nodes scripts else
      if cmp > 0 then zip order nodes o_scripts else
        (ProofEngine.bind (snd node) (snd script) ;
         zip order o_nodes o_scripts)

let reconcile nodes scripts =
249
250
251
252
  match nodes , scripts with
  | [] , [] -> ()
  | [_,n] , [_,s] -> ProofEngine.bind n s
  | _ ->
Loïc Correnson's avatar
Loïc Correnson committed
253
254
255
256
257
      if List.for_all (fun (k,_) -> k = "") scripts
      then zip fst_order nodes scripts
      else zip key_order
          (List.stable_sort key_order nodes)
          (List.stable_sort key_order scripts)
Loïc Correnson's avatar
Loïc Correnson committed
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

let rec forall phi = function
  | x::xs ->
      phi x >>= fun ok ->
      if ok then forall phi xs else Task.return false
  | [] -> Task.return true

let rec exists phi = function
  | x::xs ->
      phi x >>= fun ok ->
      if ok then Task.return true else exists phi xs
  | [] -> Task.return false

let prove_node env node prv =
  let wpo = Env.goal env (Some node) in
  if not (VCS.is_verdict (Wpo.get_result wpo prv)) then
    Env.prove env wpo prv
  else Task.return false

(* -------------------------------------------------------------------------- *)
(* --- Auto & Seach Mode                                                  --- *)
(* -------------------------------------------------------------------------- *)

let rec auto env ?(depth=0) node : bool Task.task =
  exists (prove_node env node) (Env.provers env) >>= fun ok ->
  if ok then Task.return true else
Loïc Correnson's avatar
Loïc Correnson committed
284
285
286
287
288
289
290
291
292
293
294
295
296
  if depth > 0 then
    autosearch env ~depth node
  else
    begin
      autosearch env ~depth node >>= fun ok ->
      if ok then Task.return true else
        match Env.backtrack env with
        | Some (depth,fork) ->
            Env.progress env "Backtracking" ;
            autofork env ~depth fork
        | None ->
            Task.return false
    end
Loïc Correnson's avatar
Loïc Correnson committed
297
298
299

and autosearch env ~depth node : bool Task.task =
  match Env.search env node ~depth with
Loïc Correnson's avatar
Loïc Correnson committed
300
  | None -> Task.return false
Loïc Correnson's avatar
Loïc Correnson committed
301
302
303
  | Some fork -> autofork env ~depth fork

and autofork env ~depth fork =
304
  let _,children = ProofEngine.commit fork in
305
  let pending = Env.backtracking env in
Loïc Correnson's avatar
Loïc Correnson committed
306
307
308
309
310
311
312
  if pending > 0 then
    begin
      Env.progress env (Printf.sprintf "Auto %d" pending) ;
      let depth = succ depth in
      forall (auto env ~depth) (List.map snd children)
    end
  else
Loïc Correnson's avatar
Loïc Correnson committed
313
314
    ( Env.validate env ; Task.return true )

315
316
317
318
319
320
321
322
323
324
(* -------------------------------------------------------------------------- *)
(* --- Apply Script Tactic                                                --- *)
(* -------------------------------------------------------------------------- *)

let apply env node jtactic subscripts =
  match jfork (Env.tree env) ?node jtactic with
  | None -> failwith "Selector not found"
  | Some fork ->
      let _,children = ProofEngine.commit fork in
      reconcile children subscripts ; (*TODO: saveback forgiven script ? *)
325
326
327
328
      let ok = List.for_all
          (fun (_,node) -> ProofEngine.proved node)
          children in
      if ok then [] else children
329

Loïc Correnson's avatar
Loïc Correnson committed
330
331
332
333
334
(* -------------------------------------------------------------------------- *)
(* --- Script Crawling                                                    --- *)
(* -------------------------------------------------------------------------- *)

let rec crawl env on_child node = function
Loïc Correnson's avatar
Loïc Correnson committed
335

Loïc Correnson's avatar
Loïc Correnson committed
336
337
338
339
340
  | [] ->
      let node = ProofEngine.anchor (Env.tree env) ?node () in
      auto env node >>= fun ok ->
      if ok then Env.validate env else Env.stuck env ;
      Task.return ()
Loïc Correnson's avatar
Loïc Correnson committed
341

Loïc Correnson's avatar
Loïc Correnson committed
342
  | Error(msg,json) :: alternative ->
343
344
      Wp_parameters.warning "@[<hov 2>Script Error: on goal %a@\n%S: %a@]@."
        WpPropId.pretty (Env.goal env node).po_pid
345
        msg Json.pp json ;
Loïc Correnson's avatar
Loïc Correnson committed
346
347
348
349
350
      crawl env on_child node alternative

  | Prover( prv , res ) :: alternative ->
      begin
        let task =
351
          if Env.play env prv res then
Loïc Correnson's avatar
Loïc Correnson committed
352
353
354
355
356
357
358
359
360
361
362
363
364
365
            let wpo = Env.goal env node in
            let config = VCS.configure res in
            Env.prove env wpo ~config prv
          else Task.return false in
        let continue ok =
          if ok
          then (Env.validate env ; Task.return ())
          else crawl env on_child node alternative
        in
        task >>= continue
      end

  | Tactic( _ , jtactic , subscripts ) :: alternative ->
      begin
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
        try
          let residual = apply env node jtactic subscripts in
          if residual = [] then
            Env.validate env
          else
            List.iter (fun (_,n) -> on_child n) residual ;
          Task.return ()
        with exn when Wp_parameters.protect exn ->
          Wp_parameters.warning
            "Script Error: on goal %a@\n\
             can not apply '%s'@\n\
             exception %S@\n\
             @[<hov 2>Params: %a@]@\n\
             @[<hov 2>Select: %a@]@."
            WpPropId.pretty (Env.goal env node).po_pid
            jtactic.tactic
            (Printexc.to_string exn)
            Json.pp jtactic.params
            Json.pp jtactic.select ;
          crawl env on_child node alternative
Loïc Correnson's avatar
Loïc Correnson committed
386
387
388
389
390
391
392
393
394
395
396
397
      end

(* -------------------------------------------------------------------------- *)
(* --- Main Process                                                       --- *)
(* -------------------------------------------------------------------------- *)

let schedule job =
  Task.spawn (ProverTask.server ()) (Task.thread (Task.todo job))

let rec process env node =
  schedule
    begin fun () ->
398
399
400
401
402
      if ProofEngine.proved node then
        ( Env.validate env ; Task.return () )
      else
        let script = Priority.sort (ProofEngine.bound node) in
        crawl env (process env) (Some node) script
Loïc Correnson's avatar
Loïc Correnson committed
403
404
405
406
407
408
409
    end

let task
    ~valid ~failed ~provers
    ~depth ~width ~backtrack ~auto
    ~start ~progress ~result ~success wpo =
  begin fun () ->
410
411
    Prover.simplify ~start ~result wpo >>= fun succeed ->
    if succeed
412
413
    then
      ( success wpo (Some VCS.Qed) ; Task.return ())
414
415
416
417
418
419
420
421
422
423
    else
      let json = ProofSession.load wpo in
      let script = Priority.sort (ProofScript.decode json) in
      let tree = ProofEngine.proof ~main:wpo in
      let env = Env.make tree
          ~valid ~failed ~provers
          ~depth ~width ~backtrack ~auto
          ~progress ~result ~success in
      crawl env (process env) None script >>?
      (fun _ -> ProofEngine.forward tree) ;
Loïc Correnson's avatar
Loïc Correnson committed
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
  end

(* -------------------------------------------------------------------------- *)
(* --- Main Entry Points                                                  --- *)
(* -------------------------------------------------------------------------- *)

type 'a process =
  ?valid:bool -> ?failed:bool -> ?provers:VCS.prover list ->
  ?depth:int -> ?width:int -> ?backtrack:int ->
  ?auto:Strategy.heuristic list ->
  ?start:(Wpo.t -> unit) ->
  ?progress:(Wpo.t -> string -> unit) ->
  ?result:(Wpo.t -> VCS.prover -> VCS.result -> unit) ->
  ?success:(Wpo.t -> VCS.prover option -> unit) ->
  Wpo.t -> 'a

let skip1 _ = ()
let skip2 _ _ = ()
let skip3 _ _ _ = ()

let prove
    ?(valid = true) ?(failed = true) ?(provers = [])
    ?(depth = 0) ?(width = 0) ?(backtrack = 0) ?(auto = [])
    ?(start = skip1) ?(progress = skip2) ?(result = skip3) ?(success = skip2)
    wpo =
  Task.todo (task
               ~valid ~failed ~provers
               ~depth ~width ~backtrack ~auto
               ~start ~progress ~result ~success wpo)

let spawn
    ?(valid = true) ?(failed = true) ?(provers = [])
    ?(depth = 0) ?(width = 0) ?(backtrack = 0) ?(auto = [])
    ?(start = skip1) ?(progress = skip2) ?(result = skip3) ?(success = skip2)
    wpo =
  schedule (task
              ~valid ~failed ~provers
              ~depth ~width ~backtrack ~auto
              ~start ~progress ~result ~success wpo)

let search
    ?(depth = 0) ?(width = 0) ?(backtrack = 0) ?(auto = []) ?(provers = [])
    ?(progress = skip2) ?(result = skip3) ?(success = skip2)
    tree node =
  begin
    let env = Env.make tree
        ~valid:false ~failed:false ~provers
        ~depth ~width ~backtrack ~auto
        ~progress ~result ~success in
    schedule
      begin fun () ->
        autosearch env ~depth:0 node >>=
        fun ok ->
        if ok then Env.validate ~finalize:true env else Env.stuck env ;
        Task.return ()
      end
  end

(* -------------------------------------------------------------------------- *)
(* --- Save Session                                                       --- *)
(* -------------------------------------------------------------------------- *)

let proofs = Hashtbl.create 32
487

Loïc Correnson's avatar
Loïc Correnson committed
488
489
490
491
492
let has_proof wpo =
  let wid = wpo.Wpo.po_gid in
  try Hashtbl.find proofs wid
  with Not_found ->
    if ProofSession.exists wpo then
Loïc Correnson's avatar
Loïc Correnson committed
493
      let ok =
Loïc Correnson's avatar
Loïc Correnson committed
494
495
        try
          let script = ProofScript.decode (ProofSession.load wpo) in
496
          ProofScript.has_proof script
Loïc Correnson's avatar
Loïc Correnson committed
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
        with _ -> false in
      (Hashtbl.add proofs wid ok ; ok)
    else false

let save wpo =
  let script = ProofEngine.script (ProofEngine.proof ~main:wpo) in
  Hashtbl.remove proofs wpo.Wpo.po_gid ;
  ProofSession.save wpo (ProofScript.encode script)

let get wpo =
  match ProofEngine.get wpo with
  | `None -> `None
  | `Proof -> `Proof
  | `Saved -> `Saved
  | `Script -> if has_proof wpo then `Script else `Proof

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