From 7cfc4e5146be5666419451bdd516f1f3f264d24a Mon Sep 17 00:00:00 2001 From: Enrico Tassi Date: Sun, 25 Jan 2015 14:42:51 +0100 Subject: Imported Upstream version 8.5~beta1+dfsg --- kernel/context.ml | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 kernel/context.ml (limited to 'kernel/context.ml') diff --git a/kernel/context.ml b/kernel/context.ml new file mode 100644 index 00000000..796f06d3 --- /dev/null +++ b/kernel/context.ml @@ -0,0 +1,137 @@ +(************************************************************************) +(* v * The Coq Proof Assistant / The Coq Development Team *) +(* decl + | n, _ :: sign -> lookup_rel (n-1) sign + | _, [] -> raise Not_found + +let rel_context_length = List.length + +let rel_context_nhyps hyps = + let rec nhyps acc = function + | [] -> acc + | (_,None,_)::hyps -> nhyps (1+acc) hyps + | (_,Some _,_)::hyps -> nhyps acc hyps in + nhyps 0 hyps + +let rel_context_tags ctx = + let rec aux l = function + | [] -> l + | (_,Some _,_)::ctx -> aux (true::l) ctx + | (_,None,_)::ctx -> aux (false::l) ctx + in aux [] ctx + +(*s Signatures of named hypotheses. Used for section variables and + goal assumptions. *) + +type named_context = named_declaration list +type named_list_context = named_list_declaration list + +let empty_named_context = [] + +let add_named_decl d sign = d::sign + +let rec lookup_named id = function + | (id',_,_ as decl) :: _ when Id.equal id id' -> decl + | _ :: sign -> lookup_named id sign + | [] -> raise Not_found + +let named_context_length = List.length +let named_context_equal = List.equal eq_named_declaration + +let vars_of_named_context ctx = + List.fold_left (fun accu (id, _, _) -> Id.Set.add id accu) Id.Set.empty ctx + +let instance_from_named_context sign = + let filter = function + | (id, None, _) -> Some (Constr.mkVar id) + | (_, Some _, _) -> None + in + List.map_filter filter sign + +let fold_named_context f l ~init = List.fold_right f l init +let fold_named_list_context f l ~init = List.fold_right f l init +let fold_named_context_reverse f ~init l = List.fold_left f init l + +(*s Signatures of ordered section variables *) +type section_context = named_context + +let fold_rel_context f l ~init:x = List.fold_right f l x +let fold_rel_context_reverse f ~init:x l = List.fold_left f x l + +let map_context f l = + let map_decl (n, body_o, typ as decl) = + let body_o' = Option.smartmap f body_o in + let typ' = f typ in + if body_o' == body_o && typ' == typ then decl else + (n, body_o', typ') + in + List.smartmap map_decl l + +let map_rel_context = map_context +let map_named_context = map_context + +let iter_rel_context f = List.iter (fun (_,b,t) -> f t; Option.iter f b) +let iter_named_context f = List.iter (fun (_,b,t) -> f t; Option.iter f b) -- cgit v1.2.3