| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Proof |
| 12 |
|
| 13 |
type t = |
| 14 |
| Dash of int |
| 15 |
| Star of int |
| 16 |
| Plus of int |
| 17 |
|
| 18 |
let bullet_eq b1 b2 = match b1, b2 with |
| 19 |
| Dash n1, Dash n2 -> n1 = n2 |
| 20 |
| Star n1, Star n2 -> n1 = n2 |
| 21 |
| Plus n1, Plus n2 -> n1 = n2 |
| 22 |
| _ -> false |
| 23 |
|
| 24 |
let pr_bullet b = |
| 25 |
match b with |
| 26 |
| Dash n -> Pp.(str (String.make n '-')) |
| 27 |
| Star n -> Pp.(str (String.make n '*')) |
| 28 |
| Plus n -> Pp.(str (String.make n '+')) |
| 29 |
|
| 30 |
|
| 31 |
type behavior = { |
| 32 |
name : string; |
| 33 |
put : Proof.t -> t -> Proof.t; |
| 34 |
suggest: Proof.t -> Pp.t |
| 35 |
} |
| 36 |
|
| 37 |
let behaviors = Hashtbl.create 4 |
| 38 |
let register_behavior b = Hashtbl.add behaviors b.name b |
| 39 |
|
| 40 |
|
| 41 |
let none = { |
| 42 |
name = "None"; |
| 43 |
put = (fun x _ -> x); |
| 44 |
suggest = (fun _ -> Pp.mt ()) |
| 45 |
} |
| 46 |
let _ = register_behavior none |
| 47 |
|
| 48 |
module Strict = struct |
| 49 |
type suggestion = |
| 50 |
| Suggest of t |
| 51 |
| Unfinished of t |
| 52 |
| NoBulletInUse |
| 53 |
|
| 54 |
| NeedClosingBrace |
| 55 |
| ProofFinished |
| 56 |
|
| 57 |
|
| 58 |
let suggest_on_solved_goal sugg = |
| 59 |
match sugg with |
| 60 |
| NeedClosingBrace -> Pp.(str"Try unfocusing with \"}\".") |
| 61 |
| NoBulletInUse -> Pp.mt () |
| 62 |
| ProofFinished -> Pp.mt () |
| 63 |
| Suggest b -> Pp.(str"Focus next goal with bullet " ++ pr_bullet b ++ str".") |
| 64 |
| Unfinished b -> Pp.(str"The current bullet " ++ pr_bullet b ++ str" is unfinished.") |
| 65 |
|
| 66 |
|
| 67 |
let suggest_on_error sugg = |
| 68 |
match sugg with |
| 69 |
| NeedClosingBrace -> Pp.(str"Try unfocusing with \"}\".") |
| 70 |
| NoBulletInUse -> assert false |
| 71 |
| ProofFinished -> Pp.(str"No more goals.") |
| 72 |
| Suggest b -> Pp.(str"Expecting " ++ pr_bullet b ++ str".") |
| 73 |
| Unfinished b -> Pp.(str"Current bullet " ++ pr_bullet b ++ str" is not finished.") |
| 74 |
|
| 75 |
exception FailedBullet of t * suggestion |
| 76 |
|
| 77 |
let _ = |
| 78 |
CErrors.register_handler |
| 79 |
(function |
| 80 |
| FailedBullet (b,sugg) -> |
| 81 |
let prefix = Pp.(str"Wrong bullet " ++ pr_bullet b ++ str": ") in |
| 82 |
Some Pp.(str "[Focus]" ++ spc () ++ prefix ++ suggest_on_error sugg) |
| 83 |
| _ -> None) |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
let bullet_kind = (new_focus_kind "bullet_kind" : t list focus_kind) |
| 88 |
let bullet_cond = done_cond ~loose_end:true bullet_kind |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
let get_bullets pr = |
| 94 |
if is_last_focus bullet_kind pr then |
| 95 |
get_at_focus bullet_kind pr |
| 96 |
else |
| 97 |
[] |
| 98 |
|
| 99 |
let has_bullet bul pr = |
| 100 |
let rec has_bullet = function |
| 101 |
| b'::_ when bullet_eq bul b' -> true |
| 102 |
| _::l -> has_bullet l |
| 103 |
| [] -> false |
| 104 |
in |
| 105 |
has_bullet (get_bullets pr) |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
let pop pr = |
| 111 |
match get_bullets pr with |
| 112 |
| b::_ -> Some (unfocus bullet_kind pr (), b) |
| 113 |
| _ -> None |
| 114 |
|
| 115 |
let push (b:t) pr = |
| 116 |
focus bullet_cond (b::get_bullets pr) 1 pr |
| 117 |
|
| 118 |
let suggest_bullet (prf : Proof.t): suggestion = |
| 119 |
if is_done prf then ProofFinished |
| 120 |
else if not (no_focused_goal prf) |
| 121 |
then |
| 122 |
match get_bullets prf with |
| 123 |
| b::_ -> Unfinished b |
| 124 |
| _ -> NoBulletInUse |
| 125 |
else |
| 126 |
|
| 127 |
let rec loop prf = |
| 128 |
match pop prf with |
| 129 |
| Some (prf, b) -> |
| 130 |
|
| 131 |
|
| 132 |
begin |
| 133 |
try ignore (push b prf); Suggest b |
| 134 |
with e when CErrors.noncritical e -> |
| 135 |
|
| 136 |
loop prf |
| 137 |
end |
| 138 |
| None -> |
| 139 |
|
| 140 |
|
| 141 |
NeedClosingBrace |
| 142 |
in |
| 143 |
loop prf |
| 144 |
|
| 145 |
let rec pop_until (prf : Proof.t) bul : Proof.t = |
| 146 |
let prf', b = Option.get (pop prf) in |
| 147 |
if bullet_eq bul b then prf' |
| 148 |
else pop_until prf' bul |
| 149 |
|
| 150 |
let put p bul = |
| 151 |
try |
| 152 |
if not (has_bullet bul p) then |
| 153 |
|
| 154 |
|
| 155 |
push bul p |
| 156 |
else |
| 157 |
match suggest_bullet p with |
| 158 |
| Suggest suggested_bullet when bullet_eq bul suggested_bullet |
| 159 |
-> |
| 160 |
let p' = pop_until p bul in |
| 161 |
push bul p' |
| 162 |
|
| 163 |
| sugg -> raise (FailedBullet (bul,sugg)) |
| 164 |
with NoSuchGoals _ -> |
| 165 |
raise (FailedBullet (bul,suggest_bullet p)) |
| 166 |
|
| 167 |
let strict = { |
| 168 |
name = "Strict Subproofs"; |
| 169 |
put = put; |
| 170 |
suggest = (fun prf -> suggest_on_solved_goal (suggest_bullet prf)) |
| 171 |
|
| 172 |
} |
| 173 |
let _ = register_behavior strict |
| 174 |
end |
| 175 |
|
| 176 |
|
| 177 |
let { Goptions.get = current_behavior } = |
| 178 |
Goptions.declare_interpreted_string_option_and_ref |
| 179 |
~key:["Bullet";"Behavior"] |
| 180 |
~value:Strict.strict |
| 181 |
(fun n -> |
| 182 |
try Hashtbl.find behaviors n |
| 183 |
with Not_found -> |
| 184 |
CErrors.user_err Pp.(str ("Unknown bullet behavior: \"" ^ n ^ "\"."))) |
| 185 |
(fun v -> v.name) |
| 186 |
() |
| 187 |
|
| 188 |
let put p b = |
| 189 |
(current_behavior ()).put p b |
| 190 |
|
| 191 |
let suggest p = |
| 192 |
(current_behavior ()).suggest p |
| 193 |
|
| 194 |
let _ = CErrors.register_handler begin function |
| 195 |
| Proof.SuggestNoSuchGoals(n,proof) -> |
| 196 |
let suffix = suggest proof in |
| 197 |
Some (Pp.(str "No such " ++ str (CString.plural n "goal") ++ str "." ++ |
| 198 |
pr_non_empty_arg (fun x -> x) suffix)) |
| 199 |
| _ -> None |
| 200 |
end |