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