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