| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Locus |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
let or_var_map f = function |
| 17 |
| ArgArg x -> ArgArg (f x) |
| 18 |
| ArgVar _ as y -> y |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
let occurrences_map f = function |
| 23 |
| OnlyOccurrences l -> |
| 24 |
let l' = f l in |
| 25 |
if l' = [] then NoOccurrences else OnlyOccurrences l' |
| 26 |
| AllOccurrencesBut l -> |
| 27 |
let l' = f l in |
| 28 |
if l' = [] then AllOccurrences else AllOccurrencesBut l' |
| 29 |
| (NoOccurrences|AllOccurrences|AtLeastOneOccurrence) as o -> o |
| 30 |
|
| 31 |
type occurrences_count = {current: int; remaining: int list; where: (bool * int)} |
| 32 |
|
| 33 |
let error_invalid_occurrence l = |
| 34 |
CErrors.user_err Pp.(str ("Invalid occurrence " ^ String.plural (List.length l) "number" ^": ") |
| 35 |
++ prlist_with_sep spc int l ++ str ".") |
| 36 |
|
| 37 |
let initialize_occurrence_counter occs = |
| 38 |
let (nowhere_except_in,occs) = |
| 39 |
match occs with |
| 40 |
| AtLeastOneOccurrence -> (false,[]) |
| 41 |
| AllOccurrences -> (false,[]) |
| 42 |
| AllOccurrencesBut l -> (false,List.sort_uniquize Int.compare l) |
| 43 |
| NoOccurrences -> (true,[]) |
| 44 |
| OnlyOccurrences l -> (true,List.sort_uniquize Int.compare l) in |
| 45 |
let max = |
| 46 |
match occs with |
| 47 |
| n::_ when n <= 0 -> error_invalid_occurrence [n] |
| 48 |
| [] -> 0 |
| 49 |
| _ -> Util.List.last occs in |
| 50 |
{current = 0; remaining = occs; where = (nowhere_except_in,max)} |
| 51 |
|
| 52 |
let update_occurrence_counter {current; remaining; where = (nowhere_except_in,_ as where)} = |
| 53 |
let current = succ current in |
| 54 |
match remaining with |
| 55 |
| occ::remaining when Int.equal current occ -> (nowhere_except_in,{current;remaining;where}) |
| 56 |
| _ -> (not nowhere_except_in,{current;remaining;where}) |
| 57 |
|
| 58 |
let check_used_occurrences {remaining} = |
| 59 |
if not (Util.List.is_empty remaining) then error_invalid_occurrence remaining |
| 60 |
|
| 61 |
let occurrences_done {current; where = (nowhere_except_in,max)} = |
| 62 |
nowhere_except_in && current > max |
| 63 |
|
| 64 |
let current_occurrence {current} = current |
| 65 |
|
| 66 |
let more_specific_occurrences {current; where = (_,max)} = |
| 67 |
current <= max |
| 68 |
|
| 69 |
let is_selected occ = function |
| 70 |
| AtLeastOneOccurrence -> true |
| 71 |
| AllOccurrences -> true |
| 72 |
| AllOccurrencesBut l -> not (Int.List.mem occ l) |
| 73 |
| OnlyOccurrences l -> Int.List.mem occ l |
| 74 |
| NoOccurrences -> false |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
let allHypsAndConcl = { onhyps=None; concl_occs=AllOccurrences } |
| 79 |
let allHyps = { onhyps=None; concl_occs=NoOccurrences } |
| 80 |
let onConcl = { onhyps=Some[]; concl_occs=AllOccurrences } |
| 81 |
let nowhere = { onhyps=Some[]; concl_occs=NoOccurrences } |
| 82 |
let onHyp h = |
| 83 |
{ onhyps=Some[(AllOccurrences,h),InHyp]; concl_occs=NoOccurrences } |
| 84 |
|
| 85 |
let is_nowhere = function |
| 86 |
| { onhyps=Some[]; concl_occs=NoOccurrences } -> true |
| 87 |
| _ -> false |
| 88 |
|
| 89 |
let is_all_occurrences = function |
| 90 |
| AtLeastOneOccurrence |
| 91 |
| AllOccurrences -> true |
| 92 |
| _ -> false |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
let simple_clause_of enum_hyps cl = |
| 99 |
let error_occurrences () = |
| 100 |
CErrors.user_err Pp.(str "This tactic does not support occurrences selection") in |
| 101 |
let error_body_selection () = |
| 102 |
CErrors.user_err Pp.(str "This tactic does not support body selection") in |
| 103 |
let hyps = |
| 104 |
match cl.onhyps with |
| 105 |
| None -> |
| 106 |
List.map Option.make (enum_hyps ()) |
| 107 |
| Some l -> |
| 108 |
List.map (fun ((occs,id),w) -> |
| 109 |
if not (is_all_occurrences occs) then error_occurrences (); |
| 110 |
if w = InHypValueOnly then error_body_selection (); |
| 111 |
Some id) l in |
| 112 |
if cl.concl_occs = NoOccurrences then hyps |
| 113 |
else |
| 114 |
if not (is_all_occurrences cl.concl_occs) then error_occurrences () |
| 115 |
else None :: hyps |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
let concrete_clause_of enum_hyps cl = |
| 120 |
let hyps = |
| 121 |
match cl.onhyps with |
| 122 |
| None -> |
| 123 |
let f id = OnHyp (id,AllOccurrences,InHyp) in |
| 124 |
List.map f (enum_hyps ()) |
| 125 |
| Some l -> |
| 126 |
List.map (fun ((occs,id),w) -> OnHyp (id,occs,w)) l in |
| 127 |
if cl.concl_occs = NoOccurrences then hyps |
| 128 |
else |
| 129 |
OnConcl cl.concl_occs :: hyps |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
let out_arg = function |
| 134 |
| ArgVar _ -> CErrors.anomaly (Pp.str "Unevaluated or_var variable.") |
| 135 |
| ArgArg x -> x |
| 136 |
|
| 137 |
let occurrences_of_hyp id cls = |
| 138 |
let rec hyp_occ = function |
| 139 |
[] -> NoOccurrences, InHyp |
| 140 |
| ((occs,id'),hl)::_ when Names.Id.equal id id' -> |
| 141 |
occurrences_map (List.map out_arg) occs, hl |
| 142 |
| _::l -> hyp_occ l in |
| 143 |
match cls.onhyps with |
| 144 |
None -> AllOccurrences,InHyp |
| 145 |
| Some l -> hyp_occ l |
| 146 |
|
| 147 |
let occurrences_of_goal cls = |
| 148 |
occurrences_map (List.map out_arg) cls.concl_occs |
| 149 |
|
| 150 |
let in_every_hyp cls = Option.is_empty cls.onhyps |
| 151 |
|
| 152 |
let clause_with_generic_occurrences cls = |
| 153 |
let hyps = match cls.onhyps with |
| 154 |
| None -> true |
| 155 |
| Some hyps -> |
| 156 |
List.for_all |
| 157 |
(function ((AllOccurrences,_),_) -> true | _ -> false) hyps in |
| 158 |
let concl = match cls.concl_occs with |
| 159 |
| AtLeastOneOccurrence | AllOccurrences | NoOccurrences -> true |
| 160 |
| _ -> false in |
| 161 |
hyps && concl |
| 162 |
|
| 163 |
let clause_with_generic_context_selection cls = |
| 164 |
let hyps = match cls.onhyps with |
| 165 |
| None -> true |
| 166 |
| Some hyps -> |
| 167 |
List.for_all |
| 168 |
(function ((AllOccurrences,_),InHyp) -> true | _ -> false) hyps in |
| 169 |
let concl = match cls.concl_occs with |
| 170 |
| AtLeastOneOccurrence | AllOccurrences | NoOccurrences -> true |
| 171 |
| _ -> false in |
| 172 |
hyps && concl |