Search code examples
haskellpurely-functional

How does pure functional language(haskell) users make Design?what alternative to uml they use?


giving that haskell doesn't have the notion objects(even it has the keyword class) How does Haskell developer design their code? how do they represent interaction/entities? does they have alternative to uml? or class equivalent?(or do they even care?)


Solution

  • I don't know of any formal(ized) notion of drawing or describing architecture.

    Nevertheless you can structure your application

    The most basic way is using the expressiveness of the type system:

    • wrapping simple types in newtypes - or using type synonyms to - distinguish between say an Int and an Age value, e.g. newtype Age = Age Int.
    • composing more complex types as products of existing e.g. data Person = Person { name :: String, age :: Age}
    • or by using sum types data Employee = Chef Person | Waiter Person

    All these data types - which can be made much more elaborate - can be accessed/modified* via record syntax or lenses. I tend to think of the data types as my skeleton of the application I write - and use the compiler to stay true to the ideas I had when starting and never subvert the types by using unsafeXX functions.

    Lack of OO/Encapsulation

    I have never had - having functions attached to objects is not necessary (utterly wrong in my opinion, but that is not up for discussion here).

    A basic form of encapsulation can be done via newtype and modules with their exports.

    Polymorphism

    A lot can already be done with typeclasses, which if you have not yet worked with them, are somewhat like interfaces, (but with less typing) - but there is a lot more you can accomplish with advanced things like type families.


    Conclusion

    So if I would like to draw a structure/architecture of my program, I would start with my data-types as boxes and use arrows as functions between them. And maybe use "special" boxes for container types.

    *: technically one usually doesn't modify stuff, but create new values from old values as (almost) everything is immutable.