News for hackers. Stuff that matters.
Re:Idris: A Language for Type-Driven Development by bsaul on Saturday January 21, 2023 @08:22AM (#34466258)
good example. i thought i used type-driven design, but your example made me realize i'm clearly not.

In your example, how would you qualify using an enum "State" with the case "shuffled" and "ordered" ? I feel like this would help me check all cases in places where it's needed, sometimes with the help of the compiler. But does that qualify as "type-driven" ?

In particular, do languages like idriss require encoding all possible states of a state machine as a unique type to leverage the full power of the checking mechanisms ? This would seem a bit unmanageable in practice..

Re:Idris: A Language for Type-Driven Development by henrydark on Saturday January 21, 2023 @03:10PM (#34469895)
I'm not an expert, I don't know how the community would treat such enums. I think the bottom line is if you can't fail to shuffle the cards, whether through an interesting enums and matching mechanism or through some other part of the type system, then you're good.

I've never used idris, but as far as I understand it encoding state machines in the type system is exactly the kind of thing type-dependent languages in general and idris in particular shine at. This is the prototypical example given, and it's the one Mathew Farwell himself gives in a podcast interview [1]

[1] software engineering radio, episode 296, http://feedproxy.google.com/~r/se-radio/~5/Kg1Py4rd2i0/SE-Ra...

Re:Idris: A Language for Type-Driven Development by chrisworden on Monday January 23, 2023 @06:42AM (#34487713)
There is at least one language that does this as a form of its core mechanism of execution- Mercury Language, a declarative/logic programming language.

Its type system is fairly complex. To accomplish the goal you describe, functions must be declared as deterministic, non-deterministic, or multi-deterministic. Meaning, they always have a solution, sometimes have a solution, or multiple solutions for the same input.

Armed with that information, the compiler will throw an error if you haven't thought of all possibilities in your logic flow. The fact that the type system required you to be very descriptive in many different ways compounded the number of bugs that the compiler would catch before your program ever runs... I think someone said "if you can get your code past the compiler, it will work on the first try."

I wish Mercury were more popular, it's an awesome project!

Re:Idris: A Language for Type-Driven Development by satvikpendem on Monday January 23, 2023 @12:44PM (#34492209)
Another example, let's say you're parsing a subscriber name for an email list, and you have a few checks such as whether it's too long, has whitespace, orcontains forbidden characters:

    pub fn parse(s: String) -> SubscriberName {
        let is_empty_or_whitespace = s.trim().is_empty();

        let is_too_long = s.graphemes(true).count() > 256;

        let forbidden_characters = ['/', '(', ')', '"', '<', '>', '\\', '{', '}'];

        let contains_forbidden_characters = s
            .chars()
            .any(|g| forbidden_characters.contains(&g));

        if is_empty_or_whitespace || is_too_long || contains_forbidden_characters {
            panic!("{} is not a valid subscriber name.", s)
        } else {
            Self(s)
        }
    }
And let's say your code to add a new subscriber to the email list only accepts a SubscriberName, not a String:

    pub struct NewSubscriber {
        pub email: SubscriberEmail,
        pub name: SubscriberName,
    }
Because `parse` gives back a SubscriberName if and only if all of the parse checks pass, you simply cannot add a new subscriber without all checks passing; the function will simply fail otherwise.

This example comes from the excellent book Zero To Production In Rust, which has a chapter on type-driven development [0].

[0] https://zero2prod.com

Re:Idris: A Language for Type-Driven Development by still_grokking on Tuesday January 24, 2023 @03:05AM (#34500836)
"Funny" code.

I'm quite sure nobody would do something like that "in production"…

The problem starts with the signature of this function: There is—of course—no function which could take an arbitrary String and return a SubscriberName.

You need to return at least an Option, or better a Result.

But at this point the fun starts. You need to thread this wrapped value throughout your whole program.

Crashing your program (by a panic in the above function, or by a unwrap somewhere later) because someone sent you an invalid String is not an option in real life.

Re:Idris: A Language for Type-Driven Development by satvikpendem on Tuesday January 24, 2023 @03:11AM (#34500877)
That's a simplified example that I grabbed in the middle of the book before the author refactors it, of course you shouldn't `panic` if you don't need to.
Re:Idris: A Language for Type-Driven Development by still_grokking on Tuesday January 24, 2023 @03:17AM (#34500918)
I've pointed this out as I think the follow-up problem is more difficult actually.

It's easy to validate something "and just throw an exception".

The fun starts when you can't do that (or don't want to do it like that).