Debian Codegraph @

kernel/context.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Created by Jean-Christophe Filliâtre out of names.ml as part of the
12 rebuilding of Coq around a purely functional abstract type-checker,
13 Aug 1999 *)
14 (* Miscellaneous extensions, restructurations and bug-fixes by Hugo
15 Herbelin and Bruno Barras *)
16
17 (* This file defines types and combinators regarding indexes-based and
18 names-based contexts *)
19
20 (** The modules defined below represent a {e local context}
21 as defined by Chapter 4 in the Reference Manual:
22
23 A {e local context} is an ordered list of of {e local declarations}
24 of names that we call {e variables}.
25
26 A {e local declaration} of some variable can be either:
27 - a {e local assumption}, or
28 - a {e local definition}.
29 *)
30
31 open Util
32 open Names
33
34 type ('a,'r) pbinder_annot = { binder_name : 'a; binder_relevance : 'r }
35
36 let eq_annot eq eqr {binder_name=na1;binder_relevance=r1} {binder_name=na2;binder_relevance=r2} =
37 eq na1 na2 && eqr r1 r2
38
39 let hash_annot h {binder_name=n;binder_relevance=r} =
40 Hashset.Combine.combinesmall (Sorts.relevance_hash r) (h n)
41
42 let map_annot f {binder_name=na;binder_relevance} =
43 let na' = f na in
44 {binder_name=na';binder_relevance}
45
46 let map_annot_relevance fr ({binder_name=na;binder_relevance=r} as a) =
47 let r' = fr r in
48 if r == r' then a else {binder_name=na;binder_relevance=r'}
49
50 let map_annot_relevance_het fr {binder_name=na;binder_relevance=r} =
51 let r' = fr r in
52 {binder_name=na;binder_relevance=r'}
53
54 let make_annot x r = {binder_name=x;binder_relevance=r}
55
56 let binder_name x = x.binder_name
57 let binder_relevance x = x.binder_relevance
58
59 let annotR x = make_annot x Sorts.Relevant
60
61 let nameR x = annotR (Name x)
62 let anonR = annotR Anonymous
63
64 (** Representation of contexts that can capture anonymous as well as non-anonymous variables.
65 Individual declarations are then designated by de Bruijn indexes. *)
66 module Rel =
67 struct
68 (** Representation of {e local declarations}. *)
69 module Declaration =
70 struct
71 (* local declaration *)
72 type ('constr, 'types, 'r) pt =
73 | LocalAssum of (Name.t,'r) pbinder_annot * 'types (** name, type *)
74 | LocalDef of (Name.t,'r) pbinder_annot * 'constr * 'types (** name, value, type *)
75
76 let get_annot = function
77 | LocalAssum (na,_) | LocalDef (na,_,_) -> na
78
79 (** Return the name bound by a given declaration. *)
80 let get_name x = (get_annot x).binder_name
81
82 (** Return [Some value] for local-declarations and [None] for local-assumptions. *)
83 let get_value = function
84 | LocalAssum _ -> None
85 | LocalDef (_,v,_) -> Some v
86
87 (** Return the type of the name bound by a given declaration. *)
88 let get_type = function
89 | LocalAssum (_,ty)
90 | LocalDef (_,_,ty) -> ty
91
92 let get_relevance x = (get_annot x).binder_relevance
93
94 let set_annot x d =
95 if get_annot d == x then d else match d with
96 | LocalAssum (_,ty) -> LocalAssum (x, ty)
97 | LocalDef (_,v,ty) -> LocalDef (x, v, ty)
98
99 (** Set the name that is bound by a given declaration. *)
100 let set_name na = function
101 | LocalAssum (x,ty) -> LocalAssum ({x with binder_name=na}, ty)
102 | LocalDef (x,v,ty) -> LocalDef ({x with binder_name=na}, v, ty)
103
104 let set_relevance r = function
105 | LocalAssum (x,ty) -> LocalAssum ({x with binder_relevance=r}, ty)
106 | LocalDef (x,v,ty) -> LocalDef ({x with binder_relevance=r}, v, ty)
107
108 (** Set the type of the bound variable in a given declaration. *)
109 let set_type ty = function
110 | LocalAssum (na,_) -> LocalAssum (na, ty)
111 | LocalDef (na,v,_) -> LocalDef (na, v, ty)
112
113 (** Return [true] iff a given declaration is a local assumption. *)
114 let is_local_assum = function
115 | LocalAssum _ -> true
116 | LocalDef _ -> false
117
118 (** Return [true] iff a given declaration is a local definition. *)
119 let is_local_def = function
120 | LocalAssum _ -> false
121 | LocalDef _ -> true
122
123 (** Check whether any term in a given declaration satisfies a given predicate. *)
124 let exists f = function
125 | LocalAssum (_, ty) -> f ty
126 | LocalDef (_, v, ty) -> f v || f ty
127
128 (** Check whether all terms in a given declaration satisfy a given predicate. *)
129 let for_all f = function
130 | LocalAssum (_, ty) -> f ty
131 | LocalDef (_, v, ty) -> f v && f ty
132
133 (** Check whether the two given declarations are equal. *)
134 let equal eqr eq decl1 decl2 =
135 match decl1, decl2 with
136 | LocalAssum (n1,ty1), LocalAssum (n2, ty2) ->
137 eq_annot Name.equal eqr n1 n2 && eq ty1 ty2
138 | LocalDef (n1,v1,ty1), LocalDef (n2,v2,ty2) ->
139 eq_annot Name.equal eqr n1 n2 && eq v1 v2 && eq ty1 ty2
140 | _ ->
141 false
142
143 (** Map the name bound by a given declaration. *)
144 let map_name f x =
145 let na = get_name x in
146 let na' = f na in
147 if na == na' then x else set_name na' x
148
149 let map_relevance f x =
150 let r = get_relevance x in
151 let r' = f r in
152 if r == r' then x else set_relevance r' x
153
154 (** For local assumptions, this function returns the original local assumptions.
155 For local definitions, this function maps the value in the local definition. *)
156 let map_value f = function
157 | LocalAssum _ as decl -> decl
158 | LocalDef (na, v, t) as decl ->
159 let v' = f v in
160 if v == v' then decl else LocalDef (na, v', t)
161
162 (** Map the type of the name bound by a given declaration. *)
163 let map_type f = function
164 | LocalAssum (na, ty) as decl ->
165 let ty' = f ty in
166 if ty == ty' then decl else LocalAssum (na, ty')
167 | LocalDef (na, v, ty) as decl ->
168 let ty' = f ty in
169 if ty == ty' then decl else LocalDef (na, v, ty')
170
171 (** Map all terms in a given declaration. *)
172 let map_constr f = function
173 | LocalAssum (na, ty) as decl ->
174 let ty' = f ty in
175 if ty == ty' then decl else LocalAssum (na, ty')
176 | LocalDef (na, v, ty) as decl ->
177 let v' = f v in
178 let ty' = f ty in
179 if v == v' && ty == ty' then decl else LocalDef (na, v', ty')
180
181 (** Map all terms in a given declaration. *)
182 let map_constr_with_relevance g f = function
183 | LocalAssum (na, ty) as decl ->
184 let na' = map_annot_relevance g na in
185 let ty' = f ty in
186 if na == na' && ty == ty' then decl else LocalAssum (na', ty')
187 | LocalDef (na, v, ty) as decl ->
188 let na' = map_annot_relevance g na in
189 let v' = f v in
190 let ty' = f ty in
191 if na == na' && v == v' && ty == ty' then decl else LocalDef (na', v', ty')
192
193 let map_constr_het fr f = function
194 | LocalAssum (na, ty) ->
195 let ty' = f ty in
196 LocalAssum (map_annot_relevance_het fr na, ty')
197 | LocalDef (na, v, ty) ->
198 let v' = f v in
199 let ty' = f ty in
200 LocalDef (map_annot_relevance_het fr na, v', ty')
201
202 (** Perform a given action on all terms in a given declaration. *)
203 let iter_constr f = function
204 | LocalAssum (_,ty) -> f ty
205 | LocalDef (_,v,ty) -> f v; f ty
206
207 (** Reduce all terms in a given declaration to a single value. *)
208 let fold_constr f decl acc =
209 match decl with
210 | LocalAssum (_n,ty) -> f ty acc
211 | LocalDef (_n,v,ty) -> f ty (f v acc)
212
213 let to_tuple = function
214 | LocalAssum (na, ty) -> na, None, ty
215 | LocalDef (na, v, ty) -> na, Some v, ty
216
217 let drop_body = function
218 | LocalAssum _ as d -> d
219 | LocalDef (na, _v, ty) -> LocalAssum (na, ty)
220
221 end
222
223 (** Rel-context is represented as a list of declarations.
224 Inner-most declarations are at the beginning of the list.
225 Outer-most declarations are at the end of the list. *)
226 type ('constr, 'types, 'r) pt = ('constr, 'types, 'r) Declaration.pt list
227
228 (** empty rel-context *)
229 let empty = []
230
231 (** Return a new rel-context enriched by with a given inner-most declaration. *)
232 let add d ctx = d :: ctx
233
234 (** Return the number of {e local declarations} in a given rel-context. *)
235 let length = List.length
236
237 (** Return the number of {e local assumptions} in a given rel-context. *)
238 let nhyps ctx =
239 let open Declaration in
240 let rec nhyps acc = function
241 | [] -> acc
242 | LocalAssum _ :: hyps -> nhyps (succ acc) hyps
243 | LocalDef _ :: hyps -> nhyps acc hyps
244 in
245 nhyps 0 ctx
246
247 (** Return a declaration designated by a given de Bruijn index.
248 @raise Not_found if the designated de Bruijn index is not present in the designated rel-context. *)
249 let rec lookup n ctx =
250 match n, ctx with
251 | 1, decl :: _ -> decl
252 | n, _ :: sign -> lookup (n-1) sign
253 | _, [] -> raise Not_found
254
255 (** Check whether given two rel-contexts are equal. *)
256 let equal eqr eq l = List.equal (fun c -> Declaration.equal eqr eq c) l
257
258 (** Map all terms in a given rel-context. *)
259 let map f = List.Smart.map (Declaration.map_constr f)
260
261 let map_with_relevance g f = List.Smart.map (Declaration.map_constr_with_relevance g f)
262
263 let map_het fr f = List.map (Declaration.map_constr_het fr f)
264
265 (** Map all terms in a given rel-context. *)
266 let map_with_binders f ctx =
267 let rec aux k = function
268 | decl :: ctx as l ->
269 let decl' = Declaration.map_constr (f k) decl in
270 let ctx' = aux (k-1) ctx in
271 if decl == decl' && ctx == ctx' then l else decl' :: ctx'
272 | [] -> []
273 in
274 aux (length ctx) ctx
275
276 (** Perform a given action on every declaration in a given rel-context. *)
277 let iter f = List.iter (Declaration.iter_constr f)
278
279 (** Reduce all terms in a given rel-context to a single value.
280 Innermost declarations are processed first. *)
281 let fold_inside f ~init = List.fold_left f init
282
283 (** Reduce all terms in a given rel-context to a single value.
284 Outermost declarations are processed first. *)
285 let fold_outside f l ~init = List.fold_right f l init
286
287 (** Return the set of all named variables bound in a given rel-context. *)
288 let to_vars l =
289 List.fold_left (fun accu decl ->
290 match Declaration.get_name decl with
291 | Name id -> Id.Set.add id accu
292 | Anonymous -> accu)
293 Id.Set.empty l
294
295 (** Map a given rel-context to a list where each {e local definition} is mapped to [true]
296 and each {e local assumption} is mapped to [false]. *)
297 let to_tags l =
298 let rec aux l = function
299 | [] -> l
300 | Declaration.LocalDef _ :: ctx -> aux (true::l) ctx
301 | Declaration.LocalAssum _ :: ctx -> aux (false::l) ctx
302 in aux [] l
303
304 let drop_bodies l = List.Smart.map Declaration.drop_body l
305
306 (** Split a context so that the second part contains [n]
307 [LocalAssum], keeping all [LocalDef] in the middle in the first part *)
308 let chop_nhyps n l =
309 let rec aux l' = function
310 | (0, l) -> (List.rev l', l)
311 | (n, (Declaration.LocalDef _ as h) :: l) -> aux (h::l') (n, l)
312 | (n, (Declaration.LocalAssum _ as h) :: l) -> aux (h::l') (n-1, l)
313 | (_, []) -> CErrors.anomaly (Pp.str "chop_nhyps: not enough hypotheses.")
314 in aux [] (n,l)
315
316 (** [extended_list n Γ] builds an instance [args] such that [Γ,Δ ⊢ args:Γ]
317 with n = |Δ| and with the {e local definitions} of [Γ] skipped in
318 [args]. Example: for [x:T, y:=c, z:U] and [n]=2, it gives [Rel 5, Rel 3]. *)
319 let to_extended_list mk n l =
320 let rec reln l p = function
321 | Declaration.LocalAssum _ :: hyps -> reln (mk (n+p) :: l) (p+1) hyps
322 | Declaration.LocalDef _ :: hyps -> reln l (p+1) hyps
323 | [] -> l
324 in
325 reln [] 1 l
326
327 (** [extended_vect n Γ] does the same, returning instead an array. *)
328 let to_extended_vect mk n hyps = Array.of_list (to_extended_list mk n hyps)
329
330 (** Consistency with terminology in Named *)
331 let instance = to_extended_vect
332 let instance_list = to_extended_list
333 end
334
335 (** This module represents contexts that can capture non-anonymous variables.
336 Individual declarations are then designated by the identifiers they bind. *)
337 module Named =
338 struct
339 (** Representation of {e local declarations}. *)
340 module Declaration =
341 struct
342 (** local declaration *)
343 type ('constr, 'types, 'r) pt =
344 | LocalAssum of (Id.t,'r) pbinder_annot * 'types (** identifier, type *)
345 | LocalDef of (Id.t,'r) pbinder_annot * 'constr * 'types (** identifier, value, type *)
346
347 let get_annot = function
348 | LocalAssum (na,_) | LocalDef (na,_,_) -> na
349
350 (** Return the identifier bound by a given declaration. *)
351 let get_id x = (get_annot x).binder_name
352
353 (** Return [Some value] for local-declarations and [None] for local-assumptions. *)
354 let get_value = function
355 | LocalAssum _ -> None
356 | LocalDef (_,v,_) -> Some v
357
358 (** Return the type of the name bound by a given declaration. *)
359 let get_type = function
360 | LocalAssum (_,ty)
361 | LocalDef (_,_,ty) -> ty
362
363 let get_relevance x = (get_annot x).binder_relevance
364
365 (** Set the identifier that is bound by a given declaration. *)
366 let set_id id =
367 let set x = {x with binder_name = id} in
368 function
369 | LocalAssum (x,ty) -> LocalAssum (set x, ty)
370 | LocalDef (x, v, ty) -> LocalDef (set x, v, ty)
371
372 (** Set the type of the bound variable in a given declaration. *)
373 let set_type ty = function
374 | LocalAssum (id,_) -> LocalAssum (id, ty)
375 | LocalDef (id,v,_) -> LocalDef (id, v, ty)
376
377 (** Return [true] iff a given declaration is a local assumption. *)
378 let is_local_assum = function
379 | LocalAssum _ -> true
380 | LocalDef _ -> false
381
382 (** Return [true] iff a given declaration is a local definition. *)
383 let is_local_def = function
384 | LocalDef _ -> true
385 | LocalAssum _ -> false
386
387 (** Check whether any term in a given declaration satisfies a given predicate. *)
388 let exists f = function
389 | LocalAssum (_, ty) -> f ty
390 | LocalDef (_, v, ty) -> f v || f ty
391
392 (** Check whether all terms in a given declaration satisfy a given predicate. *)
393 let for_all f = function
394 | LocalAssum (_, ty) -> f ty
395 | LocalDef (_, v, ty) -> f v && f ty
396
397 (** Check whether the two given declarations are equal. *)
398 let equal eqr eq decl1 decl2 =
399 match decl1, decl2 with
400 | LocalAssum (id1, ty1), LocalAssum (id2, ty2) ->
401 eq_annot Id.equal eqr id1 id2 && eq ty1 ty2
402 | LocalDef (id1, v1, ty1), LocalDef (id2, v2, ty2) ->
403 eq_annot Id.equal eqr id1 id2 && eq v1 v2 && eq ty1 ty2
404 | _ ->
405 false
406
407 (** Map the identifier bound by a given declaration. *)
408 let map_id f x =
409 let id = get_id x in
410 let id' = f id in
411 if id == id' then x else set_id id' x
412
413 (** For local assumptions, this function returns the original local assumptions.
414 For local definitions, this function maps the value in the local definition. *)
415 let map_value f = function
416 | LocalAssum _ as decl -> decl
417 | LocalDef (na, v, t) as decl ->
418 let v' = f v in
419 if v == v' then decl else LocalDef (na, v', t)
420
421 (** Map the type of the name bound by a given declaration. *)
422 let map_type f = function
423 | LocalAssum (id, ty) as decl ->
424 let ty' = f ty in
425 if ty == ty' then decl else LocalAssum (id, ty')
426 | LocalDef (id, v, ty) as decl ->
427 let ty' = f ty in
428 if ty == ty' then decl else LocalDef (id, v, ty')
429
430 (** Map all terms in a given declaration. *)
431 let map_constr f = function
432 | LocalAssum (id, ty) as decl ->
433 let ty' = f ty in
434 if ty == ty' then decl else LocalAssum (id, ty')
435 | LocalDef (id, v, ty) as decl ->
436 let v' = f v in
437 let ty' = f ty in
438 if v == v' && ty == ty' then decl else LocalDef (id, v', ty')
439
440 (** Map all terms in a given declaration. *)
441 let map_constr_with_relevance g f = function
442 | LocalAssum (id, ty) as decl ->
443 let id' = map_annot_relevance g id in
444 let ty' = f ty in
445 if id == id' && ty == ty' then decl else LocalAssum (id', ty')
446 | LocalDef (id, v, ty) as decl ->
447 let id' = map_annot_relevance g id in
448 let v' = f v in
449 let ty' = f ty in
450 if id == id' && v == v' && ty == ty' then decl else LocalDef (id', v', ty')
451
452 let map_constr_het fr f = function
453 | LocalAssum (id, ty) ->
454 let ty' = f ty in
455 LocalAssum (map_annot_relevance_het fr id, ty')
456 | LocalDef (id, v, ty) ->
457 let v' = f v in
458 let ty' = f ty in
459 LocalDef (map_annot_relevance_het fr id, v', ty')
460
461 (** Perform a given action on all terms in a given declaration. *)
462 let iter_constr f = function
463 | LocalAssum (_, ty) -> f ty
464 | LocalDef (_, v, ty) -> f v; f ty
465
466 (** Reduce all terms in a given declaration to a single value. *)
467 let fold_constr f decl a =
468 match decl with
469 | LocalAssum (_, ty) -> f ty a
470 | LocalDef (_, v, ty) -> a |> f v |> f ty
471
472 let to_tuple = function
473 | LocalAssum (id, ty) -> id, None, ty
474 | LocalDef (id, v, ty) -> id, Some v, ty
475
476 let of_tuple = function
477 | id, None, ty -> LocalAssum (id, ty)
478 | id, Some v, ty -> LocalDef (id, v, ty)
479
480 let drop_body = function
481 | LocalAssum _ as d -> d
482 | LocalDef (id, _v, ty) -> LocalAssum (id, ty)
483
484 let of_rel_decl f = function
485 | Rel.Declaration.LocalAssum (na,t) ->
486 LocalAssum (map_annot f na, t)
487 | Rel.Declaration.LocalDef (na,v,t) ->
488 LocalDef (map_annot f na, v, t)
489
490 let to_rel_decl =
491 let name x = {binder_name=Name x.binder_name;binder_relevance=x.binder_relevance} in
492 function
493 | LocalAssum (id,t) ->
494 Rel.Declaration.LocalAssum (name id, t)
495 | LocalDef (id,v,t) ->
496 Rel.Declaration.LocalDef (name id,v,t)
497 end
498
499 (** Named-context is represented as a list of declarations.
500 Inner-most declarations are at the beginning of the list.
501 Outer-most declarations are at the end of the list. *)
502 type ('constr, 'types, 'r) pt = ('constr, 'types, 'r) Declaration.pt list
503
504 (** empty named-context *)
505 let empty = []
506
507 (** Return a new named-context enriched by with a given inner-most declaration. *)
508 let add d ctx = d :: ctx
509
510 (** Return the number of {e local declarations} in a given named-context. *)
511 let length = List.length
512
513 (** Return a declaration designated by a given identifier
514 @raise Not_found if the designated identifier is not present in the designated named-context. *)
515 let rec lookup id = function
516 | decl :: _ when Id.equal id (Declaration.get_id decl) -> decl
517 | _ :: sign -> lookup id sign
518 | [] -> raise Not_found
519
520 (** Check whether given two named-contexts are equal. *)
521 let equal eqr eq l = List.equal (fun c -> Declaration.equal eqr eq c) l
522
523 (** Map all terms in a given named-context. *)
524 let map f = List.Smart.map (Declaration.map_constr f)
525
526 let map_with_relevance g f = List.Smart.map (Declaration.map_constr_with_relevance g f)
527
528 let map_het fr f = List.map (Declaration.map_constr_het fr f)
529
530 (** Perform a given action on every declaration in a given named-context. *)
531 let iter f = List.iter (Declaration.iter_constr f)
532
533 (** Reduce all terms in a given named-context to a single value.
534 Innermost declarations are processed first. *)
535 let fold_inside f ~init = List.fold_left f init
536
537 (** Reduce all terms in a given named-context to a single value.
538 Outermost declarations are processed first. *)
539 let fold_outside f l ~init = List.fold_right f l init
540
541 (** Return the set of all identifiers bound in a given named-context. *)
542 let to_vars l =
543 List.fold_left (fun accu decl -> Id.Set.add (Declaration.get_id decl) accu) Id.Set.empty l
544
545 let drop_bodies l = List.Smart.map Declaration.drop_body l
546
547 (** [to_instance Ω] builds an instance [args] in reverse order such
548 that [Ω ⊢ args:Ω] where [Ω] is a named context and with the local
549 definitions of [Ω] skipped. Example: for [id1:T,id2:=c,id3:U], it
550 gives [Var id1, Var id3]. All [idj] are supposed distinct. *)
551 let to_instance mk l =
552 let filter = function
553 | Declaration.LocalAssum (id, _) -> Some (mk id.binder_name)
554 | _ -> None
555 in
556 List.map_filter filter l
557
558 (** [instance Ω] builds an instance [args] such
559 that [Ω ⊢ args:Ω] where [Ω] is a named context and with the local
560 definitions of [Ω] skipped. Example: for [id1:T,id2:=c,id3:U], it
561 gives [Var id1, Var id3]. All [idj] are supposed distinct. *)
562 let instance_list mk l =
563 let filter = function
564 | Declaration.LocalAssum (id, _) -> Some (mk id.binder_name)
565 | _ -> None
566 in
567 List.rev (List.map_filter filter l)
568
569 let instance mk l =
570 Array.of_list (instance_list mk l)
571 end
572
573 module Compacted =
574 struct
575 module Declaration =
576 struct
577 type ('constr, 'types, 'r) pt =
578 | LocalAssum of (Id.t,'r) pbinder_annot list * 'types
579 | LocalDef of (Id.t,'r) pbinder_annot list * 'constr * 'types
580
581 let map_constr f = function
582 | LocalAssum (ids, ty) as decl ->
583 let ty' = f ty in
584 if ty == ty' then decl else LocalAssum (ids, ty')
585 | LocalDef (ids, c, ty) as decl ->
586 let ty' = f ty in
587 let c' = f c in
588 if c == c' && ty == ty' then decl else LocalDef (ids,c',ty')
589
590 let of_named_decl = function
591 | Named.Declaration.LocalAssum (id,t) ->
592 LocalAssum ([id],t)
593 | Named.Declaration.LocalDef (id,v,t) ->
594 LocalDef ([id],v,t)
595
596 let to_named_context = function
597 | LocalAssum (ids, t) ->
598 List.map (fun id -> Named.Declaration.LocalAssum (id,t)) ids
599 | LocalDef (ids, v, t) ->
600 List.map (fun id -> Named.Declaration.LocalDef (id,v,t)) ids
601 end
602
603 type ('constr, 'types, 'r) pt = ('constr, 'types, 'r) Declaration.pt list
604
605 let fold f l ~init = List.fold_right f l init
606 end