Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

be able to print any type #165

Open
acarumut opened this issue Oct 5, 2022 · 6 comments
Open

be able to print any type #165

acarumut opened this issue Oct 5, 2022 · 6 comments
Labels

Comments

@acarumut
Copy link
Contributor

acarumut commented Oct 5, 2022

it is a pain to have to write print functions
i have encountered a good example (simplifying here to make the point)

i was writing a function

fun f (x: 'a datastructure): 'a datastructure = 
... 
bunch of code

now i am testing with 'a = int but if i try to print x inside f, then i break polymorphism because i am assuming int :(
so basically i have to change my type signature etc...

@shwestrick
Copy link
Collaborator

Yeah this would be fantastic to have. Conceptually it seems straightforward for the compiler to provide a polymorphic primitive toString: 'a -> string. We could elaborate it into real code immediately after monomorphization.

Generating pretty printers automatically for arbitrary types seems like it could be a pain! Perhaps we could start with simple things (e.g. just base types, integers, tuples, etc.) and extend it incrementally with support for more interesting stuff.

@umutacar
Copy link
Collaborator

umutacar commented Oct 11, 2022

would it be feasible to extend the compiler with an "anytime" expression that gets passed through the various stages of the compilation? if so, then this and similar things could be easy to implement perhaps?
(this feature would be used only by the compiler, so we would not expose it to the user.)

@shwestrick
Copy link
Collaborator

Yeah, MLton/MPL essentially already have these! MLton calls them "primitive" operations. Primitives can pass through all compilation stages if needed, and different primitives are eliminated (i.e., implemented) by different passes.

@umutacar
Copy link
Collaborator

maybe we need a to_string primitive?

@shwestrick
Copy link
Collaborator

Yup! That's what I was imagining above. The primitive can then be eliminated (elaborated into real code) at any point after the monomorphization pass.

@acarumut
Copy link
Contributor Author

cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants