r/ProgrammingLanguages • u/baehyunsol 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...
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?
26
u/Inconstant_Moo 🧿 Pipefish Jul 29 '26
It's called functional core/imperative shell. I think I'm the only person who's done it as a language paradigm, but I'd welcome the company.
Making the imperative shell bash-like is neither usual nor mandatory, there are nicer ways to do imperative things. The imperative language and the functional one should be united as much as possible by their syntax and type system, divided only by their semantics.
Pipefish and the lambda calculus.
Functional core/imperative shell.
1
u/Erythrina_ Jul 29 '26
I've been tinkering with a procedural-functional language. The procedural code is, in effect, a wrapper around the functional code that can call functional code but not be called by it. The procedural code obviously handles all the side effects like file, database, and network access and it has mutable state. The functional code is strictly pure and built around functions, lists, and ADTs.
I'm sure a script language would work for the same purpose too. The key there would just be how the data passes across the boundary between the two.
1
u/pliron Jul 29 '26
I once worked on a language called scilla (it was a DLS for smart contracts) that did exactly this. There is an oopsla paper and the code is available on github
3
u/Rechenplaner Jul 29 '26
Every functional language has solutions for handling I/O.
Haskell has its beloved monads, Clean has "uniqueness," and others use things like algebraic effects; meanwhile, languages like OCaml simply allow imperative messiness everywhere…
Take your pick…
Developing a second language for imperative code strikes me as a bit of overkill.
Besides, the interface between purely functional programming and the outside world is the most exciting part.
2
u/catbrane Jul 29 '26
I've done something similar for one of my spare-time projects:
https://github.com/libvips/nip4
It's an image processing spreadsheet. The language you use to write formula and implement all the menus is (a bit like) interpreted Haskell, and you use it by typing formula into cells or clicking menu items. The "shell" (the spreadsheet interface) is imperative, the logic is pure functional.
There's an introduction to the interface here:
https://www.libvips.org/2025/03/20/introduction-to-nip4.html
And here's the code for a menu item that transforms an image to polar coordinates, for example:
https://github.com/libvips/nip4/blob/main/share/nip4/start/Filter.def#L690-L719
2
u/mamcx Jul 29 '26
Think more deeply: What actual difference is between the 2?
Look at your AST:
(probably like:)
IF you have this, you have a expression based language. What you need to make it "imperative"?
Then your syntax to make the illusion.
Why I pointing this so obvious? Because "imperative" IS NOT IMPURITY.
"impurity" and "mutation, side-effects, etc" are else that arise when you plug into a environment where you have not FULL control, then NOW, you need to account for it.
(if you were doing forth from scratch in assembler you see that)
You can add "pure functional" into a "impure stateful" world, is only matter of how BIG your fiction is, like:
A SUPER IMPURE FUNCTIONAL PURE PROGRAM
So, the ONLY question is:
Do you wanna to surface the reality of "impurity" or not? Because you can just make all IO and HTTP "pure" (example: https://witheve.com). (I mean, how much be can be pedantic here, but I see a lang like this and think that is the feel, ok?)
To recap:
You can do 100% "functional" language that totally can do IO and terminal things with zero imperative functionality.
Impurity/purity is not derived by functionality or imperatively but by environment
HOW you communicate both things is a big deal, but you can go any directions, others has done the same!
1
u/FuncSug_dev Jul 29 '26 edited Jul 29 '26
"Has anyone tried similar approach?": Yes, but I've only made the imperative language (your "script" language). My language FuncSug (playground with game examples) has to call functions from another language (a functional one, ideally). I think the imperative part is generally overlooked in particular if one targets programs that mainly aim to organize interactions (like some games or industrial programs).
1
u/esotologist Jul 29 '26
I'm working on a language that has functional context and structural context depending on the assignment operators
1
u/SwedishFindecanor Jul 31 '26
I have only brainstormed about a shell scripting language using a similar model, and the result was only a few design notes. The first idea was that the return value from a functional code embedded in a command line would be able to produce a command parameter (single or list).
The functional core would not be able to run commands or read or write files but it would be able to have structured data piped to it and from it. And that structured data would in turn be able to contain computed data values in the functional language itself.
The functional core was also supposed to be a standalone module embeddable into anything.
1
u/david-1-1 Aug 02 '26
Sounds like two different languages patched together. It'll work, but always clumsily, like a mixture of PHP and JavaScript.