diku-dk / futhark

:boom::computer::boom: A data-parallel functional programming language
http://futhark-lang.org
ISC License
2.4k stars 167 forks source link

Add expression attribute for repl display #1567

Open leonardschneider opened 2 years ago

leonardschneider commented 2 years ago

I like the Futhark repl, but I'm missing formatting while printing the values. I propose adding some basic formatting, which can come handy while debugging a program, by adding expression attributes that will be interpreted by the repl to display expressions.

Here are some examples I'd like to have.

> #[char] 97u8 -- display a character
'a'
> #[utf8] "Hello, world!" -- display a string in utf8 encoding
"Hello, world"
> #[raw] "Hello, world!" -- direct display for extensibility
Hello, world! 
athas commented 2 years ago

I don't think an attribute is the right way to do this. What about a :string command that prints the result of the expression as a string literal (if it has an appropriate type)?

FluxusMagna commented 2 years ago

That could probably also be useful in futhark literate.

athas commented 2 years ago

Yeah, now that I think about it, maybe a general :format command that accepts a printf-style format string would be better. The same machinery could be used for futhark literate.

leonardschneider commented 2 years ago

You're right. I've played with a prototype and a command makes more sense.

athas commented 2 years ago

Instead of printf style, I think the most elegant option would be something similar to Pythons formatted string literals. They are similar to printf strings, but instead of being positional, you put the name (or eventually, expression) of what you want to print:

> let str = "foo"
> :print "as array: {str}"
as array: [102u8, 111u8, 111u8]
> :print "as string: {str:s}"
as string: foo

With no specifier it will print in the default manner for the type, but with a specifier we can force e.g. arrays to be interpreted as UTF-8 strings.