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