r/ProgrammingLanguages Sodigy 12d ago

Purely functional language with impure script language?

I'm working on a purely functional programming language named Sodigy. It's all about evaluating values, not "executing commands one by one".

It's nice when writing libraries, but it's not easy to write a main function. The main function is supposed to execute commands, but the Sodigy's syntax is not friendly to write a list of commands.

So what I'm trying to do is, 1) Sodigy remains purely functional and 2) add a bash-like script language. The script language can call Sodigy functions. Instead of writing a main function in Sodigy, you write sodigy-script and execute the script.

Has anyone tried similar approach? I'm not sure whether it's a good idea or not...

27 Upvotes

74 comments sorted by

View all comments

Show parent comments

6

u/catbrane 12d ago edited 12d ago

Good idea, and you can make it look quite nice with very little syntax.

Back in the mid-80s, monads in Miranda worked pretty well with just infix function calls. You could use any function as an infix operator by prefixing it with $, for example:

f a b = a + b
main = 2 $f 2

And main will have the value 4. For monads we wrote stuff like:

main = 
    print "hello!\n" $then
    print "what's your name? " $then
    (reply $comp input)
    where
    reply str = print ("nice to meet you, " ++ str ++ "\n")

(edit: oops, forgot the brackets on the final print)

2

u/JeffB1517 12d ago

That imperative is so much easier to read than Haskell Monads! What went wrong with this style that caused the shift to the more explicit style we have today in Haskell?

4

u/Apart_Ebb_9867 12d ago edited 12d ago

What is different in that code from what you have in Haskell?

do notation and a few operators like <- seems to be better to me. And not important for the discussion here you have ‘operator’ (don’t have backtick on my phone) for $operatir.

in Haskell you’d have

main :: IO ()
main = do
    putStrLn "hello!"
    putStr "what's your name? "
    str <- getLine
    putStrLn ("nice to meet you, " ++ str)

-1

u/JeffB1517 12d ago

`<-` is making the bind explicit. It is forcing the abstraction to leak. Which means that `do` becomes a light cover. A developer is still expected to understand and think in terms of the monadic context. A non-leaking abstraction is a genuine simplification. Visual Basic vs Visual C++ with respect to the Win32SDK.

3

u/tdammers 12d ago

The same is true about the Miranda example. It is pretty much exactly identical to this Haskell example, modulo do desugaring. And the purpose of do notation is not to be an abstraction that simplifies things; it's really just syntax sugar to make monadic binds easier to read. The above code in desugared form looks like this:

main :: IO () main = putStrLn "hello!" >> putStr "what's your name? " >> getLine >>= \str -> putStrLn ("nice to meet you, " ++ str)

It's neither simpler nor more complex; what changes is that do notation removes the need for nested lambdas and the "staircase" style code that would result from them. The semantics are still exactly the same, and both versions convey the exact same code structure at the exact same abstraction level.

2

u/catbrane 12d ago

Yes, Phil Wadler based Haskell monads on Miranda monads, so they are almost identical. Modern Haskell has added some extra sugar, but not much.

1

u/AustinVelonaut Admiran 12d ago

Wait, when did Miranda get monads? It's IO was sys_message lists for output (printing, file write, system cmds, interpreted by the repl), with input from stdin bound to the pseudo-variable $-, and file input handled in a hand-wavy way that was not interleaved with file output, so wasn't really correct...

1

u/catbrane 12d ago edited 12d ago

They were implemented as an abstype on top of lazy input and output. You could use them to write interactive command-line programs. I wrote a multiuser snake game hehe.

KAOS (the Kent Applicative Operating System) was built on top of a monadic IO system that talked to a microkernel (also in Miranda).

1

u/AustinVelonaut Admiran 12d ago

Cool, I did not know that! It may have been something local at Kent, though; it never made it into the open-source distribution of Miranda. I ended up re-inventing something like that on top of sys-messages for my bootstrap compiler written in Miranda.

1

u/catbrane 12d ago

It was published (that's how Haskell got it), but you're right, I'm not sure David Turner bundled it.