X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fmatita%2FmatitaDisambiguator.ml;h=db69cb71dfdd0eedf5050d3f03278e4106c24084;hb=6565cd51fb866a80838003cd65dc00e4d5a9814b;hp=87215fba177db6ea4cc9ed50e6b7ab8249b499c5;hpb=56415e42c04f40e9c8f7cfc59a3a3d87c3d373f7;p=helm.git diff --git a/helm/matita/matitaDisambiguator.ml b/helm/matita/matitaDisambiguator.ml index 87215fba1..db69cb71d 100644 --- a/helm/matita/matitaDisambiguator.ml +++ b/helm/matita/matitaDisambiguator.ml @@ -1,4 +1,4 @@ -(* Copyright (C) 2004, HELM Team. +(* Copyright (C) 2004-2005, HELM Team. * * This file is part of HELM, an Hypertextual, Electronic * Library of Mathematics, developed at the Computer Science @@ -23,71 +23,103 @@ * http://helm.cs.unibo.it/ *) -class parserr () = - object - method parseTerm (stream: char Stream.t) = - CicTextualParser2.parse_term stream - - (* TODO Zack: implements methods below *) - method parseTactic (_: char Stream.t) : DisambiguateTypes.tactic = - MatitaTypes.not_implemented "parserr.parseTactic" - method parseTactical (_: char Stream.t) : DisambiguateTypes.tactical = - MatitaTypes.not_implemented "parserr.parseTactical" - method parseCommand (_: char Stream.t) : DisambiguateTypes.command = - MatitaTypes.not_implemented "parserr.parseCommand" - method parseScript (_: char Stream.t) : DisambiguateTypes.script = - MatitaTypes.not_implemented "parserr.parseScript" +open Printf + +open MatitaTypes + +exception Ambiguous_input + +type choose_uris_callback = + id:string -> UriManager.uri list -> UriManager.uri list + +type choose_interp_callback = (string * string) list list -> int list + +let mono_uris_callback ~id = + if Helm_registry.get_bool "matita.auto_disambiguation" then + function l -> l + else + raise Ambiguous_input + +let mono_interp_callback _ = raise Ambiguous_input + +let _choose_uris_callback = ref mono_uris_callback +let _choose_interp_callback = ref mono_interp_callback +let set_choose_uris_callback f = _choose_uris_callback := f +let set_choose_interp_callback f = _choose_interp_callback := f + +module Callbacks = + struct + let interactive_user_uri_choice ~selection_mode ?ok + ?(enable_button_for_non_vars = true) ~title ~msg ~id uris = + !_choose_uris_callback ~id uris + + let interactive_interpretation_choice interp = + !_choose_interp_callback interp + + let input_or_locate_uri ~(title:string) ?id = + (* Zack: I try to avoid using this callback. I therefore assume that + * the presence of an identifier that can't be resolved via "locate" + * query is a syntax error *) + let msg = match id with Some id -> id | _ -> "_" in + raise (Unbound_identifier msg) end + +module Disambiguator = Disambiguate.Make (Callbacks) + +(* implement module's API *) -class disambiguator - ~parserr ~mqiconn ~(chooseUris: MatitaTypes.choose_uris_callback) - ~(chooseInterp: MatitaTypes.choose_interp_callback) () - = - let disambiguate_term = - let module Callbacks = - struct - let interactive_user_uri_choice - ~selection_mode ?ok ?(enable_button_for_non_vars = true) ~title ~msg - ~id uris - = - chooseUris ~selection_mode ~title ~msg - ~nonvars_button:enable_button_for_non_vars uris - - let interactive_interpretation_choice = chooseInterp - let input_or_locate_uri ~(title:string) = - (* TODO Zack: I try to avoid using this callback. I therefore assume - * that the presence of an identifier that can't be resolved via - * "locate" query is a syntax error *) - MatitaTypes.not_implemented - "MatitaDisambiguator: input_or_locate_uri callback" - end - in - let module Disambiguator = Disambiguate.Make (Callbacks) in - Disambiguator.disambiguate_term +let disambiguate_thing ~aliases + ~(f:?fresh_instances:bool -> aliases:Disambiguate.aliases -> 'a -> 'b) + ~(set_aliases: DisambiguateTypes.environment -> 'b -> 'b) + (thing: 'a) += + let library = false, DisambiguateTypes.empty_environment in + let multi_aliases = true, aliases in + let mono_aliases = false, aliases in + let passes = (* *) + [ (false, mono_aliases, false); + (false, multi_aliases, false); + (true, library, false); + (true, mono_aliases, true); + (true, multi_aliases, true); + (true, library, true); + ] in - object (self) - val mutable parserr: parserr = parserr - method parserr = parserr - method setParserr p = parserr <- p - - val mutable _env = DisambiguateTypes.Environment.empty - method env = _env - method setEnv e = _env <- e - - method disambiguateTermAst ?(context = []) ?(metasenv = []) ?env termAst = - let (save_state, env) = - match env with - | Some env -> (false, env) - | None -> (true, _env) - in - match disambiguate_term mqiconn context metasenv termAst ~aliases:env with - | [ (env, metasenv, term) as x ] -> - if save_state then self#setEnv env; - x - | _ -> assert false - - method disambiguateTerm ?context ?metasenv ?env stream = - self#disambiguateTermAst ?context ?metasenv ?env - (parserr#parseTerm stream) - end + let try_pass (fresh_instances, aliases, use_coercions) = + CoercDb.use_coercions := use_coercions; + f ~fresh_instances ~aliases thing + in + let rec aux = + function + | [ pass ] -> try_pass pass + | hd :: tl -> + (try + try_pass hd + with Disambiguate.NoWellTypedInterpretation -> + let (_, user_asked) as res = aux tl in + if user_asked then res else set_aliases aliases res) + | [] -> assert false + in + let saved_use_coercions = !CoercDb.use_coercions in + try + let res = aux passes in + CoercDb.use_coercions := saved_use_coercions; + res + with exn -> + CoercDb.use_coercions := saved_use_coercions; + raise exn + +let set_aliases aliases (choices, user_asked) = + (List.map (fun (_, a, b, c) -> aliases, a, b, c) choices), + user_asked + +let disambiguate_term ~dbd ~context ~metasenv ?initial_ugraph ~aliases term = + let f = + Disambiguator.disambiguate_term ~dbd ~context ~metasenv ?initial_ugraph + in + disambiguate_thing ~aliases ~f ~set_aliases term + +let disambiguate_obj ~dbd ~aliases ~uri obj = + let f = Disambiguator.disambiguate_obj ~dbd ~uri in + disambiguate_thing ~aliases ~f ~set_aliases obj