r/ProgrammingLanguages • u/Inconstant_Moo 🧿 Pipefish • May 08 '23
Requesting criticism What the imperative shell of an Functional Core/Imperative Shell language looks like
So I've been struggling for a while to come up with a way of doing IO that is consistent and extensible and suitable for the language. What do I mean by that?
- ''Consistent'': it should look and feel like you're doing the same sort of thing whether you're talking to a file, a clock, a random number generator, a REST app, a bytestream ...
- ''Extensible''. Users should be able to add their own IO by wrapping Charm around embedded Go, it shouldn't be something that can be done only by me by hard-wiring stuff.
- ''Suitable for the language''. Charm is a Functional Core/Imperative Shell language. What does IO look like in such a language?
And that last question is very much asking "What should the imperative shell of a FC/IS language look like?" because the imperative shell is there to do only two things — mutate the state and do IO. Well, I'm happy with my syntax for mutating state, writing foo = bar has worked well for me. How to do IO is literally everything else.
So this is what I came up with. A first draft, please tell me what you think.
In most languages, there isn't a fundamental distinction between a function that gets e.g. what time it is now from all the other functions that handle time. Or between a function that returns a random number from 1 to 10 and one that returns the sine of an angle.
In Charm, however, the impure things are special. For one thing, they can't be functions — functions are pure and live in the functional core. Looking at the outside world is impure and must be done in the imperative shell by issuing imperative commands, as demonstrated here in the REPL (having first run a script declaring a variable z to keep data in):
#0 → get z from Random 6
ok
#0 → z
5
#0 → get z from UnixClock SECONDS
ok
#0 → z
1683493967
#0 → get z from Input "What's your name? "
What's your name? Marmaduke
ok
#0 → z
Marmaduke
#0 → get z from File "examples/poem.txt"
ok
#0 → z
Love is like
a pineapple,
sweet and
undefinable.
#0 → So the syntax is get <variable name> from <struct object>. This is nicely general, the struct can represent a random-number generator, a file, a clock, a bytestream, an HTTP service, or whatever. (In these examples I've just constructed the objects on the fly, but of course there's nothing to stop you defining a constant D20 = Random 20, for example, and in the case of a stream you would certainly want to persist the object locally or globally.)
Then output is done in a similar way:
#0 → post "Hello output!" to Output()
Hello output!
#0 → put 42 into RandomSeed()
ok
#0 → post "Some text" to File "zort.txt"
ok
#0 → post "Some different text" to File "zort.txt"
[0] Error: file 'zort.txt' already exists at line 153:50-56 of 'lib/world.ch'
#0 → put "Some different text" into File "zort.txt"
ok
#0 → get z from File "zort.txt"
ok
#0 → post z to Output()
Some different text
#0 → delete File "zort.txt"
ok
#0 → (Many thanks to u/lassehp for suggesting HTTP as a model.)
None of this has to be hardwired into the language. If there's a Go library for talking to something, it's a work of minutes for anyone who pleases to write their own get and put and post and delete commands for accessing it.
Here's some IO in the wild: this is the entire imperative shell of my little example adventure game. Note how in the imperative shell you can create local variables by assigning things to them, and that there's an imperative loop construct — at this point the functional core of Charm and its imperative shell are pretty much two languages unified by a type system.
cmd
main :
get linesToProcess from File "examples/locations.rsc", list
state = state with locations::slurpLocations(linesToProcess), playerLocation::linesToProcess[0]
get linesToProcess from File "examples/objects.rsc", list
state = state with objects::slurpObjects(linesToProcess)
post "\n" + describe(state[playerLocation], state) + "\n\n" to Output()
loop :
get userInput from Input "What now? "
strings.toLower(userInput) == "quit" :
break
else :
state = doTheThing(userInput, state)
post "\n" + state[output] + "\n" to Output()Well, the project's gotten way ahead of its documentation again, and I have a bunch of known bugs, but … I feel like I'm getting there with the design.
All comments welcome.
22
u/Athas Futhark May 08 '23
Can you elaborate on how this differs from Haskell's use of the IO monad? Your
getis very similar to thex <- readFile ...notation of Haskell'sdo-notation (which is sugar for the>>=operator). Haskell has nothing corresponding toput,postor such things - that's usually just done by IO actions that return a unit value. Haskell's IO model is not perfect, mostly in that it is not possible to limit exactly which kinds of IO are possible, but it is probably the most widely used system that delimits pure and impure code in a principled manner. (This is a low bar to clear, none of these systems are mainstream.)One question I could ask is what you are considering to do about concurrency. In your example above, it looks like
stateis an ordinary variable (i.e. mutable variables do not have a special type), since you are passing it to the (I guess?) pure functiondoTheThing. What happens if some other thread is mutatingstatewhiledoTheThingis executing? Most similar languages solve this by also making observing a mutable object an effect.7
u/phischu Effekt May 08 '23
I like it. Modern languages that distinguish between pure and impure programs like Flix, Koka, and Effekt do so on the type level instead of syntactically. This has three advantages:
post ("Hello, " ++ get from Input "What's your name?") to OutputHowever, I believe for your use-case and intended target audience your design is great.
2
u/redchomper Sophie Language May 08 '23
Well... I think you'll find the "imperative shell" part grows and grows. Sooner or later you'll want to group a sequence of steps into a named procedure. Before you know it, you'll be tempted to let a procedure return a value, and suddenly you're back to impure-land.
The other approach to "functional core" I've seen treats commands as data that a pure function can return. Consider this snippet from the rudimentary I/O subsystem I'm adding to Sophie right now:
The teletype adapter supplies code that knows how to interpret any of the
appsubtypes as a command. Part of the FFI supplies a linkage so that the driver can call back into Sophie.I should mention this is not fully implemented yet.
2
u/friedbrice May 08 '23
"Command" is the correct framing.
You don't even really need a wrapper language, you can kinda just choose to write a functional subset of Go. I demonstrate this for Python, Java, and Javascript here.
1
u/JanneJM May 08 '23
Is this meant to be an interactive shell? In that case I'd be concerned with the ergonomics. Typing a lot sucks, so there's a reason other shells make common patterns really abbreviated.
At the least I'd suggest making "get" and "post" be optional, and "<” and ">" be usable as shorthand for "from" and "to".
Keywords are fine for scripts but I'd go nuts having to type them out all the time in an interactive session.
-1
u/umlcat May 08 '23 edited May 08 '23
"How a functional programming based shell / console would look instead of an imperative programming based shell / terminal ?"
Ok, the prompt may start similar:
Usually, a console / terminal / shell, would receive an imperative / procedural instruction, with additional parameters or subinstructions, like this:
That's an imperative / procedural instruction.
In a Functional P.L., "everything is a list", may be:
In practical terms, not full lambda Calculus theory format, a functional instruction would have a variable, an assignment "->" operator, with an expression that may have parameters.
In an imperative console that would be either a command or assignment like this:
The same operation with a functional syntax may be:
The same previous list directory example, redirected to a file variable would be ( imperative) :
The same example to the console as a file variable may be ( imperative ):
The same example to the console as a file variable may be ( functional ):
If you need a full BASH alike shell script, these previous examples should be adapted as F.P.
I would not force the use of an assignment ( "->" ) to everything, only when required, and instead try a practical "LISP" alike, lists with parentheses syntax instead.
Just my two cryptocurrency coins contribution...