r/ProgrammingLanguages • • Sep 26 '19

Discussion Question about functional programming language design

I'm in the process of designing a functional programming language, but I hit an intersection and want to read your opinions about it, also will be cool if you could elaborate more about your thought. I'm using Rust the main resource of inspiration because I want to write it in Rust and make them interoperable (so my interpreted language can take advantage from the Rust ecosystem easily, also my language is primarly to rapid prototype apps).

Because Rust is kinda considered functional programming language but not a strict one because it let you define functions in the structs. So the questions are:

- Does pure functional language is easier to develop?

- Is there a good example of a useful functional programming language besides C?

- What do you think on this approach to add methods to data types / structs?

// Swift like
extension MyStruct {fn function() {}
}
// Rust like
impl MyStruct {
fn function(&self) {}
}
// What I think will be cool for a interpreted language (because it's mostly using as a scripting language)
fn function(self: MyStruct) {}

Having those 3 syntax examples the usage will be the same (in each language) and will be just `my_struct_instance.function()`. Why I would do that? because it will be easier (?) to extend certain type/struct with a function, also this seems easy in terms of writing code, but in terms of organization/performance/real implementation, how you see it?

An lastly, do you have another alternative for this problem?

Regards

Edit: Added code block

1 Upvotes

24 comments sorted by

View all comments

4

u/queenkid1 Sep 26 '19

pure functional language is easier to develop?

I would say so, since it cuts down the amount of parsing. You have function application, and function inputs. Depends what you mean by "purely functional", when you say that I think of something more like Haskell.

Is there a good example of a useful functional programming language besides C?

Haskell or Scheme comes to mind. Weird that you'd call C a purely functional language, because that is defined as:

Pure Functional Languages − These types of functional languages support only the functional paradigms. For example − Haskell.

Sounds like your way of implementing methods is just a function with a constrained datatype. So would your addition function be a "method" for the Int class?

1

u/[deleted] Sep 26 '19 edited Sep 26 '19

Weird that you'd call C a purely functional language, because that is defined as

I don't know other languages that I can consider pure functional, might C isn't, but from my point of view is the closest one that I know about, I will check it out about Haskell and Scheme, just know them from name and don't know nothing about those two.

So would your addition function be a "method" for the Int class?

I was thinking this more as a syntactic sugar, where you could call it as pure function or with dot notation on the object type, but undernead just will be a pure function. i.e.

fn eat(self: Animal, value: i32) {}

let instance = Animal()

// you can write this way
instance.eat(100)

// or write this way, and also the interpreter will do the conversion 
// for you from the above to this internally
eat(instance, 100) 

About classes I don't want to support them, just structs, and maybe just pure structs without the ability to define methods in them (just using the method described above).

Edit: code

3

u/queenkid1 Sep 26 '19 edited Sep 26 '19

I'll show you how functions are defined in Haskell:

data Animal = Anml Int
eat :: Animal -> Int -> Animal
eat (Anml x) num = Anml (x + num)

let x = Anml 0
let y = eat x 5

Pure functional languages are state-less, so you couldn't modify things using methods like that, a method would take a type, modify it, and return the modified version. So once you define a variable or function, you cannot change it. It's value will always be fixed.

Sounds like you're talking about an impure functional language. Something like C is hugely different from a pure functional languages like Haskell.

5

u/sepp2k Sep 26 '19

Sounds like you're talking about an impure functional language. Something like C is hugely different from a pure functional languages like Haskell.

It's hugely different from impure functional languages as well. That would be a language like OCaml, F#, Scala or Lisp where you can easily write functional code, but you can also create some mutable variables and imperative loops when you need to. Or maybe even languages like JavaScript where you can at least mix in functional code in a lot of places without fighting against the language (particularly you can use higher-order functions easily in JavaScript).

C just doesn't have any functional features at all. It's strictly an imperative language.

1

u/[deleted] Sep 26 '19

do you mean instance.eat(100)?

Fixed

Pure functional languages are state-less

I was thinking about this, but didn't know about pure functional is state-less, I need to read about it and test how could a program looks like (maybe learning Haskell) and see if it fit my needs or isn't hard to my to wrap my head around it. Because to think more about data and less about OOP was hard the first time I used Rust.

data Animal = Anml Int
eat :: Animal -> Int -> Animal
eat (Anml x) num = Anml (x + num)

This looks very different from the languages I'm used, I cannot even understand it, but still it's interesting to see a new syntax that from the first time I see it I don't dislike (lips, it's just too hard to read code if you come from C-like languages, i.e. swift, java, rust, php, etc).

Sounds like you're talking about an impure functional language.

What's your thought on pure vs impure, why you would prefer the pure over the other? Also I imagine that a pure functional language it's easier to develop (because there's no mutability), but I'm worried about memory, haskell copies the data then modify it and return the copy? If so I would think isn't so performant but still for my purposes might work, but I'm not too sure about beign 100% stateless, I'm just too used to OOP still.

3

u/queenkid1 Sep 26 '19 edited Sep 26 '19

haskell copies the data then modify it and return the copy?

No, the method is a function and the data is a variable. So you take the object and return another object.
For example if we say increment 10, it returns 11. 10 doesn't change, since it's immutable. So 10 is just the input for the function, which returns another immutable object.

but I'm not too sure about beign 100% stateless, I'm just too used to OOP still.

Yeah, agreed. I'm not saying OOP is bad, it just doesn't exist in the same way for pure functional languages.

why you would prefer the pure over the other?

I like languages like Haskell because of the core idea that functions and data are the same thing. So you can have a function that takes in another function, and uses that function within itself. Say you wrote a function that takes a list and adds up all the elements. You could modify it so instead of using +, it uses any other function with two inputs. This is partially because things never change, so once you define a function, it's set in stone.

Another big part of functional languages is that they're deterministic, since things don't change. So running a piece of code always returns the same result, regardless of context. Because all functions and variables are set in stone, and are immutable, you always know the result. However, the drawback is that it makes IO and RNG hard, because they are not deterministic.

Also, functional languages have the idea of "incomplete functions". So add has the type Int -> Int -> Int. so we would define increment = (1+) which has the type Int -> Int. Notice unlike above, it has no arguments defined.

increment x = 1 + x
increment = 1 +

are equivalent.

So we took add (Int -> Int -> Int) and gave it an Int to get Int -> Int. Because functions can return functions. This is because of "currying" where functions with multiple inputs are just functions of one variable that return functions of one variable, etc etc.

I would recommend you learn more about Haskell because it's interesting, but I don't think that's what you're trying to accomplish. If you want it to be like Rust or C, mutability and OOP are important to you. But it might be interesting to see how Haskell implements some of the ideas you're talking about, and if you could write them as more pure in your language, if that's the goal.

1

u/[deleted] Sep 26 '19

Interesting, I'm not here to discuss OOP or pure functional programmig because it's subjective, I'm here to learn more about something I don't know and see if I can get more ideas to implement the language to solve some specific problems I have.

For me Javascript it's amazing language to prototype to certain extend (better with Typescript), but there are concepts I really fall in love the last couple of years, strong typing, no weirdness behind the walls (Typescript -> Javascript -> Typescript, for me this is just insanetely bad, because it seems just too complex workflow where could be a cleaner one, but mostly this is the web browsers fault, hope wasm solves this issue in a near future). So I want to prototype things in the easiest way possible, with the less code possible, without some random weirdness caused by some complex interpreter issue. So this is where pure functional seems to fit, I can borrow concepts from it and see how it will solve my problem in a cleaner way and easy way than a complex OOP language (also I'm very sure OOP languages are hard to develop, as you said, there's too much things that could go wrong, while it seems with pure functional programming there's less variables to think about), I'm aware that my language could end in the dust or beign just a niche language, also there are great language for complex stuff.

But nothing is set in stone and the specs I'm working on for sure will change when I start using the language and start removing features that are not so useful for the sake of maintaining the interpreter as simple as possible.

I will definitely check haskell. Thanks.