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