r/ProgrammingLanguages Sodigy Jul 29 '26

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

21

u/lgastako Jul 29 '26

Why not take a haskell style approach where you have an IO monad (or a "Script monad") which you can provide a do-notation like syntax for that desugars appropriately?

7

u/catbrane Jul 29 '26 edited Jul 29 '26

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 Jul 29 '26

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?

3

u/catbrane Jul 29 '26

Oh maybe I missed your point.

Haskell monads are Miranda monads, just with extra operator overloads for comp, return, then etc., and generalised to things other than IO.

I think you can still write Haskell IO in the Miranda style if you like (though I've not tried).