r/functionalprogramming • u/Odd-Opinion-1135 • Feb 12 '24
Question Can a language be functional without typing?
I'm trying to learn some category theory and I got thinking about this. I'm using Elixir a lot atm, and though I see functionalish things, like immutable state; the lack of a type system makes it non trivial to do other patterns that I think are more at the heart of functional programming.
Like how do you make a functor if you don't have a type system?
And I've seen some approaches in blogs but it really seemed to be making something fit unaturally and really not supported with any static analysis.
So can a language be functional without a type system or is it just functional -ish, borrowing patterns and ideas?
21
u/Mishkun Feb 12 '24
There is a certain myth that functional programming is impossible without static typing, ADT, monads and other complicated topics. I suggest you to read a book Grokking Simplicity by Eric Normand to debunk it for you.
21
u/lpil Feb 12 '24
FP is not category theory. FP is not Haskell.
Functional programming is just programming declaratively with first class functions while avoiding side effects. Things like the functor pattern just make that easier in some places.
20
u/[deleted] Feb 12 '24
Yes, untyped lambda calculus
19
u/SV-97 Feb 12 '24
Up front: I don't think you actually mean "without typing". Elixir has typing - even strong typing AFAIK. It's dynamic vs. static and the richness of the typesystem you seem to be concerned about.
That said: being able to express a functor in the typesystem is not what makes a functional language functional. It's really a rather particular thing in the grand scheme (no pun intended) of things in that you only really find that in the Haskell descendant languages (Haskell, Agda, Lean, Idris, ...) and maaayybe more generally other hindley-millner languages (though functors play a quite different role here).
Most (if not all) other functional languages don't have that: you mentioned elixir, some others are erlang, SML, OCaml etc., Lisp and scheme, miranda (pure, lazy, statically typed FP - but no explicit functors), I think purescript and elm...
You can still utilize the fact that something is a functor even without being able to express that it is one in the language: I can define
fmapon lists in C or Python and reason about it as a functor - yes sure it will suck and I don't get the strong guarantees I get from Haskell or whatever but it's perfectly possible. It's about the concepts themselves9
u/_lindig Feb 12 '24
Lisp and Scheme are dynamically typed functional languages. So, yes.
3
u/XDracam Feb 12 '24
Plain JS is more functional than object oriented or anything else. It relies heavily on passing and calling first class functions. But JS is not purely functional. But you can still write JS without side effects if you want to. It's just not a pleasant experience.
A codebase has one primary concern: to deal with complexity that changes over time. In dynamic, non-pure code, you can quickly "hack" and test to get changes done. Just mutate stuff everywhere until it works again. In purely functional code, you can't just hack your way around an addition. Data is not encapsulated like in OOP, and you can't just mutate state from anywhere. So your changes will have propagate through the layers of the onion (pattern).
This is incredibly easy with good static typing: the compiler will tell you what you still need to change. And because you don't need to reason about side effects, you can change all non-compiling code without looking at anything outside of that specific piece of code. Which is nice.
But if you don't have strong static typing, you have a problem: you need to change a lot of code, but no tools tell you which code to change. If you forget a spot, your code might crash unexpectedly at runtime. Extra bad if it's a rare but important edge case. And some would argue that crashing at runtime is not a pure thing (e.g. Elm has no runtime exceptions at all, besides unavoidable things like stack overflows)
2
u/Inconstant_Moo Feb 13 '24
How to write a functor in Elixir.
2
u/[deleted] Feb 14 '24
One does not need static type-checking to not use side-effects.
How do you make a functor? The same way as with types, you just keep the type function part of it in your mind instead of declaring it in the code.
0
u/[deleted] Feb 12 '24
You are my type
0
u/kevin_7777777777 Feb 13 '24
There are some languages you can use without a keyboard, but I don't think they have lambda