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