]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaInterpreter.ml
snapshot
[helm.git] / helm / matita / matitaInterpreter.ml
1 (* Copyright (C) 2004, 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 (** Interpreter for textual phrases coming from matita's console (textual entry
27 * window at the bottom of the main window).
28 *
29 * Interpreter is either in `Command state or in `Proof state (see state_tag type
30 * below). In `Command state commands for starting proofs are accepted, but
31 * tactic and tactical applications are not. In `Proof state both
32 * tactic/tacticals and commands are accepted.
33 *)
34
35 open Printf
36
37 type state_tag = [ `Command | `Proof ]
38
39 exception Command_error of string
40
41 class virtual interpreterState ~(console: MatitaConsole.console) =
42   object (self)
43       (** eval a toplevel phrase in the current state and return the new state
44       *)
45     method parsePhrase s = CicTextualParser2.parse_tactical (Stream.of_string s)
46
47     method virtual evalTactical:
48       (CicAst.term, string) TacticAst.tactical -> state_tag
49
50     method evalPhrase s = self#evalTactical (self#parsePhrase s)
51   end
52
53   (** Implements phrases that should be accepted in all states *)
54 class sharedState
55   ~(disambiguator: MatitaTypes.disambiguator)
56   ~(proof_handler: MatitaTypes.proof_handler)
57   ~(console: MatitaConsole.console)
58   ()
59 =
60   object (self)
61     inherit interpreterState ~console
62     method evalTactical = function
63       | TacticAst.Command TacticAst.Quit ->
64           proof_handler.MatitaTypes.quit ();
65           `Command (* dummy answer, useless *)
66       | TacticAst.Command TacticAst.Proof ->
67             (* do nothing, just for compatibility with coq syntax *)
68           `Command
69       | tactical ->
70           raise (Command_error (TacticAstPp.pp_tactical tactical))
71   end
72
73   (** Implements phrases that should be accepted only in `Command state *)
74 class commandState
75   ~(disambiguator: MatitaTypes.disambiguator)
76   ~(proof_handler: MatitaTypes.proof_handler)
77   ~(console: MatitaConsole.console)
78   ()
79 =
80   let shared = new sharedState ~disambiguator ~proof_handler ~console () in
81   object (self)
82     inherit interpreterState ~console
83
84     method evalTactical = function
85       | TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical
86       | TacticAst.Command (TacticAst.Theorem (_, Some name, ast, None)) ->
87           let (_, metasenv, expr) = disambiguator#disambiguateTermAst ast in
88           let proof = MatitaProof.proof ~typ:expr ~metasenv () in
89           proof_handler.MatitaTypes.new_proof proof;
90           `Proof
91       | TacticAst.Command TacticAst.Quit ->
92           proof_handler.MatitaTypes.quit ();
93           `Command (* dummy answer, useless *)
94       | TacticAst.Command TacticAst.Proof ->
95             (* do nothing, just for compatibility with coq syntax *)
96           `Command
97       | tactical -> shared#evalTactical tactical
98   end
99
100 let canonical_context metano metasenv =
101   try
102     let (_, context, _) = List.find (fun (m, _, _) -> m = metano) metasenv in
103     context
104   with Not_found ->
105     failwith (sprintf "Can't find canonical context for %d" metano)
106
107   (** create a ProofEngineTypes.mk_fresh_name_type function which uses given
108   * names as long as they are available, then it fallbacks to name generation
109   * using FreshNamesGenerator module *)
110 let namer_of names =
111   let len = List.length names in
112   let count = ref 0 in
113   fun metasenv context name ~typ ->
114     if !count < len then begin
115       let name = Cic.Name (List.nth names !count) in
116       incr count;
117       name
118     end else
119       FreshNamesGenerator.mk_fresh_name metasenv context name ~typ
120
121   (** Implements phrases that should be accepted only in `Proof state, basically
122   * tacticals *)
123 class proofState
124   ~(disambiguator: MatitaTypes.disambiguator)
125   ~(proof_handler: MatitaTypes.proof_handler)
126   ~(console: MatitaConsole.console)
127   ()
128 =
129     (** term AST -> Cic.term. Uses disambiguator and change imperatively the
130     * metasenv as needed *)
131   let disambiguate ast =
132     let proof = proof_handler.MatitaTypes.get_proof () in
133     let metasenv = proof#metasenv in
134     let goal =  proof#goal in
135     let context = canonical_context goal metasenv in
136     let (_, metasenv, term) =
137       disambiguator#disambiguateTermAst ~context ~metasenv ast
138     in
139     proof#set_metasenv metasenv;
140     term
141   in
142     (** tactic AST -> ProofEngineTypes.tactic *)
143   let rec lookup_tactic = function
144     | TacticAst.LocatedTactic (_, tactic) -> lookup_tactic tactic
145     | TacticAst.Intros (_, names) ->  (* TODO Zack implement intros length *)
146         PrimitiveTactics.intros_tac ~mk_fresh_name_callback:(namer_of names) ()
147     | TacticAst.Reflexivity -> EqualityTactics.reflexivity_tac
148     | TacticAst.Assumption -> VariousTactics.assumption_tac
149     | TacticAst.Contradiction -> NegationTactics.contradiction_tac
150     | TacticAst.Exists -> IntroductionTactics.exists_tac
151     | TacticAst.Fourier -> FourierR.fourier_tac
152     | TacticAst.Left -> IntroductionTactics.left_tac
153     | TacticAst.Right -> IntroductionTactics.right_tac
154     | TacticAst.Ring -> Ring.ring_tac
155     | TacticAst.Split -> IntroductionTactics.split_tac
156     | TacticAst.Symmetry -> EqualityTactics.symmetry_tac
157     | TacticAst.Transitivity term ->
158         EqualityTactics.transitivity_tac (disambiguate term)
159     | TacticAst.Apply term -> PrimitiveTactics.apply_tac (disambiguate term)
160     | TacticAst.Exact term -> PrimitiveTactics.exact_tac (disambiguate term)
161     | TacticAst.Cut term -> PrimitiveTactics.cut_tac (disambiguate term)
162     | TacticAst.ElimType term ->
163         EliminationTactics.elim_type_tac (disambiguate term)
164     | TacticAst.Replace (what, with_what) ->
165         EqualityTactics.replace_tac ~what:(disambiguate what)
166           ~with_what:(disambiguate with_what)
167   (*
168     (* TODO Zack a lot more of tactics to be implemented here ... *)
169     | TacticAst.Absurd
170     | TacticAst.Change of 'term * 'term * 'ident option
171     | TacticAst.Change_pattern of 'term pattern * 'term * 'ident option
172     | TacticAst.Decompose of 'ident * 'ident list
173     | TacticAst.Discriminate of 'ident
174     | TacticAst.Elim of 'term * 'term option
175     | TacticAst.Fold of reduction_kind * 'term
176     | TacticAst.Injection of 'ident
177     | TacticAst.LetIn of 'term * 'ident
178     | TacticAst.Reduce of reduction_kind * 'term pattern * 'ident option
179     | TacticAst.Replace_pattern of 'term pattern * 'term
180     | TacticAst.Rewrite of direction * 'term * 'ident option
181   *)
182     | _ ->
183         MatitaTypes.not_implemented "some tactic"
184   in
185   let shared = new sharedState ~disambiguator ~proof_handler ~console () in
186   object (self)
187     inherit interpreterState ~console
188
189     method evalTactical = function
190       | TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical
191       | TacticAst.Command TacticAst.Abort ->
192           proof_handler.MatitaTypes.abort_proof ();
193           `Command
194       | TacticAst.Command (TacticAst.Undo steps) ->
195           (proof_handler.MatitaTypes.get_proof ())#undo ?steps ();
196           `Proof
197       | TacticAst.Command (TacticAst.Redo steps) ->
198           (proof_handler.MatitaTypes.get_proof ())#redo ?steps ();
199           `Proof
200       | TacticAst.Seq tacticals ->
201           (* TODO Zack check for proof completed at each step? *)
202           List.iter (fun t -> ignore (self#evalTactical t)) tacticals;
203           `Proof
204       | TacticAst.Tactic tactic_phrase ->
205           let tactic = lookup_tactic tactic_phrase in
206           (proof_handler.MatitaTypes.get_proof ())#apply_tactic tactic;
207           `Proof
208       | tactical -> shared#evalTactical tactical
209   end
210
211 class interpreter
212   ~(disambiguator: MatitaTypes.disambiguator)
213   ~(proof_handler: MatitaTypes.proof_handler)
214   ~(console: MatitaConsole.console)
215   ()
216 =
217   let commandState =
218     new commandState ~disambiguator ~proof_handler ~console ()
219   in
220   let proofState = new proofState ~disambiguator ~proof_handler ~console () in
221   object
222     val mutable state = commandState
223
224     method evalPhrase s =
225       try
226         (match state#evalPhrase s with
227         | `Command -> state <- commandState
228         | `Proof -> state <- proofState)
229       with exn ->
230         console#echo_error (sprintf "Uncaught exception: %s"
231           (Printexc.to_string exn))
232   end
233