]> matita.cs.unibo.it Git - helm.git/blob - matita/components/content/interpretations.ml
big change in parsing, trying to make all functional
[helm.git] / matita / components / content / interpretations.ml
1 (* Copyright (C) 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 = NotationPt
31
32 let debug = false
33 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
34
35 let load_patterns32 = ref (fun _ -> assert false);;
36 let set_load_patterns32 f = load_patterns32 := f;;
37
38 let idref id t = Ast.AttributedTerm (`IdRef id, t)
39
40 type cic_id = string
41
42 type term_info =
43   { sort: (cic_id, Ast.sort_kind) Hashtbl.t;
44     uri: (cic_id, NReference.reference) Hashtbl.t;
45   }
46
47 module IntMap = Map.Make(struct type t = int let compare = compare end);;
48 module StringMap = Map.Make(String);;
49
50 type db = {
51   counter: int;
52   pattern32_matrix: (bool * NotationPt.cic_appl_pattern * int) list;
53   level2_patterns32:
54    (string * string * NotationPt.argument_pattern list *
55      NotationPt.cic_appl_pattern) IntMap.t;
56   interpretations: int list StringMap.t; (* symb -> id list *)
57 }
58
59 let initial_db = {
60    counter = -1; 
61    pattern32_matrix = [];
62    level2_patterns32 = IntMap.empty;
63    interpretations = StringMap.empty
64 }
65
66 class type g_status =
67   object
68     method interp_db: db
69   end
70  
71 class status =
72  object
73    val interp_db = initial_db  
74    method interp_db = interp_db
75    method set_interp_db v = {< interp_db = v >}
76    method set_interp_status
77     : 'status. #g_status as 'status -> 'self
78     = fun o -> {< interp_db = o#interp_db >}
79  end
80
81 let find_level2_patterns32 status pid =
82  IntMap.find pid status#interp_db.level2_patterns32
83
84 let add_idrefs =
85   List.fold_right (fun idref t -> Ast.AttributedTerm (`IdRef idref, t))
86
87 let instantiate32 term_info idrefs env symbol args =
88   let rec instantiate_arg = function
89     | Ast.IdentArg (n, name) ->
90         let t = 
91           try List.assoc name env 
92           with Not_found -> prerr_endline ("name not found in env: "^name);
93                             assert false
94         in
95         let rec count_lambda = function
96           | Ast.AttributedTerm (_, t) -> count_lambda t
97           | Ast.Binder (`Lambda, _, body) -> 1 + count_lambda body
98           | _ -> 0
99         in
100         let rec add_lambda t n =
101           if n > 0 then
102             let name = NotationUtil.fresh_name () in
103             Ast.Binder (`Lambda, (Ast.Ident (name, None), None),
104               Ast.Appl [add_lambda t (n - 1); Ast.Ident (name, None)])
105           else
106             t
107         in
108         add_lambda t (n - count_lambda t)
109   in
110   let head =
111     let symbol = Ast.Symbol (symbol, 0) in
112     add_idrefs idrefs symbol
113   in
114   if args = [] then head
115   else Ast.Appl (head :: List.map instantiate_arg args)
116
117 let fresh_id status =
118   let counter = status#interp_db.counter+1 in
119    status#set_interp_db ({ status#interp_db with counter = counter  }), counter
120
121 let add_interpretation (status : #status) dsc (symbol, args) appl_pattern =
122   let status,id = fresh_id status in
123   let ids =
124    try
125     StringMap.find symbol status#interp_db.interpretations
126    with Not_found -> [id] in
127   let status =
128    status#set_interp_db { status#interp_db with
129     level2_patterns32 =
130       IntMap.add id (dsc, symbol, args, appl_pattern)
131        status#interp_db.level2_patterns32;
132     pattern32_matrix = (true,appl_pattern,id)::status#interp_db.pattern32_matrix;
133     interpretations = StringMap.add symbol ids status#interp_db.interpretations
134    }
135   in
136    !load_patterns32 status#interp_db.pattern32_matrix;
137    status
138
139 let toggle_active_interpretations status b =
140   status#set_interp_db { status#interp_db with
141    pattern32_matrix =
142      List.map (fun (_,ap,id) -> b,ap,id) status#interp_db.pattern32_matrix }
143
144 exception Interpretation_not_found
145
146 let lookup_interpretations status ?(sorted=true) symbol =
147   try
148     let raw = 
149       List.map (
150         fun id ->
151           let (dsc, _, args, appl_pattern) =
152             try IntMap.find id status#interp_db.level2_patterns32
153             with Not_found -> assert false 
154           in
155           dsc, args, appl_pattern
156       ) (StringMap.find symbol status#interp_db.interpretations)
157     in
158     if sorted then HExtlib.list_uniq (List.sort Pervasives.compare raw)
159               else raw
160   with Not_found -> raise Interpretation_not_found
161
162 let instantiate_appl_pattern 
163   ~mk_appl ~mk_implicit ~term_of_nref env appl_pattern 
164 =
165   let lookup name =
166     try List.assoc name env
167     with Not_found ->
168       prerr_endline (sprintf "Name %s not found" name);
169       assert false
170   in
171   let rec aux = function
172     | Ast.NRefPattern nref -> term_of_nref nref
173     | Ast.ImplicitPattern -> mk_implicit false
174     | Ast.VarPattern name -> lookup name
175     | Ast.ApplPattern terms -> mk_appl (List.map aux terms)
176   in
177   aux appl_pattern