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