From 46aef7e789424049c13689f4ff1736d77dfc87bc Mon Sep 17 00:00:00 2001 From: Enrico Tassi Date: Thu, 27 Mar 2008 10:50:09 +0000 Subject: [PATCH] added iterators over NCic terms --- .../components/ng_kernel/nCicUtils.ml | 81 +++++++++++++------ .../components/ng_kernel/nCicUtils.mli | 4 + 2 files changed, 61 insertions(+), 24 deletions(-) diff --git a/helm/software/components/ng_kernel/nCicUtils.ml b/helm/software/components/ng_kernel/nCicUtils.ml index 965f1cf2b..5d3304f60 100644 --- a/helm/software/components/ng_kernel/nCicUtils.ml +++ b/helm/software/components/ng_kernel/nCicUtils.ml @@ -1,27 +1,13 @@ -(* Copyright (C) 2000, HELM Team. - * - * This file is part of HELM, an Hypertextual, Electronic - * Library of Mathematics, developed at the Computer Science - * Department, University of Bologna, Italy. - * - * HELM is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * HELM 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with HELM; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, - * MA 02111-1307, USA. - * - * For details, see the HELM World-Wide-Web page, - * http://cs.unibo.it/helm/. - *) +(* + ||M|| This file is part of HELM, an Hypertextual, Electronic + ||A|| Library of Mathematics, developed at the Computer Science + ||T|| Department, University of Bologna, Italy. + ||I|| + ||T|| HELM is free software; you can redistribute it and/or + ||A|| modify it under the terms of the GNU General Public License + \ / version 2 or (at your option) any later version. + \ / This software is distributed as is, NO WARRANTY. + V_______________________________________________________________ *) (* $Id: nCicSubstitution.ml 8135 2008-02-13 15:35:43Z tassi $ *) @@ -42,3 +28,50 @@ let lookup_subst n subst = List.assoc n subst with Not_found -> raise (Subst_not_found n) +let fold f g acc = function + | NCic.Implicit _ + | NCic.Sort _ + | NCic.Const _ + | NCic.Meta _ + | NCic.Rel _ -> acc + | NCic.Appl l -> List.fold_left f acc l + | NCic.Prod (_,s,t) + | NCic.Lambda (_,s,t) -> f (g (f acc s)) t + | NCic.LetIn (_,ty,t,bo) -> f (g (f (f acc ty) t)) bo + | NCic.Match (_,oty,t,pl) -> List.fold_left f (f (f acc oty) t) pl +;; + +let sharing_map f l = + let unchanged = ref true in + let rec aux b = function + | [] as t -> unchanged := b; t + | he::tl -> + let he1 = f he in + he1 :: aux (b && he1 == he) tl + in + let l1 = aux true l in + if !unchanged then l else l1 +;; + +let map f g k = function + | NCic.Implicit _ + | NCic.Sort _ + | NCic.Const _ + | NCic.Meta _ + | NCic.Rel _ as t -> t + | NCic.Appl l -> NCic.Appl (sharing_map (f k) l) + | NCic.Prod (n,s,t) as orig -> + let s1 = f k s in let t1 = f (g k) t in + if t1 == t && s1 == s then orig else NCic.Prod (n,s1,t1) + | NCic.Lambda (n,s,t) as orig -> + let s1 = f k s in let t1 = f (g k) t in + if t1 == t && s1 == s then orig else NCic.Prod (n,s1,t1) + | NCic.LetIn (n,ty,t,b) as orig -> + let ty1 = f k ty in let t1 = f k t in let b1 = f (g k) b in + if ty1 == ty && t1 == t && b1 == b then orig else NCic.LetIn (n,ty1,t1,b1) + | NCic.Match (r,oty,t,pl) as orig -> + let oty1 = f k oty in let t1 = f k t in let pl1 = sharing_map (f k) pl in + if oty1 == oty && t1 == t && pl1 == pl then orig + else NCic.Match(r,oty1,t1,pl1) +;; + diff --git a/helm/software/components/ng_kernel/nCicUtils.mli b/helm/software/components/ng_kernel/nCicUtils.mli index 6af6ac8f0..506af8c0b 100644 --- a/helm/software/components/ng_kernel/nCicUtils.mli +++ b/helm/software/components/ng_kernel/nCicUtils.mli @@ -31,3 +31,7 @@ val expand_local_context : NCic.lc_kind -> NCic.term list val lookup_subst: int -> NCic.substitution -> NCic.subst_entry +(* both Meta agnostic, map attempts to preserve sharing. + * call the 'a->'a function when a binder is crossed *) +val fold: ('a -> NCic.term -> 'a) -> ('a -> 'a) -> 'a -> NCic.term -> 'a +val map: ('a -> NCic.term -> NCic.term) -> ('a -> 'a) -> 'a -> NCic.term -> NCic.term -- 2.39.2