1 (* Copyright (C) 2004, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
26 (** Interpreter for textual phrases coming from matita's console (textual entry
27 * window at the bottom of the main window).
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.
37 type state_tag = [ `Command | `Proof ]
39 exception Command_error of string
41 class virtual interpreterState ~(console: MatitaConsole.console) =
43 (** eval a toplevel phrase in the current state and return the new state
45 method parsePhrase s = CicTextualParser2.parse_tactical (Stream.of_string s)
47 method virtual evalTactical:
48 (CicAst.term, string) TacticAst.tactical -> state_tag
50 method evalPhrase s = self#evalTactical (self#parsePhrase s)
53 (** Implements phrases that should be accepted in all states *)
55 ~(disambiguator: MatitaTypes.disambiguator)
56 ~(proof_handler: MatitaTypes.proof_handler)
57 ~(console: MatitaConsole.console)
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 *)
70 raise (Command_error (TacticAstPp.pp_tactical tactical))
73 (** Implements phrases that should be accepted only in `Command state *)
75 ~(disambiguator: MatitaTypes.disambiguator)
76 ~(proof_handler: MatitaTypes.proof_handler)
77 ~(console: MatitaConsole.console)
80 let shared = new sharedState ~disambiguator ~proof_handler ~console () in
82 inherit interpreterState ~console
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;
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 *)
97 | tactical -> shared#evalTactical tactical
100 let canonical_context metano metasenv =
102 let (_, context, _) = List.find (fun (m, _, _) -> m = metano) metasenv in
105 failwith (sprintf "Can't find canonical context for %d" metano)
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 *)
111 let len = List.length names in
113 fun metasenv context name ~typ ->
114 if !count < len then begin
115 let name = Cic.Name (List.nth names !count) in
119 FreshNamesGenerator.mk_fresh_name metasenv context name ~typ
121 (** Implements phrases that should be accepted only in `Proof state, basically
124 ~(disambiguator: MatitaTypes.disambiguator)
125 ~(proof_handler: MatitaTypes.proof_handler)
126 ~(console: MatitaConsole.console)
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
139 proof#set_metasenv metasenv;
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)
168 (* TODO Zack a lot more of tactics to be implemented here ... *)
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
183 MatitaTypes.not_implemented "some tactic"
185 let shared = new sharedState ~disambiguator ~proof_handler ~console () in
187 inherit interpreterState ~console
189 method evalTactical = function
190 | TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical
191 | TacticAst.Command TacticAst.Abort ->
192 proof_handler.MatitaTypes.abort_proof ();
194 | TacticAst.Command (TacticAst.Undo steps) ->
195 (proof_handler.MatitaTypes.get_proof ())#undo ?steps ();
197 | TacticAst.Command (TacticAst.Redo steps) ->
198 (proof_handler.MatitaTypes.get_proof ())#redo ?steps ();
200 | TacticAst.Seq tacticals ->
201 (* TODO Zack check for proof completed at each step? *)
202 List.iter (fun t -> ignore (self#evalTactical t)) tacticals;
204 | TacticAst.Tactic tactic_phrase ->
205 let tactic = lookup_tactic tactic_phrase in
206 (proof_handler.MatitaTypes.get_proof ())#apply_tactic tactic;
208 | tactical -> shared#evalTactical tactical
212 ~(disambiguator: MatitaTypes.disambiguator)
213 ~(proof_handler: MatitaTypes.proof_handler)
214 ~(console: MatitaConsole.console)
218 new commandState ~disambiguator ~proof_handler ~console ()
220 let proofState = new proofState ~disambiguator ~proof_handler ~console () in
222 val mutable state = commandState
224 method evalPhrase s =
226 (match state#evalPhrase s with
227 | `Command -> state <- commandState
228 | `Proof -> state <- proofState)
230 console#echo_error (sprintf "Uncaught exception: %s"
231 (Printexc.to_string exn))