Skip to content

Commit

Permalink
[hover] Fix universe printing in hover.
Browse files Browse the repository at this point in the history
Our code was incomplete. The API here could be really improved
Coq-side. About is too verbose for hover, hence our code. We could opt
for the version in `prettyp.ml` tho.

Fixes #835
  • Loading branch information
ejgallego committed Sep 28, 2024
1 parent 46679d4 commit 79d3065
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- [serlib] Fix Ltac2 AST piercing bug, add test case that should help
in the future (@ejgallego, @jim-portegies, #821)
- [fleche] [8.20] understand rewrite rules and symbols on document
outline (@ejgallego, @Alizter, #825, fixes: #824)
outline (@ejgallego, @Alizter, #825, fixes #824)
- [fleche] [coq] support `Restart` meta command (@ejgallego,
@Alizter, #828, fixes #827)
- [fleche] [plugins] New plugin example `explain_errors`, that will
Expand All @@ -23,6 +23,8 @@
provides a full working Coq enviroment in `vscode.dev`. The web
worker version is build as an artifact on CI (@ejgallego
@corwin-of-amber, #433)
- [hover] Fix universe and level printing in hover (#839, fixes #835
, @ejgallego , @Alizter)

# coq-lsp 0.2.0: From Green to Blue
-----------------------------------
Expand Down
55 changes: 45 additions & 10 deletions controller/rq_hover.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,21 @@ let build_ind_type mip = Inductive.type_of_inductive mip

type id_info =
| Notation of Pp.t
| Def of (Pp.t * Names.Constant.t option * string option)
| Def of (Pp.t * Pp.t * Names.Constant.t option * string option)

let info_of_ind env sigma ((sp, i) : Names.Ind.t) =
let print_params env sigma params =
if CList.is_empty params then Pp.mt ()
else Pp.(spc () ++ Printer.pr_rel_context env sigma params ++ brk (1, 2))

let info_of_ind env ((sp, i) : Names.Ind.t) =
let udecl = None in
let mib = Environ.lookup_mind sp env in
let bl =
Printer.universe_binders_with_opt_names
(Declareops.inductive_polymorphic_context mib)
udecl
in
let sigma = Evd.from_ctx (UState.of_names bl) in
let u =
UVars.make_abstract_instance (Declareops.inductive_polymorphic_context mib)
in
Expand All @@ -37,17 +48,34 @@ let info_of_ind env sigma ((sp, i) : Names.Ind.t) =
(Impargs.implicits_of_global (Names.GlobRef.IndRef (sp, i)))
in
let impargs = List.map Impargs.binding_kind_of_status impargs in
Def (Printer.pr_ltype_env ~impargs env_params sigma arity, None, None)
let inst =
if Declareops.inductive_is_polymorphic mib then
Printer.pr_universe_instance sigma u
else Pp.mt ()
in
let params = EConstr.Unsafe.to_rel_context params in
let typ = Printer.pr_ltype_env ~impargs env_params sigma arity in
let params = Pp.(inst ++ print_params env sigma params) in
Def (typ, params, None, None)

let type_of_constant cb = cb.Declarations.const_type

let info_of_const env sigma cr =
let info_of_const env cr =
let udecl = None in
let cdef = Environ.lookup_constant cr env in
let bl =
Printer.universe_binders_with_opt_names
(Environ.constant_context env cr)
udecl
in
let sigma = Evd.from_ctx (UState.of_names bl) in
(* This prints the definition *)
(* let cb = Environ.lookup_constant cr env in *)
(* Option.cata (fun (cb,_univs,_uctx) -> Some cb ) None *)
(* (Global.body_of_constant_body Library.indirect_accessor cb), *)
let typ = type_of_constant cdef in
let univs = Declareops.constant_polymorphic_context cdef in
let inst = UVars.make_abstract_instance univs in
let impargs =
Impargs.select_stronger_impargs
(Impargs.implicits_of_global (Names.GlobRef.ConstRef cr))
Expand All @@ -56,7 +84,12 @@ let info_of_const env sigma cr =
let typ = Printer.pr_ltype_env env sigma ~impargs typ in
let dp = Names.Constant.modpath cr |> Names.ModPath.dp in
let source = Coq.Module.(make dp |> Result.to_option |> Option.map source) in
Def (typ, Some cr, source)
let inst =
if Environ.polymorphic_constant cr env then
Printer.pr_universe_instance sigma inst
else Pp.mt ()
in
Def (typ, inst, Some cr, source)

let info_of_var env vr =
let vdef = Environ.lookup_named vr env in
Expand All @@ -73,7 +106,8 @@ let info_of_constructor env cr =
in
ctype

let print_type env sigma x = Def (Printer.pr_ltype_env env sigma x, None, None)
let print_type env sigma x =
Def (Printer.pr_ltype_env env sigma x, Pp.mt (), None, None)

let info_of_id env sigma id =
let qid = Libnames.qualid_of_string id in
Expand All @@ -89,8 +123,8 @@ let info_of_id env sigma id =
let open Names.GlobRef in
(match lid with
| VarRef vr -> info_of_var env vr |> print_type env sigma
| ConstRef cr -> info_of_const env sigma cr
| IndRef ir -> info_of_ind env sigma ir
| ConstRef cr -> info_of_const env cr
| IndRef ir -> info_of_ind env ir
| ConstructRef cr -> info_of_constructor env cr |> print_type env sigma)
|> fun x -> Some x
| Abbrev kn ->
Expand Down Expand Up @@ -128,10 +162,11 @@ let pp_file fmt = function
| Some file -> Format.fprintf fmt " - **in file**: `%s`" file

let pp_typ id = function
| Def (typ, cr, file) ->
| Def (typ, param, cr, file) ->
let typ = Pp.string_of_ppcmds typ in
let param = Pp.string_of_ppcmds param in
Format.(
asprintf "@[```coq\n%s : %s@\n```@\n@[%a@]@[%a@]@]" id typ pp_cr cr
asprintf "@[```coq\n%s%s: %s@\n```@\n@[%a@]@[%a@]@]" id param typ pp_cr cr
pp_file file)
| Notation nt ->
let nt = Pp.string_of_ppcmds nt in
Expand Down
14 changes: 14 additions & 0 deletions examples/print_univs.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
From Coq Require Import Prelude.
Set Printing Universes.
Set Universe Polymorphism.

Definition arr (S: Type) : Type := S.

Print arr.

Inductive foo (M : Type) : Type -> Type :=
bar : M -> Type -> foo M nat.

Print foo.

About foo.

0 comments on commit 79d3065

Please sign in to comment.