Skip to content

toplevel: print shorter paths for constructors/labels in submodules of open modules - #13536

Merged
gasche merged 6 commits into
ocaml:trunkfrom
gasche:genprintval-shorter-paths
Dec 17, 2024
Merged

gasche merged 6 commits into
ocaml:trunkfrom
gasche:genprintval-shorter-paths

Conversation

@gasche

@gasche gasche commented Oct 9, 2024

Copy link
Copy Markdown
Member

This is my attempt at fixing #12642. Reminder of what the issue is:

# module M = struct
    module N = struct
      type t = A | B | C
      let x = (A, B, C)
  end
end;;

(* Expected output. *)
# M.N.x;;
- : M.N.t * M.N.t * M.N.t = (M.N.A, M.N.B, M.N.C)
(* with short-paths: same output *)

# open M;;

(* Unexpected output. This is the problem here: *)
# N.x;;
- : M.N.t * M.N.t * M.N.t = (M.N.A, M.N.B, M.N.C)
(* with -short-paths:
- : N.t * N.t * N.t = (M.N.A, M.N.B, M.N.C)
*)

# open N;;

(* Expected output. *)
# x;;
- : M.N.t * M.N.t * M.N.t = (A, B, C)
(* with -short-paths:
- : t * t * t = (A, B, C)
*)

Currently, to print M.N.Foo, the toplevel printer tries to get lucky and see if just Foo works, and otherwise it prints M.N.Foo. With the proposed changes, it will try all of Foo, N.Foo and M.N.Foo in order.

(There is a surprising amount of list gymnastics involved, because paths/idents are snoc lists, and computing the list [Foo; N.Foo; M.N.Foo] is harder than I would have guessed.)

At the risk of putting too much weight on the same camel, I'm afraid that @Octachron is the perfect reviewer for this.

@gasche
gasche force-pushed the genprintval-shorter-paths branch from dffc517 to 7b53f74 Compare October 9, 2024 11:26
@gasche

gasche commented Oct 9, 2024

Copy link
Copy Markdown
Member Author

(I reduced the amount of list gymnastics involved.)

Comment thread toplevel/genprintval.ml
in
loop lid [] (* [[]; [Foo]; [N; Foo]; [M; N; Foo]] *)
|> List.filter_map Longident.unflatten
in

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation feels quite awkward when the environment for constructors and labels contains the list od successive opens. I have the impression that adding a relative_constructor_longident: Path.t -> t -> Longident to Env would be more natural.

@gasche gasche Oct 10, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the code in env.ml. Maybe TycompTbl is supposed to serve to do this, who knows? I cannot propose an alternative implementation along the lines you suggest in an acceptable amount of effort, so if you think this is a requirement to implement this feature I will have to close the current PR and let someone else work on it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have a look at implementing my suggestion at the top of this PR to make the discussion clearer.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Octachron proposed an implementation of his suggestion in gasche#15 , but my understanding from the PR and a brief discussion this morning is that this alternative approach in fact does not really work in presence of shadowing, and it may be that the approach in the present PR is in fact the simplest.

@gasche
gasche force-pushed the genprintval-shorter-paths branch 3 times, most recently from e15ef36 to 331d783 Compare October 14, 2024 13:58
@Octachron Octachron self-assigned this Oct 30, 2024
Comment thread toplevel/genprintval.ml
| Lident last -> [suff; (last :: suff)]
| Ldot(p, s) -> suff :: loop p (s :: suff)
| Lapply _ -> raise apply_exn
in

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the two functions above could be merged since computing the candidates from the Path.t is not that different from computing them from a Longident.t.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both functions are in fact relatively tricky. (The second one is your standard tricky recursive function. The first tries to find the "last" component of a path, and this is more difficult than it should be.) I just tried to merge them and failed. Maybe we could leave them as-is, or you want to propose your own merged version?

@gasche gasche Dec 12, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, maybe I have a version that works (untested):

      (* [candidates exn M.N.t] is [Foo; N.Foo; M.N.Foo].
         @raise [exn] on functor application. *)
      let candidates apply_exn path =
        (* [loop M.N.t [Foo]] is [[Foo]; [N; Foo]; [M; N; Foo]] *)
        let rec loop ~is_last path suff = match path with
          | Pident id -> [suff; (if is_last then name else id) :: suff]
          | Pdot(p, s) -> suff :: loop ~is_last:false p ((if is_last then name else s) :: suff)
          | Papply _ -> raise apply_exn
          | Pextra_ty (p, _) ->
              (* These can only appear directly inside of the associated
                 constructor so we can just drop the prefix *)
              loop path p suff
        in
        loop ~is_last:true path [] (* [[]; [Foo]; [N; Foo]; [M; N; Foo]] *)
        |> List.filter_map Longident.unflatten
      in

I personally find this unreadable and would have a preference for keeping the two functions separate.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I agree that this function is mixing too many concern. Last light suggestion with code:

      let lid = match Untypeast.lident_of_path ty_path with
        | Lident _ -> Lident name
        | Ldot (p,_) -> Ldot(p,name)
        | x -> x
       in

while keeping candidates, that you can use if you wish and then I will stop side-nitpicking on the lack of helper functions in the typechecker.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I rebased the PR -- and added your "hellish example" below in the testsuite.

let it = Q.Sub.A
[%%expect {|
val it : Q.Sub.t = Q.Sub.A
|}]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A hellish example that we might want to keep in a corner:

module M = struct
  module N = struct
    module A = struct
      module B = struct
        type t = X
      end
    end
    module F(X:sig type t end) = struct type t = A of X.t end
  end
end
open M open N open A open B
let x = let module FB = F(B) in FB.A X

@Octachron Octachron left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the implementation works, and it will not be a performance bottleneck ever, so we should merge it.

@gasche
gasche force-pushed the genprintval-shorter-paths branch 2 times, most recently from 0bfb92e to ccb069c Compare December 12, 2024 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants