]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/content_pres/termContentPres.ml
notation_id were compared using Pervasives.equal this was rarely triggering the
[helm.git] / helm / software / components / content_pres / termContentPres.ml
1 (* Copyright (C) 2004-2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 open Printf
29
30 module Ast = CicNotationPt
31 module Env = CicNotationEnv
32
33 let debug = false
34 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
35
36 type pattern_id = int
37 type pretty_printer_id = pattern_id
38
39 let resolve_binder = function
40   | `Lambda -> "\\lambda"
41   | `Pi -> "\\Pi"
42   | `Forall -> "\\forall"
43   | `Exists -> "\\exists"
44
45 let add_level_info prec t = Ast.AttributedTerm (`Level prec, t)
46
47 let rec top_pos t = add_level_info ~-1 t
48
49 let rec remove_level_info =
50   function
51   | Ast.AttributedTerm (`Level _, t) -> remove_level_info t
52   | Ast.AttributedTerm (a, t) -> Ast.AttributedTerm (a, remove_level_info t)
53   | t -> t
54
55 let add_xml_attrs attrs t =
56   if attrs = [] then t else Ast.AttributedTerm (`XmlAttrs attrs, t)
57
58 let add_keyword_attrs =
59   add_xml_attrs (RenderingAttrs.keyword_attributes `MathML)
60
61 let box kind spacing indent content =
62   Ast.Layout (Ast.Box ((kind, spacing, indent), content))
63
64 let hbox = box Ast.H
65 let vbox = box Ast.V
66 let hvbox = box Ast.HV
67 let hovbox = box Ast.HOV
68 let break = Ast.Layout Ast.Break
69 let space = Ast.Literal (`Symbol " ")
70 let builtin_symbol s = Ast.Literal (`Symbol s)
71 let keyword k = add_keyword_attrs (Ast.Literal (`Keyword k))
72
73 let number s =
74   add_xml_attrs (RenderingAttrs.number_attributes `MathML)
75     (Ast.Literal (`Number s))
76
77 let ident i =
78   add_xml_attrs (RenderingAttrs.ident_attributes `MathML) (Ast.Ident (i, None))
79
80 let ident_w_href href i =
81   match href with
82   | None -> ident i
83   | Some href ->
84       let href = UriManager.string_of_uri href in
85       add_xml_attrs [Some "xlink", "href", href] (ident i)
86
87 let binder_symbol s =
88   add_xml_attrs (RenderingAttrs.builtin_symbol_attributes `MathML)
89     (builtin_symbol s)
90
91 let string_of_sort_kind = function
92   | `Prop -> "Prop"
93   | `Set -> "Set"
94   | `CProp _ -> "CProp"
95   | `Type _ -> "Type"
96
97 let pp_ast0 t k =
98   let rec aux =
99     function
100     | Ast.Appl ts ->
101         let rec aux_args level =
102           function
103           | [] -> []
104           | [ last ] ->
105               [ Ast.AttributedTerm (`Level level,k last) ]
106           | hd :: tl ->
107               (Ast.AttributedTerm (`Level level, k hd)) :: aux_args 71 tl
108         in
109         add_level_info Ast.apply_prec 
110           (hovbox true true (CicNotationUtil.dress break (aux_args 70 ts)))
111     | Ast.Binder (binder_kind, (id, ty), body) ->
112         add_level_info Ast.binder_prec
113           (hvbox false true
114             [ binder_symbol (resolve_binder binder_kind);
115               k id; builtin_symbol ":"; aux_ty ty; break;
116               builtin_symbol "."; k body ])
117     | Ast.Case (what, indty_opt, outty_opt, patterns) ->
118         let outty_box =
119           match outty_opt with
120           | None -> []
121           | Some outty ->
122               [ space; keyword "return"; space; break; remove_level_info (k outty)]
123         in
124         let indty_box =
125           match indty_opt with
126           | None -> []
127           | Some (indty, href) -> [ space; keyword "in"; space; break; ident_w_href href indty ]
128         in
129         let match_box =
130           hvbox false false [
131            hvbox false true [
132             hvbox false true [keyword "match"; space; break; top_pos (k what)];
133             break;
134             hvbox false true indty_box;
135             break;
136             hvbox false true outty_box
137            ];
138            break;
139            space;
140            keyword "with";
141            space
142          ]
143         in
144         let mk_case_pattern =
145          function
146             Ast.Pattern (head, href, vars) ->
147              hvbox true false (ident_w_href href head :: List.map aux_var vars)
148           | Ast.Wildcard -> builtin_symbol "_"
149         in
150         let patterns' =
151           List.map
152             (fun (lhs, rhs) ->
153               remove_level_info
154                 (hvbox false true [
155                   hbox false true [
156                     mk_case_pattern lhs; builtin_symbol "\\Rightarrow" ];
157                   break; top_pos (k rhs) ]))
158             patterns
159         in
160         let patterns'' =
161           let rec aux_patterns = function
162             | [] -> assert false
163             | [ last ] ->
164                 [ break; 
165                   hbox false false [
166                     builtin_symbol "|";
167                     last; builtin_symbol "]" ] ]
168             | hd :: tl ->
169                 [ break; hbox false false [ builtin_symbol "|"; hd ] ]
170                 @ aux_patterns tl
171           in
172           match patterns' with
173           | [] ->
174               [ hbox false false [ builtin_symbol "["; builtin_symbol "]" ] ]
175           | [ one ] ->
176               [ hbox false false [
177                 builtin_symbol "["; one; builtin_symbol "]" ] ]
178           | hd :: tl ->
179               hbox false false [ builtin_symbol "["; hd ]
180               :: aux_patterns tl
181         in
182         add_level_info Ast.simple_prec
183           (hvbox false false [
184             hvbox false false ([match_box]); break;
185             hbox false false [ hvbox false false patterns'' ] ])
186     | Ast.Cast (bo, ty) ->
187         add_level_info Ast.simple_prec
188           (hvbox false true [
189             builtin_symbol "("; top_pos (k bo); break; builtin_symbol ":";
190             top_pos (k ty); builtin_symbol ")"])
191     | Ast.LetIn (var, s, t) ->
192         add_level_info Ast.let_in_prec 
193           (hvbox false true [
194             hvbox false true [
195               keyword "let"; space;
196               hvbox false true [
197                 aux_var var; space;
198                 builtin_symbol "\\def"; break; top_pos (k s) ];
199               break; space; keyword "in"; space ];
200             break;
201             k t ])
202     | Ast.LetRec (rec_kind, funs, where) ->
203         let rec_op =
204           match rec_kind with `Inductive -> "rec" | `CoInductive -> "corec"
205         in
206         let mk_fun (args, (name,ty), body, rec_param) =
207          List.map aux_var args ,k name, HExtlib.map_option k ty, k body,  
208            fst (List.nth args rec_param)
209         in
210         let mk_funs = List.map mk_fun in
211         let fst_fun, tl_funs =
212           match mk_funs funs with hd :: tl -> hd, tl | [] -> assert false
213         in
214         let fst_row =
215           let (params, name, ty, body, rec_param) = fst_fun in
216           hvbox false true ([
217             keyword "let";
218             space;
219             keyword rec_op;
220             space;
221             name] @
222             params @
223             [space; keyword "on" ; space ; rec_param ;space ] @
224             (match ty with None -> [] | Some ty -> [builtin_symbol ":"; ty]) @
225             [ builtin_symbol "\\def";
226             break;
227             top_pos body ])
228         in
229         let tl_rows =
230           List.map
231             (fun (params, name, ty, body, rec_param) ->
232               [ break;
233                 hvbox false true ([
234                   keyword "and";
235                   name] @
236                   params @
237                   [space; keyword "on" ; space; rec_param ;space ] @
238                   (match ty with
239                       None -> []
240                     | Some ty -> [builtin_symbol ":"; ty]) @
241                   [ builtin_symbol "\\def"; break; body ])])
242             tl_funs
243         in
244         add_level_info Ast.let_in_prec
245           ((hvbox false false
246             (fst_row :: List.flatten tl_rows
247              @ [ break; keyword "in"; break; k where ])))
248     | Ast.Implicit -> builtin_symbol "?"
249     | Ast.Meta (n, l) ->
250         let local_context l =
251             List.map (function None -> None | Some t -> Some (k t)) l
252         in
253         Ast.Meta(n, local_context l)
254     | Ast.Sort sort -> aux_sort sort
255     | Ast.Num _
256     | Ast.Symbol _
257     | Ast.Ident (_, None) | Ast.Ident (_, Some [])
258     | Ast.Uri (_, None) | Ast.Uri (_, Some [])
259     | Ast.Literal _
260     | Ast.UserInput as leaf -> leaf
261     | t -> CicNotationUtil.visit_ast ~special_k k t
262   and aux_sort sort_kind =
263     add_xml_attrs (RenderingAttrs.keyword_attributes `MathML)
264       (Ast.Ident (string_of_sort_kind sort_kind, None))
265   and aux_ty = function
266     | None -> builtin_symbol "?"
267     | Some ty -> k ty
268   and aux_var = function
269     | name, Some ty ->
270         hvbox false true [
271           builtin_symbol "("; name; builtin_symbol ":"; break; k ty;
272           builtin_symbol ")" ]
273     | name, None -> name
274   and special_k = function
275     | Ast.AttributedTerm (attrs, t) -> Ast.AttributedTerm (attrs, k t)
276     | t ->
277         prerr_endline ("unexpected special: " ^ CicNotationPp.pp_term t);
278         assert false
279   in
280   aux t
281
282   (* persistent state *)
283
284 let level1_patterns21 = Hashtbl.create 211
285
286 let compiled21 = ref None
287
288 let pattern21_matrix = ref []
289
290 let get_compiled21 () =
291   match !compiled21 with
292   | None -> assert false
293   | Some f -> Lazy.force f
294
295 let set_compiled21 f = compiled21 := Some f
296
297 let add_idrefs =
298   List.fold_right (fun idref t -> Ast.AttributedTerm (`IdRef idref, t))
299
300 let instantiate21 idrefs env l1 =
301   let rec subst_singleton pos env =
302     function
303       Ast.AttributedTerm (attr, t) ->
304         Ast.AttributedTerm (attr, subst_singleton pos env t)
305     | t -> CicNotationUtil.group (subst pos env t)
306   and subst pos env = function
307     | Ast.AttributedTerm (attr, t) ->
308 (*         prerr_endline ("loosing attribute " ^ CicNotationPp.pp_attribute attr); *)
309         subst pos env t
310     | Ast.Variable var ->
311         let name, expected_ty = CicNotationEnv.declaration_of_var var in
312         let ty, value =
313           try
314             List.assoc name env
315           with Not_found ->
316             prerr_endline ("name " ^ name ^ " not found in environment");
317             assert false
318         in
319         assert (CicNotationEnv.well_typed ty value); (* INVARIANT *)
320         (* following assertion should be a conditional that makes this
321          * instantiation fail *)
322         if not (CicNotationEnv.well_typed expected_ty value) then
323          begin
324           prerr_endline ("The variable " ^ name ^ " is used with the wrong type in the notation declaration");
325           assert false
326          end;
327         let value = CicNotationEnv.term_of_value value in
328         let value = 
329           match expected_ty with
330           | Env.TermType l -> Ast.AttributedTerm (`Level l,value) 
331           | _ -> value
332         in
333         [ value ]
334     | Ast.Magic m -> subst_magic pos env m
335     | Ast.Literal l as t ->
336         let t = add_idrefs idrefs t in
337         (match l with
338         | `Keyword k -> [ add_keyword_attrs t ]
339         | _ -> [ t ])
340     | Ast.Layout l -> [ Ast.Layout (subst_layout pos env l) ]
341     | t -> [ CicNotationUtil.visit_ast (subst_singleton pos env) t ]
342   and subst_magic pos env = function
343     | Ast.List0 (p, sep_opt)
344     | Ast.List1 (p, sep_opt) ->
345         prerr_endline "1";
346         let rec_decls = CicNotationEnv.declarations_of_term p in
347         prerr_endline "2";
348         let rec_values =
349           List.map (fun (n, _) -> CicNotationEnv.lookup_list env n) rec_decls
350         in
351         prerr_endline "3";
352         let values = CicNotationUtil.ncombine rec_values in
353         prerr_endline "4";
354         let sep =
355           match sep_opt with
356             | None -> []
357             | Some l -> [ Ast.Literal l; break; space ]
358         in
359         prerr_endline "5";
360         let rec instantiate_list acc = function
361           | [] -> List.rev acc
362           | value_set :: [] ->
363               let env = CicNotationEnv.combine rec_decls value_set in
364               instantiate_list (CicNotationUtil.group (subst pos env p) :: acc)
365                 []
366           | value_set :: tl ->
367               let env = CicNotationEnv.combine rec_decls value_set in
368               let terms = subst pos env p in
369               instantiate_list (CicNotationUtil.group (terms @ sep) :: acc) tl
370         in
371         if values = [] then []
372         else [hovbox false false (instantiate_list [] values)]
373     | Ast.Opt p ->
374         let opt_decls = CicNotationEnv.declarations_of_term p in
375         let env =
376           let rec build_env = function
377             | [] -> []
378             | (name, ty) :: tl ->
379                   (* assumption: if one of the value is None then all are *)
380                 (match CicNotationEnv.lookup_opt env name with
381                 | None -> raise Exit
382                 | Some v -> (name, (ty, v)) :: build_env tl)
383           in
384           try build_env opt_decls with Exit -> []
385         in
386           begin
387             match env with
388               | [] -> []
389               | _ -> subst pos env p
390           end
391     | _ -> assert false (* impossible *)
392   and subst_layout pos env = function
393     | Ast.Box (kind, tl) ->
394         let tl' = subst_children pos env tl in
395         Ast.Box (kind, List.concat tl')
396     | l -> CicNotationUtil.visit_layout (subst_singleton pos env) l
397   and subst_children pos env =
398     function
399     | [] -> []
400     | [ child ] ->
401         let pos' =
402           match pos with
403           | `Inner -> `Right
404           | `Left -> `Left
405 (*           | `None -> assert false *)
406           | `Right -> `Right
407         in
408         [ subst pos' env child ]
409     | hd :: tl ->
410         let pos' =
411           match pos with
412           | `Inner -> `Inner
413           | `Left -> `Inner
414 (*           | `None -> assert false *)
415           | `Right -> `Right
416         in
417         (subst pos env hd) :: subst_children pos' env tl
418   in
419     subst_singleton `Left env l1
420
421 let rec pp_ast1 term = 
422   let rec pp_value = function
423     | CicNotationEnv.NumValue _ as v -> v
424     | CicNotationEnv.StringValue _ as v -> v
425 (*     | CicNotationEnv.TermValue t when t == term -> CicNotationEnv.TermValue (pp_ast0 t pp_ast1) *)
426     | CicNotationEnv.TermValue t -> CicNotationEnv.TermValue (pp_ast1 t)
427     | CicNotationEnv.OptValue None as v -> v
428     | CicNotationEnv.OptValue (Some v) -> 
429         CicNotationEnv.OptValue (Some (pp_value v))
430     | CicNotationEnv.ListValue vl ->
431         CicNotationEnv.ListValue (List.map pp_value vl)
432   in
433   let ast_env_of_env env =
434     List.map (fun (var, (ty, value)) -> (var, (ty, pp_value value))) env
435   in
436 (* prerr_endline ("pattern matching from 2 to 1 on term " ^ CicNotationPp.pp_term term); *)
437   match term with
438   | Ast.AttributedTerm (attrs, term') ->
439       Ast.AttributedTerm (attrs, pp_ast1 term')
440   | _ ->
441       (match (get_compiled21 ()) term with
442       | None -> pp_ast0 term pp_ast1
443       | Some (env, ctors, pid) ->
444           let idrefs =
445             List.flatten (List.map CicNotationUtil.get_idrefs ctors)
446           in
447           let l1 =
448             try
449               Hashtbl.find level1_patterns21 pid
450             with Not_found -> assert false
451           in
452           instantiate21 idrefs (ast_env_of_env env) l1)
453
454 let load_patterns21 t =
455   set_compiled21 (lazy (Content2presMatcher.Matcher21.compiler t))
456
457 let pp_ast ast =
458   debug_print (lazy "pp_ast <-");
459   let ast' = pp_ast1 ast in
460   debug_print (lazy ("pp_ast -> " ^ CicNotationPp.pp_term ast'));
461   ast'
462
463 exception Pretty_printer_not_found
464
465 let fill_pos_info l1_pattern = l1_pattern
466 (*   let rec aux toplevel pos =
467     function
468     | Ast.Layout l ->
469         (match l 
470
471     | Ast.Magic m ->
472         Ast.Box (
473     | Ast.Variable _ as t -> add_pos_info pos t
474     | t -> t
475   in
476   aux true l1_pattern *)
477
478 let counter = ref ~-1 
479 let reset () =
480   counter := ~-1;
481   Hashtbl.clear level1_patterns21
482 ;;
483 let fresh_id =
484   fun () ->
485     incr counter;
486     !counter
487
488 let add_pretty_printer l2 (CicNotationParser.CL1P (l1,precedence)) =
489   let id = fresh_id () in
490   let l1' = add_level_info precedence (fill_pos_info l1) in
491   let l2' = CicNotationUtil.strip_attributes l2 in
492   Hashtbl.add level1_patterns21 id l1';
493   pattern21_matrix := (l2', id) :: !pattern21_matrix;
494   load_patterns21 !pattern21_matrix;
495   id
496
497 let remove_pretty_printer id =
498   (try
499     Hashtbl.remove level1_patterns21 id;
500   with Not_found -> raise Pretty_printer_not_found);
501   pattern21_matrix := List.filter (fun (_, id') -> id <> id') !pattern21_matrix;
502   load_patterns21 !pattern21_matrix
503
504   (* presentation -> content *)
505
506 let unopt_names names env =
507   let rec aux acc = function
508     | (name, (ty, v)) :: tl when List.mem name names ->
509         (match ty, v with
510         | Env.OptType ty, Env.OptValue (Some v) ->
511             aux ((name, (ty, v)) :: acc) tl
512         | _ -> assert false)
513     | hd :: tl -> aux (hd :: acc) tl
514     | [] -> acc
515   in
516   aux [] env
517
518 let head_names names env =
519   let rec aux acc = function
520     | (name, (ty, v)) :: tl when List.mem name names ->
521         (match ty, v with
522         | Env.ListType ty, Env.ListValue (v :: _) ->
523             aux ((name, (ty, v)) :: acc) tl
524         | Env.TermType _, Env.TermValue _  ->
525             aux ((name, (ty, v)) :: acc) tl
526         | _ -> assert false)
527     | _ :: tl -> aux acc tl
528       (* base pattern may contain only meta names, thus we trash all others *)
529     | [] -> acc
530   in
531   aux [] env
532
533 let tail_names names env =
534   let rec aux acc = function
535     | (name, (ty, v)) :: tl when List.mem name names ->
536         (match ty, v with
537         | Env.ListType ty, Env.ListValue (_ :: vtl) ->
538             aux ((name, (Env.ListType ty, Env.ListValue vtl)) :: acc) tl
539         | Env.TermType _, Env.TermValue _  ->
540             aux ((name, (ty, v)) :: acc) tl
541         | _ -> assert false)
542     | binding :: tl -> aux (binding :: acc) tl
543     | [] -> acc
544   in
545   aux [] env
546
547 let instantiate_level2 env term =
548 (*   prerr_endline ("istanzio: " ^ CicNotationPp.pp_term term); *)
549   let fresh_env = ref [] in
550   let lookup_fresh_name n =
551     try
552       List.assoc n !fresh_env
553     with Not_found ->
554       let new_name = CicNotationUtil.fresh_name () in
555       fresh_env := (n, new_name) :: !fresh_env;
556       new_name
557   in
558   let rec aux env term =
559 (*    prerr_endline ("ENV " ^ CicNotationPp.pp_env env); *)
560     match term with
561     | Ast.AttributedTerm (a, term) -> (*Ast.AttributedTerm (a, *)aux env term
562     | Ast.Appl terms -> Ast.Appl (List.map (aux env) terms)
563     | Ast.Binder (binder, var, body) ->
564         Ast.Binder (binder, aux_capture_var env var, aux env body)
565     | Ast.Case (term, indty, outty_opt, patterns) ->
566         Ast.Case (aux env term, indty, aux_opt env outty_opt,
567           List.map (aux_branch env) patterns)
568     | Ast.LetIn (var, t1, t3) ->
569         Ast.LetIn (aux_capture_var env var, aux env t1, aux env t3)
570     | Ast.LetRec (kind, definitions, body) ->
571         Ast.LetRec (kind, List.map (aux_definition env) definitions,
572           aux env body)
573     | Ast.Uri (name, None) -> Ast.Uri (name, None)
574     | Ast.Uri (name, Some substs) ->
575         Ast.Uri (name, Some (aux_substs env substs))
576     | Ast.Ident (name, Some substs) ->
577         Ast.Ident (name, Some (aux_substs env substs))
578     | Ast.Meta (index, substs) -> Ast.Meta (index, aux_meta_substs env substs)
579
580     | Ast.Implicit
581     | Ast.Ident _
582     | Ast.Num _
583     | Ast.Sort _
584     | Ast.Symbol _
585     | Ast.UserInput -> term
586
587     | Ast.Magic magic -> aux_magic env magic
588     | Ast.Variable var -> aux_variable env var
589
590     | _ -> assert false
591   and aux_opt env = function
592     | Some term -> Some (aux env term)
593     | None -> None
594   and aux_capture_var env (name, ty_opt) = (aux env name, aux_opt env ty_opt)
595   and aux_branch env (pattern, term) =
596     (aux_pattern env pattern, aux env term)
597   and aux_pattern env =
598    function
599       Ast.Pattern (head, hrefs, vars) ->
600        Ast.Pattern (head, hrefs, List.map (aux_capture_var env) vars)
601     | Ast.Wildcard -> Ast.Wildcard
602   and aux_definition env (params, var, term, i) =
603     (List.map (aux_capture_var env) params, aux_capture_var env var, aux env term, i)
604   and aux_substs env substs =
605     List.map (fun (name, term) -> (name, aux env term)) substs
606   and aux_meta_substs env meta_substs = List.map (aux_opt env) meta_substs
607   and aux_variable env = function
608     | Ast.NumVar name -> Ast.Num (Env.lookup_num env name, 0)
609     | Ast.IdentVar name -> Ast.Ident (Env.lookup_string env name, None)
610     | Ast.TermVar (name,(Ast.Level l|Ast.Self l)) -> 
611         Ast.AttributedTerm (`Level l,Env.lookup_term env name)
612     | Ast.FreshVar name -> Ast.Ident (lookup_fresh_name name, None)
613     | Ast.Ascription (term, name) -> assert false
614   and aux_magic env = function
615     | Ast.Default (some_pattern, none_pattern) ->
616         let some_pattern_names = CicNotationUtil.names_of_term some_pattern in
617         let none_pattern_names = CicNotationUtil.names_of_term none_pattern in
618         let opt_names =
619           List.filter
620             (fun name -> not (List.mem name none_pattern_names))
621             some_pattern_names
622         in
623         (match opt_names with
624         | [] -> assert false (* some pattern must contain at least 1 name *)
625         | (name :: _) as names ->
626             (match Env.lookup_value env name with
627             | Env.OptValue (Some _) ->
628                 (* assumption: if "name" above is bound to Some _, then all
629                  * names returned by "meta_names_of" are bound to Some _ as well
630                  *)
631                 aux (unopt_names names env) some_pattern
632             | Env.OptValue None -> aux env none_pattern
633             | _ ->
634                 prerr_endline (sprintf
635                   "lookup of %s in env %s did not return an optional value"
636                   name (CicNotationPp.pp_env env));
637                 assert false))
638     | Ast.Fold (`Left, base_pattern, names, rec_pattern) ->
639         let acc_name = List.hd names in (* names can't be empty, cfr. parser *)
640         let meta_names =
641           List.filter ((<>) acc_name)
642             (CicNotationUtil.names_of_term rec_pattern)
643         in
644         (match meta_names with
645         | [] -> assert false (* as above *)
646         | (name :: _) as names ->
647             let rec instantiate_fold_left acc env' =
648               match Env.lookup_value env' name with
649               | Env.ListValue (_ :: _) ->
650                   instantiate_fold_left 
651                     (let acc_binding =
652                       acc_name, (Env.TermType 0, Env.TermValue acc)
653                      in
654                      aux (acc_binding :: head_names names env') rec_pattern)
655                     (tail_names names env')
656               | Env.ListValue [] -> acc
657               | _ -> assert false
658             in
659             instantiate_fold_left (aux env base_pattern) env)
660     | Ast.Fold (`Right, base_pattern, names, rec_pattern) ->
661         let acc_name = List.hd names in (* names can't be empty, cfr. parser *)
662         let meta_names =
663           List.filter ((<>) acc_name)
664             (CicNotationUtil.names_of_term rec_pattern)
665         in
666         (match meta_names with
667         | [] -> assert false (* as above *)
668         | (name :: _) as names ->
669             let rec instantiate_fold_right env' =
670               match Env.lookup_value env' name with
671               | Env.ListValue (_ :: _) ->
672                   let acc = instantiate_fold_right (tail_names names env') in
673                   let acc_binding =
674                     acc_name, (Env.TermType 0, Env.TermValue acc)
675                   in
676                   aux (acc_binding :: head_names names env') rec_pattern
677               | Env.ListValue [] -> aux env base_pattern
678               | _ -> assert false
679             in
680             instantiate_fold_right env)
681     | Ast.If (_, p_true, p_false) as t ->
682         aux env (CicNotationUtil.find_branch (Ast.Magic t))
683     | Ast.Fail -> assert false
684     | _ -> assert false
685   in
686   aux env term
687
688   (* initialization *)
689
690 let _ = load_patterns21 []
691
692
693