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