]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaEngine.ml
test branch
[helm.git] / helm / matita / matitaEngine.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 let debug = false ;;
31 let debug_print = if debug then prerr_endline else ignore ;;
32
33 let disambiguate_tactic lexicon_status_ref grafite_status goal tac =
34  let metasenv,tac =
35   GrafiteDisambiguate.disambiguate_tactic
36    lexicon_status_ref
37    (GrafiteTypes.get_proof_context grafite_status goal)
38    (GrafiteTypes.get_proof_metasenv grafite_status)
39    tac
40  in
41   GrafiteTypes.set_metasenv metasenv grafite_status,tac
42
43 let disambiguate_command lexicon_status_ref grafite_status cmd =
44  let lexicon_status,metasenv,cmd =
45   GrafiteDisambiguate.disambiguate_command
46    ~baseuri:(
47      try
48       Some (GrafiteTypes.get_string_option grafite_status "baseuri")
49      with
50       GrafiteTypes.Option_error _ -> None)
51    !lexicon_status_ref (GrafiteTypes.get_proof_metasenv grafite_status) cmd
52  in
53   lexicon_status_ref := lexicon_status;
54   GrafiteTypes.set_metasenv metasenv grafite_status,cmd
55
56 let disambiguate_macro lexicon_status_ref grafite_status macro context =
57  let metasenv,macro =
58   GrafiteDisambiguate.disambiguate_macro
59    lexicon_status_ref
60    (GrafiteTypes.get_proof_metasenv grafite_status)
61    context macro
62  in
63   GrafiteTypes.set_metasenv metasenv grafite_status,macro
64
65 let eval_ast ?do_heavy_checks ?clean_baseuri lexicon_status
66  grafite_status ast
67 =
68  let lexicon_status_ref = ref lexicon_status in
69  let new_grafite_status,new_objs =
70   GrafiteEngine.eval_ast
71    ~disambiguate_tactic:(disambiguate_tactic lexicon_status_ref)
72    ~disambiguate_command:(disambiguate_command lexicon_status_ref)
73    ~disambiguate_macro:(disambiguate_macro lexicon_status_ref)
74    ?do_heavy_checks ?clean_baseuri grafite_status ast in
75  let new_lexicon_status =
76   LexiconSync.add_aliases_for_objs !lexicon_status_ref new_objs in
77  let new_aliases =
78   LexiconSync.alias_diff ~from:lexicon_status new_lexicon_status in
79  let _,intermediate_states = 
80   let baseuri = GrafiteTypes.get_string_option new_grafite_status "baseuri" in
81   List.fold_left
82    (fun (lexicon_status,acc) (k,((v,_) as value)) -> 
83      let b =
84       try
85        UriManager.buri_of_uri (UriManager.uri_of_string v) = baseuri
86       with
87        UriManager.IllFormedUri _ -> false (* v is a description, not a URI *)
88      in
89       if b then 
90        lexicon_status,acc
91       else
92        let new_lexicon_status =
93         LexiconEngine.set_proof_aliases lexicon_status [k,value]
94        in
95         new_lexicon_status,
96          ((new_grafite_status,new_lexicon_status),Some (k,value))::acc
97    ) (lexicon_status,[]) new_aliases
98  in
99   ((new_grafite_status,new_lexicon_status),None)::intermediate_states
100
101 let eval_from_stream ~first_statement_only ~include_paths ?(prompt=false)
102  ?do_heavy_checks ?clean_baseuri lexicon_status grafite_status str cb 
103 =
104  let rec loop lexicon_status grafite_status statuses =
105   let loop =
106    if first_statement_only then
107     fun _ _ _ -> raise End_of_file
108    else
109     loop
110   in
111    if prompt then (print_string "matita> "; flush stdout);
112    try
113     let lexicon_status,ast =
114      GrafiteParser.parse_statement ~include_paths str lexicon_status
115     in
116      (match ast with
117          GrafiteParser.LNone _ ->
118           loop lexicon_status grafite_status
119            (((grafite_status,lexicon_status),None)::statuses)
120        | GrafiteParser.LSome ast ->
121           cb grafite_status ast;
122           let new_statuses =
123            eval_ast ?do_heavy_checks ?clean_baseuri lexicon_status
124             grafite_status ast in
125           let grafite_status,lexicon_status =
126            match new_statuses with
127               [] -> assert false
128             | (s,_)::_ -> s
129           in
130            loop lexicon_status grafite_status (new_statuses @ statuses))
131    with
132     End_of_file -> statuses
133  in
134   loop lexicon_status grafite_status []
135 ;;
136
137 let eval_string ~first_statement_only ~include_paths ?do_heavy_checks
138  ?clean_baseuri lexicon_status status str
139 =
140  eval_from_stream ~first_statement_only ~include_paths ?do_heavy_checks
141   ?clean_baseuri lexicon_status status (Ulexing.from_utf8_string str)
142   (fun _ _ -> ())