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