eliminated some old folds;
authorwenzelm
Thu Oct 29 23:56:33 2009 +0100 (2009-10-29)
changeset 33339d41f77196338
parent 33338 de76079f973a
child 33347 6748bd742d7a
eliminated some old folds;
src/FOLP/simp.ML
src/HOL/Import/lazy_seq.ML
src/HOL/Import/proof_kernel.ML
src/HOL/Library/Sum_Of_Squares/positivstellensatz_tools.ML
src/HOL/Tools/Function/size.ML
src/HOL/Tools/TFL/post.ML
src/HOL/Tools/TFL/rules.ML
src/HOL/Tools/TFL/tfl.ML
src/HOL/Tools/choice_specification.ML
src/HOL/Tools/lin_arith.ML
src/HOL/Tools/meson.ML
src/HOL/Tools/metis_tools.ML
src/HOL/Tools/refute.ML
src/HOL/Tools/res_axioms.ML
src/HOL/Tools/simpdata.ML
src/Provers/classical.ML
src/Provers/typedsimp.ML
src/ZF/Tools/datatype_package.ML
     1.1 --- a/src/FOLP/simp.ML	Thu Oct 29 23:49:55 2009 +0100
     1.2 +++ b/src/FOLP/simp.ML	Thu Oct 29 23:56:33 2009 +0100
     1.3 @@ -66,7 +66,7 @@
     1.4  fun eq_brl ((b1 : bool, th1), (b2, th2)) = b1 = b2 andalso Thm.eq_thm_prop (th1, th2);
     1.5  
     1.6  (*insert a thm in a discrimination net by its lhs*)
     1.7 -fun lhs_insert_thm (th,net) =
     1.8 +fun lhs_insert_thm th net =
     1.9      Net.insert_term eq_brl (lhs_of (concl_of th), (false,th)) net
    1.10      handle  Net.INSERT => net;
    1.11  
    1.12 @@ -434,7 +434,7 @@
    1.13          val thms = map (trivial o cterm_of(Thm.theory_of_thm thm)) As;
    1.14          val new_rws = maps mk_rew_rules thms;
    1.15          val rwrls = map mk_trans (maps mk_rew_rules thms);
    1.16 -        val anet' = List.foldr lhs_insert_thm anet rwrls
    1.17 +        val anet' = fold_rev lhs_insert_thm rwrls anet;
    1.18      in  if !tracing andalso not(null new_rws)
    1.19          then writeln (cat_lines
    1.20            ("Adding rewrites:" :: map Display.string_of_thm_without_context new_rws))
     2.1 --- a/src/HOL/Import/lazy_seq.ML	Thu Oct 29 23:49:55 2009 +0100
     2.2 +++ b/src/HOL/Import/lazy_seq.ML	Thu Oct 29 23:56:33 2009 +0100
     2.3 @@ -407,8 +407,8 @@
     2.4          make (fn () => copy (f x))
     2.5      end
     2.6  
     2.7 -fun EVERY fs = List.foldr (op THEN) succeed fs
     2.8 -fun FIRST fs = List.foldr (op ORELSE) fail fs
     2.9 +fun EVERY fs = fold_rev (curry op THEN) fs succeed
    2.10 +fun FIRST fs = fold_rev (curry op ORELSE) fs fail
    2.11  
    2.12  fun TRY f x =
    2.13      make (fn () =>
     3.1 --- a/src/HOL/Import/proof_kernel.ML	Thu Oct 29 23:49:55 2009 +0100
     3.2 +++ b/src/HOL/Import/proof_kernel.ML	Thu Oct 29 23:56:33 2009 +0100
     3.3 @@ -776,7 +776,7 @@
     3.4                  val (c,asl) = case terms of
     3.5                                    [] => raise ERR "x2p" "Bad oracle description"
     3.6                                  | (hd::tl) => (hd,tl)
     3.7 -                val tg = List.foldr (fn (oracle,tg) => Tag.merge (Tag.read oracle) tg) Tag.empty_tag ors
     3.8 +                val tg = fold_rev (Tag.merge o Tag.read) ors Tag.empty_tag
     3.9              in
    3.10                  mk_proof (POracle(tg,map xml_to_term asl,xml_to_term c))
    3.11              end
    3.12 @@ -1160,7 +1160,7 @@
    3.13          | replace_name n' _ = ERR "replace_name"
    3.14        (* map: old or fresh name -> old free,
    3.15           invmap: old free which has fresh name assigned to it -> fresh name *)
    3.16 -      fun dis (v, mapping as (map,invmap)) =
    3.17 +      fun dis v (mapping as (map,invmap)) =
    3.18            let val n = name_of v in
    3.19              case Symtab.lookup map n of
    3.20                NONE => (Symtab.update (n, v) map, invmap)
    3.21 @@ -1179,11 +1179,11 @@
    3.22        else
    3.23          let
    3.24            val (_, invmap) =
    3.25 -              List.foldl dis (Symtab.empty, Termtab.empty) frees
    3.26 -          fun make_subst ((oldfree, newname), (intros, elims)) =
    3.27 +              fold dis frees (Symtab.empty, Termtab.empty)
    3.28 +          fun make_subst (oldfree, newname) (intros, elims) =
    3.29                (cterm_of thy oldfree :: intros,
    3.30                 cterm_of thy (replace_name newname oldfree) :: elims)
    3.31 -          val (intros, elims) = List.foldl make_subst ([], []) (Termtab.dest invmap)
    3.32 +          val (intros, elims) = fold make_subst (Termtab.dest invmap) ([], [])
    3.33          in
    3.34            forall_elim_list elims (forall_intr_list intros thm)
    3.35          end
    3.36 @@ -1837,7 +1837,7 @@
    3.37                        | inst_type ty1 ty2 (ty as Type(name,tys)) =
    3.38                          Type(name,map (inst_type ty1 ty2) tys)
    3.39                  in
    3.40 -                    List.foldr (fn (v,th) =>
    3.41 +                    fold_rev (fn v => fn th =>
    3.42                                let
    3.43                                    val cdom = fst (dom_rng (fst (dom_rng cty)))
    3.44                                    val vty  = type_of v
    3.45 @@ -1845,11 +1845,11 @@
    3.46                                    val cc = cterm_of thy (Const(cname,newcty))
    3.47                                in
    3.48                                    mk_COMB (mk_REFL cc) (mk_ABS v th thy) thy
    3.49 -                              end) th vlist'
    3.50 +                              end) vlist' th
    3.51                  end
    3.52                | SOME _ => raise ERR "GEN_ABS" "Bad constant"
    3.53                | NONE =>
    3.54 -                List.foldr (fn (v,th) => mk_ABS v th thy) th vlist'
    3.55 +                fold_rev (fn v => fn th => mk_ABS v th thy) vlist' th
    3.56          val res = HOLThm(rens_of info',th1)
    3.57          val _ = message "RESULT:"
    3.58          val _ = if_debug pth res
    3.59 @@ -2020,8 +2020,8 @@
    3.60                                 Sign.add_consts_i (map (fn (c, T, mx) => (Binding.name c, T, mx)) consts) thy'
    3.61                             end
    3.62  
    3.63 -            val thy1 = List.foldr (fn(name,thy)=>
    3.64 -                                snd (get_defname thyname name thy)) thy1 names
    3.65 +            val thy1 = fold_rev (fn name => fn thy =>
    3.66 +                                snd (get_defname thyname name thy)) names thy1
    3.67              fun new_name name = fst (get_defname thyname name thy1)
    3.68              val names' = map (fn name => (new_name name,name,false)) names
    3.69              val (thy',res) = Choice_Specification.add_specification NONE
    3.70 @@ -2041,12 +2041,12 @@
    3.71                       then quotename name
    3.72                       else (quotename newname) ^ ": " ^ (quotename name),thy')
    3.73                  end
    3.74 -            val (new_names,thy') = List.foldr (fn(name,(names,thy)) =>
    3.75 +            val (new_names,thy') = fold_rev (fn name => fn (names, thy) =>
    3.76                                              let
    3.77                                                  val (name',thy') = handle_const (name,thy)
    3.78                                              in
    3.79                                                  (name'::names,thy')
    3.80 -                                            end) ([],thy') names
    3.81 +                                            end) names ([], thy')
    3.82              val thy'' = add_dump ("specification (" ^ (spaces new_names) ^ ") " ^ thmname ^ ": " ^ (smart_string_of_thm th) ^
    3.83                                    "\n  by (import " ^ thyname ^ " " ^ thmname ^ ")")
    3.84                                   thy'
     4.1 --- a/src/HOL/Library/Sum_Of_Squares/positivstellensatz_tools.ML	Thu Oct 29 23:49:55 2009 +0100
     4.2 +++ b/src/HOL/Library/Sum_Of_Squares/positivstellensatz_tools.ML	Thu Oct 29 23:56:33 2009 +0100
     4.3 @@ -100,7 +100,7 @@
     4.4    (fn (x, k) => (cterm_of (ProofContext.theory_of ctxt) (Free (x, @{typ real})), k)) 
     4.5  
     4.6  fun parse_monomial ctxt = repeat_sep "*" (parse_varpow ctxt) >>
     4.7 -  List.foldl (uncurry FuncUtil.Ctermfunc.update) FuncUtil.Ctermfunc.empty
     4.8 +  (fn xs => fold FuncUtil.Ctermfunc.update xs FuncUtil.Ctermfunc.empty)
     4.9  
    4.10  fun parse_cmonomial ctxt =
    4.11    rat_int --| str "*" -- (parse_monomial ctxt) >> swap ||
    4.12 @@ -108,7 +108,7 @@
    4.13    rat_int >> (fn r => (FuncUtil.Ctermfunc.empty, r))
    4.14  
    4.15  fun parse_poly ctxt = repeat_sep "+" (parse_cmonomial ctxt) >>
    4.16 -  List.foldl (uncurry FuncUtil.Monomialfunc.update) FuncUtil.Monomialfunc.empty
    4.17 +  (fn xs => fold FuncUtil.Monomialfunc.update xs FuncUtil.Monomialfunc.empty)
    4.18  
    4.19  (* positivstellensatz parser *)
    4.20  
     5.1 --- a/src/HOL/Tools/Function/size.ML	Thu Oct 29 23:49:55 2009 +0100
     5.2 +++ b/src/HOL/Tools/Function/size.ML	Thu Oct 29 23:56:33 2009 +0100
     5.3 @@ -115,7 +115,7 @@
     5.4            then HOLogic.zero
     5.5            else foldl1 plus (ts @ [HOLogic.Suc_zero])
     5.6        in
     5.7 -        List.foldr (fn (T, t') => Abs ("x", T, t')) t (Ts @ replicate k HOLogic.natT)
     5.8 +        fold_rev (fn T => fn t' => Abs ("x", T, t')) (Ts @ replicate k HOLogic.natT) t
     5.9        end;
    5.10  
    5.11      val fs = maps (fn (_, (name, _, constrs)) =>
     6.1 --- a/src/HOL/Tools/TFL/post.ML	Thu Oct 29 23:49:55 2009 +0100
     6.2 +++ b/src/HOL/Tools/TFL/post.ML	Thu Oct 29 23:56:33 2009 +0100
     6.3 @@ -28,7 +28,7 @@
     6.4   *--------------------------------------------------------------------------*)
     6.5  fun termination_goals rules =
     6.6      map (Type.freeze o HOLogic.dest_Trueprop)
     6.7 -      (List.foldr (fn (th,A) => uncurry (union (op aconv)) (prems_of th, A)) [] rules);
     6.8 +      (fold_rev (union (op aconv) o prems_of) rules []);
     6.9  
    6.10  (*---------------------------------------------------------------------------
    6.11   * Three postprocessors are applied to the definition.  It
     7.1 --- a/src/HOL/Tools/TFL/rules.ML	Thu Oct 29 23:49:55 2009 +0100
     7.2 +++ b/src/HOL/Tools/TFL/rules.ML	Thu Oct 29 23:56:33 2009 +0100
     7.3 @@ -131,8 +131,7 @@
     7.4  
     7.5  fun FILTER_DISCH_ALL P thm =
     7.6   let fun check tm = P (#t (Thm.rep_cterm tm))
     7.7 - in  List.foldr (fn (tm,th) => if check tm then DISCH tm th else th)
     7.8 -              thm (chyps thm)
     7.9 + in  fold_rev (fn tm => fn th => if check tm then DISCH tm th else th) (chyps thm) thm
    7.10   end;
    7.11  
    7.12  (* freezeT expensive! *)
     8.1 --- a/src/HOL/Tools/TFL/tfl.ML	Thu Oct 29 23:49:55 2009 +0100
     8.2 +++ b/src/HOL/Tools/TFL/tfl.ML	Thu Oct 29 23:56:33 2009 +0100
     8.3 @@ -529,7 +529,7 @@
     8.4                   then writeln (cat_lines ("Extractants =" ::
     8.5                    map (Display.string_of_thm_global thy) extractants))
     8.6                   else ()
     8.7 -     val TCs = List.foldr (uncurry (union (op aconv))) [] TCl
     8.8 +     val TCs = fold_rev (union (op aconv)) TCl []
     8.9       val full_rqt = WFR::TCs
    8.10       val R' = S.mk_select{Bvar=R1, Body=S.list_mk_conj full_rqt}
    8.11       val R'abs = S.rand R'
     9.1 --- a/src/HOL/Tools/choice_specification.ML	Thu Oct 29 23:49:55 2009 +0100
     9.2 +++ b/src/HOL/Tools/choice_specification.ML	Thu Oct 29 23:56:33 2009 +0100
     9.3 @@ -120,7 +120,8 @@
     9.4                  val frees = OldTerm.term_frees prop
     9.5                  val _ = forall (fn v => Sign.of_sort thy (type_of v,HOLogic.typeS)) frees
     9.6                    orelse error "Specificaton: Only free variables of sort 'type' allowed"
     9.7 -                val prop_closed = List.foldr (fn ((vname,T),prop) => HOLogic.mk_all (vname,T,prop)) prop (map dest_Free frees)
     9.8 +                val prop_closed = fold_rev (fn (vname, T) => fn prop =>
     9.9 +                  HOLogic.mk_all (vname, T, prop)) (map dest_Free frees) prop
    9.10              in
    9.11                  (prop_closed,frees)
    9.12              end
    9.13 @@ -151,7 +152,7 @@
    9.14                    | _ => error ("Specification: Several variations of \"" ^ Syntax.string_of_term_global thy c ^ "\" found (try applying explicit type constraints)")
    9.15              end
    9.16          val proc_consts = map proc_const consts
    9.17 -        fun mk_exist (c,prop) =
    9.18 +        fun mk_exist c prop =
    9.19              let
    9.20                  val T = type_of c
    9.21                  val cname = Long_Name.base_name (fst (dest_Const c))
    9.22 @@ -161,7 +162,7 @@
    9.23              in
    9.24                  HOLogic.exists_const T $ Abs(vname,T,Term.abstract_over (c,prop))
    9.25              end
    9.26 -        val ex_prop = List.foldr mk_exist prop proc_consts
    9.27 +        val ex_prop = fold_rev mk_exist proc_consts prop
    9.28          val cnames = map (fst o dest_Const) proc_consts
    9.29          fun post_process (arg as (thy,thm)) =
    9.30              let
    10.1 --- a/src/HOL/Tools/lin_arith.ML	Thu Oct 29 23:49:55 2009 +0100
    10.2 +++ b/src/HOL/Tools/lin_arith.ML	Thu Oct 29 23:56:33 2009 +0100
    10.3 @@ -645,9 +645,9 @@
    10.4  
    10.5  fun filter_prems_tac_items (p : term -> bool) (terms : term list) : term list =
    10.6  let
    10.7 -  fun filter_prems (t, (left, right)) =
    10.8 -    if  p t  then  (left, right @ [t])  else  (left @ right, [])
    10.9 -  val (left, right) = List.foldl filter_prems ([], []) terms
   10.10 +  fun filter_prems t (left, right) =
   10.11 +    if p t then (left, right @ [t]) else (left @ right, [])
   10.12 +  val (left, right) = fold filter_prems terms ([], [])
   10.13  in
   10.14    right @ left
   10.15  end;
    11.1 --- a/src/HOL/Tools/meson.ML	Thu Oct 29 23:49:55 2009 +0100
    11.2 +++ b/src/HOL/Tools/meson.ML	Thu Oct 29 23:56:33 2009 +0100
    11.3 @@ -432,7 +432,7 @@
    11.4  
    11.5  (*Generate Horn clauses for all contrapositives of a clause. The input, th,
    11.6    is a HOL disjunction.*)
    11.7 -fun add_contras crules (th,hcs) =
    11.8 +fun add_contras crules th hcs =
    11.9    let fun rots (0,th) = hcs
   11.10          | rots (k,th) = zero_var_indexes (make_horn crules th) ::
   11.11                          rots(k-1, assoc_right (th RS disj_comm))
   11.12 @@ -443,9 +443,9 @@
   11.13  
   11.14  (*Use "theorem naming" to label the clauses*)
   11.15  fun name_thms label =
   11.16 -    let fun name1 (th, (k,ths)) =
   11.17 +    let fun name1 th (k, ths) =
   11.18            (k-1, Thm.put_name_hint (label ^ string_of_int k) th :: ths)
   11.19 -    in  fn ths => #2 (List.foldr name1 (length ths, []) ths)  end;
   11.20 +    in  fn ths => #2 (fold_rev name1 ths (length ths, []))  end;
   11.21  
   11.22  (*Is the given disjunction an all-negative support clause?*)
   11.23  fun is_negative th = forall (not o #1) (literals (prop_of th));
   11.24 @@ -491,9 +491,9 @@
   11.25      TRYALL_eq_assume_tac;
   11.26  
   11.27  (*Sums the sizes of the subgoals, ignoring hypotheses (ancestors)*)
   11.28 -fun addconcl(prem,sz) = size_of_term(Logic.strip_assums_concl prem) + sz
   11.29 +fun addconcl prem sz = size_of_term (Logic.strip_assums_concl prem) + sz;
   11.30  
   11.31 -fun size_of_subgoals st = List.foldr addconcl 0 (prems_of st);
   11.32 +fun size_of_subgoals st = fold_rev addconcl (prems_of st) 0;
   11.33  
   11.34  
   11.35  (*Negation Normal Form*)
   11.36 @@ -553,19 +553,19 @@
   11.37         (trace_msg (fn () => "Failed to Skolemize " ^ Display.string_of_thm ctxt th);
   11.38          skolemize_nnf_list ctxt ths);
   11.39  
   11.40 -fun add_clauses (th,cls) =
   11.41 +fun add_clauses th cls =
   11.42    let val ctxt0 = Variable.thm_context th
   11.43 -      val (cnfs,ctxt) = make_cnf [] th ctxt0
   11.44 +      val (cnfs, ctxt) = make_cnf [] th ctxt0
   11.45    in Variable.export ctxt ctxt0 cnfs @ cls end;
   11.46  
   11.47  (*Make clauses from a list of theorems, previously Skolemized and put into nnf.
   11.48    The resulting clauses are HOL disjunctions.*)
   11.49 -fun make_clauses ths = sort_clauses (List.foldr add_clauses [] ths);
   11.50 +fun make_clauses ths = sort_clauses (fold_rev add_clauses ths []);
   11.51  
   11.52  (*Convert a list of clauses (disjunctions) to Horn clauses (contrapositives)*)
   11.53  fun make_horns ths =
   11.54      name_thms "Horn#"
   11.55 -      (distinct Thm.eq_thm_prop (List.foldr (add_contras clause_rules) [] ths));
   11.56 +      (distinct Thm.eq_thm_prop (fold_rev (add_contras clause_rules) ths []));
   11.57  
   11.58  (*Could simply use nprems_of, which would count remaining subgoals -- no
   11.59    discrimination as to their size!  With BEST_FIRST, fails for problem 41.*)
    12.1 --- a/src/HOL/Tools/metis_tools.ML	Thu Oct 29 23:49:55 2009 +0100
    12.2 +++ b/src/HOL/Tools/metis_tools.ML	Thu Oct 29 23:56:33 2009 +0100
    12.3 @@ -628,15 +628,14 @@
    12.4          | set_mode FT = FT
    12.5        val mode = set_mode mode0
    12.6        (*transform isabelle clause to metis clause *)
    12.7 -      fun add_thm is_conjecture (ith, {axioms, tfrees}) : logic_map =
    12.8 +      fun add_thm is_conjecture ith {axioms, tfrees} : logic_map =
    12.9          let val (mth, tfree_lits) = hol_thm_to_fol is_conjecture ctxt mode ith
   12.10          in
   12.11             {axioms = (mth, Meson.make_meta_clause ith) :: axioms,
   12.12              tfrees = union (op =) tfree_lits tfrees}
   12.13          end;
   12.14 -      val lmap0 = List.foldl (add_thm true)
   12.15 -                        {axioms = [], tfrees = init_tfrees ctxt} cls
   12.16 -      val lmap = List.foldl (add_thm false) (add_tfrees lmap0) ths
   12.17 +      val lmap0 = fold (add_thm true) cls {axioms = [], tfrees = init_tfrees ctxt}
   12.18 +      val lmap = fold (add_thm false) ths (add_tfrees lmap0)
   12.19        val clause_lists = map (Metis.Thm.clause o #1) (#axioms lmap)
   12.20        fun used c = exists (Metis.LiteralSet.exists (const_in_metis c o #2)) clause_lists
   12.21        (*Now check for the existence of certain combinators*)
   12.22 @@ -647,7 +646,7 @@
   12.23        val thS   = if used "c_COMBS" then [comb_S] else []
   12.24        val thEQ  = if used "c_fequal" then [fequal_imp_equal', equal_imp_fequal'] else []
   12.25        val lmap' = if mode=FO then lmap
   12.26 -                  else List.foldl (add_thm false) lmap (thEQ @ thS @ thC @ thB @ thK @ thI)
   12.27 +                  else fold (add_thm false) (thEQ @ thS @ thC @ thB @ thK @ thI) lmap
   12.28    in
   12.29        (mode, add_type_thm (type_ext thy (map prop_of (cls @ ths))) lmap')
   12.30    end;
    13.1 --- a/src/HOL/Tools/refute.ML	Thu Oct 29 23:49:55 2009 +0100
    13.2 +++ b/src/HOL/Tools/refute.ML	Thu Oct 29 23:56:33 2009 +0100
    13.3 @@ -954,7 +954,7 @@
    13.4              (* required for mutually recursive datatypes; those need to   *)
    13.5              (* be added even if they are an instance of an otherwise non- *)
    13.6              (* recursive datatype                                         *)
    13.7 -            fun collect_dtyp (d, acc) =
    13.8 +            fun collect_dtyp d acc =
    13.9              let
   13.10                val dT = typ_of_dtyp descr typ_assoc d
   13.11              in
   13.12 @@ -962,7 +962,7 @@
   13.13                  DatatypeAux.DtTFree _ =>
   13.14                  collect_types dT acc
   13.15                | DatatypeAux.DtType (_, ds) =>
   13.16 -                collect_types dT (List.foldr collect_dtyp acc ds)
   13.17 +                collect_types dT (fold_rev collect_dtyp ds acc)
   13.18                | DatatypeAux.DtRec i =>
   13.19                  if dT mem acc then
   13.20                    acc  (* prevent infinite recursion *)
   13.21 @@ -976,9 +976,9 @@
   13.22                          insert (op =) dT acc
   13.23                        else acc
   13.24                      (* collect argument types *)
   13.25 -                    val acc_dtyps = List.foldr collect_dtyp acc_dT dtyps
   13.26 +                    val acc_dtyps = fold_rev collect_dtyp dtyps acc_dT
   13.27                      (* collect constructor types *)
   13.28 -                    val acc_dconstrs = List.foldr collect_dtyp acc_dtyps (maps snd dconstrs)
   13.29 +                    val acc_dconstrs = fold_rev collect_dtyp (maps snd dconstrs) acc_dtyps
   13.30                    in
   13.31                      acc_dconstrs
   13.32                    end
   13.33 @@ -986,7 +986,7 @@
   13.34            in
   13.35              (* argument types 'Ts' could be added here, but they are also *)
   13.36              (* added by 'collect_dtyp' automatically                      *)
   13.37 -            collect_dtyp (DatatypeAux.DtRec index, acc)
   13.38 +            collect_dtyp (DatatypeAux.DtRec index) acc
   13.39            end
   13.40          | NONE =>
   13.41            (* not an inductive datatype, e.g. defined via "typedef" or *)
   13.42 @@ -1596,8 +1596,9 @@
   13.43      val Ts = Term.binder_types (Term.fastype_of t)
   13.44      val t' = Term.incr_boundvars i t
   13.45    in
   13.46 -    List.foldr (fn (T, term) => Abs ("<eta_expand>", T, term))
   13.47 -      (Term.list_comb (t', map Bound (i-1 downto 0))) (List.take (Ts, i))
   13.48 +    fold_rev (fn T => fn term => Abs ("<eta_expand>", T, term))
   13.49 +      (List.take (Ts, i))
   13.50 +      (Term.list_comb (t', map Bound (i-1 downto 0)))
   13.51    end;
   13.52  
   13.53  (* ------------------------------------------------------------------------- *)
   13.54 @@ -2058,7 +2059,7 @@
   13.55              Const (@{const_name insert}, HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
   13.56          in
   13.57            (* functions as graphs, i.e. as a (HOL) set of pairs "(x, y)" *)
   13.58 -          map (List.foldr (fn (pair, acc) => HOLogic_insert $ pair $ acc)
   13.59 +          map (fn ps => fold_rev (fn pair => fn acc => HOLogic_insert $ pair $ acc) ps
   13.60              HOLogic_empty_set) pairss
   13.61          end
   13.62        | Type (s, Ts) =>
   13.63 @@ -2590,8 +2591,8 @@
   13.64                          (* interpretation list *)
   13.65                          val arg_intrs = map (uncurry rec_intr) rec_dtyps_intrs
   13.66                          (* apply 'intr' to all recursive arguments *)
   13.67 -                        val result = List.foldl (fn (arg_i, i) =>
   13.68 -                          interpretation_apply (i, arg_i)) intr arg_intrs
   13.69 +                        val result = fold (fn arg_i => fn i =>
   13.70 +                          interpretation_apply (i, arg_i)) arg_intrs intr
   13.71                          (* update 'REC_OPERATORS' *)
   13.72                          val _ = Array.update (arr, elem, (true, result))
   13.73                        in
   13.74 @@ -2970,11 +2971,11 @@
   13.75              "intersection: interpretation for set is not a node")
   13.76          (* interpretation -> interpretaion *)
   13.77          fun lfp (Node resultsets) =
   13.78 -          List.foldl (fn ((set, resultset), acc) =>
   13.79 +          fold (fn (set, resultset) => fn acc =>
   13.80              if is_subset (resultset, set) then
   13.81                intersection (acc, set)
   13.82              else
   13.83 -              acc) i_univ (i_sets ~~ resultsets)
   13.84 +              acc) (i_sets ~~ resultsets) i_univ
   13.85            | lfp _ =
   13.86              raise REFUTE ("lfp_interpreter",
   13.87                "lfp: interpretation for function is not a node")
   13.88 @@ -3025,11 +3026,11 @@
   13.89              "union: interpretation for set is not a node")
   13.90          (* interpretation -> interpretaion *)
   13.91          fun gfp (Node resultsets) =
   13.92 -          List.foldl (fn ((set, resultset), acc) =>
   13.93 +          fold (fn (set, resultset) => fn acc =>
   13.94              if is_subset (set, resultset) then
   13.95                union (acc, set)
   13.96              else
   13.97 -              acc) i_univ (i_sets ~~ resultsets)
   13.98 +              acc) (i_sets ~~ resultsets) i_univ
   13.99            | gfp _ =
  13.100              raise REFUTE ("gfp_interpreter",
  13.101                "gfp: interpretation for function is not a node")
  13.102 @@ -3127,8 +3128,7 @@
  13.103          val HOLogic_insert    =
  13.104            Const (@{const_name insert}, HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  13.105        in
  13.106 -        SOME (List.foldr (fn (pair, acc) => HOLogic_insert $ pair $ acc)
  13.107 -          HOLogic_empty_set pairs)
  13.108 +        SOME (fold_rev (fn pair => fn acc => HOLogic_insert $ pair $ acc) pairs HOLogic_empty_set)
  13.109        end
  13.110      | Type ("prop", [])      =>
  13.111        (case index_from_interpretation intr of
    14.1 --- a/src/HOL/Tools/res_axioms.ML	Thu Oct 29 23:49:55 2009 +0100
    14.2 +++ b/src/HOL/Tools/res_axioms.ML	Thu Oct 29 23:56:33 2009 +0100
    14.3 @@ -476,7 +476,7 @@
    14.4        val remaining_hyps = filter_out (member (op aconv) (map Thm.term_of defs))
    14.5                                        (map Thm.term_of hyps)
    14.6        val fixed = OldTerm.term_frees (concl_of st) @
    14.7 -                  List.foldl (uncurry (union (op aconv))) [] (map OldTerm.term_frees remaining_hyps)
    14.8 +                  fold (union (op aconv)) (map OldTerm.term_frees remaining_hyps) []
    14.9    in Seq.of_list [LocalDefs.expand (filter (is_okdef fixed o Thm.term_of) defs) st] end;
   14.10  
   14.11  
    15.1 --- a/src/HOL/Tools/simpdata.ML	Thu Oct 29 23:49:55 2009 +0100
    15.2 +++ b/src/HOL/Tools/simpdata.ML	Thu Oct 29 23:56:33 2009 +0100
    15.3 @@ -64,8 +64,8 @@
    15.4      else
    15.5        let
    15.6          val Ps = map (fn k => Free ("P" ^ string_of_int k, propT)) (1 upto j);
    15.7 -        fun mk_simp_implies Q = List.foldr (fn (R, S) =>
    15.8 -          Const ("HOL.simp_implies", propT --> propT --> propT) $ R $ S) Q Ps
    15.9 +        fun mk_simp_implies Q = fold_rev (fn R => fn S =>
   15.10 +          Const ("HOL.simp_implies", propT --> propT --> propT) $ R $ S) Ps Q
   15.11          val aT = TFree ("'a", HOLogic.typeS);
   15.12          val x = Free ("x", aT);
   15.13          val y = Free ("y", aT)
    16.1 --- a/src/Provers/classical.ML	Thu Oct 29 23:49:55 2009 +0100
    16.2 +++ b/src/Provers/classical.ML	Thu Oct 29 23:56:33 2009 +0100
    16.3 @@ -198,10 +198,10 @@
    16.4  (*Uses introduction rules in the normal way, or on negated assumptions,
    16.5    trying rules in order. *)
    16.6  fun swap_res_tac rls =
    16.7 -    let fun addrl (rl,brls) = (false, rl) :: (true, rl RSN (2, Data.swap)) :: brls
    16.8 +    let fun addrl rl brls = (false, rl) :: (true, rl RSN (2, Data.swap)) :: brls
    16.9      in  assume_tac      ORELSE'
   16.10          contr_tac       ORELSE'
   16.11 -        biresolve_tac (List.foldr addrl [] rls)
   16.12 +        biresolve_tac (fold_rev addrl rls [])
   16.13      end;
   16.14  
   16.15  (*Duplication of hazardous rules, for complete provers*)
    17.1 --- a/src/Provers/typedsimp.ML	Thu Oct 29 23:49:55 2009 +0100
    17.2 +++ b/src/Provers/typedsimp.ML	Thu Oct 29 23:56:33 2009 +0100
    17.3 @@ -64,12 +64,12 @@
    17.4  
    17.5  (*If the rule proves an equality then add both forms to simp_rls
    17.6    else add the rule to other_rls*)
    17.7 -fun add_rule (rl, (simp_rls, other_rls)) =
    17.8 +fun add_rule rl (simp_rls, other_rls) =
    17.9      (simp_rule rl :: resimp_rule rl :: simp_rls, other_rls)
   17.10      handle THM _ => (simp_rls, rl :: other_rls);
   17.11  
   17.12  (*Given the list rls, return the pair (simp_rls, other_rls).*)
   17.13 -fun process_rules rls = List.foldr add_rule ([],[]) rls;
   17.14 +fun process_rules rls = fold_rev add_rule rls ([], []);
   17.15  
   17.16  (*Given list of rewrite rules, return list of both forms, reject others*)
   17.17  fun process_rewrites rls = 
    18.1 --- a/src/ZF/Tools/datatype_package.ML	Thu Oct 29 23:49:55 2009 +0100
    18.2 +++ b/src/ZF/Tools/datatype_package.ML	Thu Oct 29 23:56:33 2009 +0100
    18.3 @@ -129,7 +129,7 @@
    18.4        Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **)
    18.5  
    18.6    (*The function variable for a single constructor*)
    18.7 -  fun add_case (((_, T, _), name, args, _), (opno, cases)) =
    18.8 +  fun add_case ((_, T, _), name, args, _) (opno, cases) =
    18.9      if Syntax.is_identifier name then
   18.10        (opno, (Free (case_varname ^ "_" ^ name, T), args) :: cases)
   18.11      else
   18.12 @@ -138,12 +138,12 @@
   18.13  
   18.14    (*Treatment of a list of constructors, for one part
   18.15      Result adds a list of terms, each a function variable with arguments*)
   18.16 -  fun add_case_list (con_ty_list, (opno, case_lists)) =
   18.17 -    let val (opno', case_list) = List.foldr add_case (opno, []) con_ty_list
   18.18 +  fun add_case_list con_ty_list (opno, case_lists) =
   18.19 +    let val (opno', case_list) = fold_rev add_case con_ty_list (opno, [])
   18.20      in (opno', case_list :: case_lists) end;
   18.21  
   18.22    (*Treatment of all parts*)
   18.23 -  val (_, case_lists) = List.foldr add_case_list (1,[]) con_ty_lists;
   18.24 +  val (_, case_lists) = fold_rev add_case_list con_ty_lists (1, []);
   18.25  
   18.26    (*extract the types of all the variables*)
   18.27    val case_typ = maps (map (#2 o #1)) con_ty_lists ---> @{typ "i => i"};
   18.28 @@ -215,7 +215,7 @@
   18.29    val rec_ty_lists = (map (map rec_ty_elem) con_ty_lists);
   18.30  
   18.31    (*Treatment of all parts*)
   18.32 -  val (_, recursor_lists) = List.foldr add_case_list (1,[]) rec_ty_lists;
   18.33 +  val (_, recursor_lists) = fold_rev add_case_list rec_ty_lists (1, []);
   18.34  
   18.35    (*extract the types of all the variables*)
   18.36    val recursor_typ = maps (map (#2 o #1)) rec_ty_lists ---> @{typ "i => i"};