More specifically, how do you model a functional program, or one developed using the Functional Style (without classes) using a diagram, and not textual representation.
-
2A related question, "Is there a modeling language for the functional programming paradigm?": http://stackoverflow.com/questions/1364237/is-there-a-modeling-language-for-the-functional-programming-paradigm (the highest-voted answer at the moment is "I believe the modeling language for Haskell is called 'math'") – Chuck Mar 17 '10 at 23:54
-
Here's a good question and answer: https://softwareengineering.stackexchange.com/questions/302811/what-are-functional-programmers-using-in-place-of-uml – Fuhrmanator Sep 19 '17 at 13:24
9 Answers
Functional programmers generally don't have a lot of use for diagrams. Many functional programmers (but not all) find that writing down types is a good way to encapsulate the design relationships that OO programmers put into UML diagrams.
Because mutable state is rare in functional programs, there are no mutable "objects", so it is not usually useful or necessary to diagram relationships among them. And while one function might call another, this property is usually not important to the overall design of a system but only to the implementation of the function doing the calling.
If I were feeling a strong need to diagram a functional program I might use a concept map in which types or functions play the role of concepts.

- 198,648
- 61
- 360
- 533
-
Thanks Norman, a bit of a recursive question, but I think that you have answered what I was feeling most eloquently. – WeNeedAnswers Mar 17 '10 at 12:44
-
1revising this, I think that immutable types can be modelled. You can then pass these immutable objects into functions that act upon them, producing a new output. This question I posted was for a project I was working on. In the end I used UML, but split the objects up into functions and immutable types. It worked a treat. – WeNeedAnswers Oct 12 '10 at 03:04
-
4I'm curious if this answer is still true today. How is complexity managed? E.g., UML has package/class/method levels of abstractions that allow managing complexity (hiding details at different abstraction levels). The diagrams allow visualizing those levels. My experience so far with FP is too trivial to see how this is necessary, but any complex system would be hard to grok without some kind of diagram. – Fuhrmanator Jan 26 '15 at 22:34
-
Lots of functional diagramming going on here: http://felsin9.de/nnis/ghc-vis/ – Fuhrmanator Sep 19 '17 at 13:17
UML isn't only class diagrams.
Most of the other diagram types (Use case diagrams, activity diagrams, sequence diagrams...) are perfectly applicable for a purely functional programming style. Even class diagrams could still be useful, if you simply don't use attributes and associations and interpret "class" as "collection of related functions".

- 342,105
- 78
- 482
- 720
-
1I'm starting to see the original author's point, though. If you're just modeling a collection of related functions, you're not really 'modeling,' so much as organizing. I don't disagree that it's still _useful_, but it's not so much modeling behavior. – Marc Bollinger Mar 16 '10 at 21:24
-
1@Marc: class diagrams aren't about modeling behaviour anyway; that's what activity and sequence diagrams are for. – Michael Borgwardt Mar 17 '10 at 00:28
-
UML is all about Classes, why else create such a paper based language?, I done a lot of other methods before and used methodologies that wrap up the Procedural Type Languages (SSADM to name one). Not yet seen one that does the same for functional languages. UML is all about messages and collaborations with descrete units of work called classes. But since a Function can be a Class, how would one model something without state? UML I feel is one hammer too many for something which is a pin. – WeNeedAnswers Mar 17 '10 at 12:41
-
3@WeNeedAnsers: UML is not "paper based", nor is it "all about classes". Many of the concepts of UML have nothing to do with classes. Modeling something without state is as simple as refraining from using the model elements that constitute state. It looks to me like in this question you were looking mainly for validation of your misinformed prejudices. – Michael Borgwardt Mar 17 '10 at 12:59
-
@Michael. Not prejudices at all, I have worked with UML for a while now, before that I think it was called OMT or something. The "paper based language" is a language of expressing a system using visual and textual tools. Yes I agree that it does encompass more than just modelling and that most of it is about elicitation. But there have been many many methods and methodologies over the years, the selling point of UML was all about modelling of a system using Classes. – WeNeedAnswers Mar 20 '10 at 01:21
-
1@Michael: An object without state does not have a behavioural pattern and is very difficult to model using Object notation. Please show me an example in UML, and I mean the heart and soul diagrams where a function can be modelled without an artificially created Object wrapping it up. Question is, how do you model a Function in UML without these "shoe horn fitting methods"? Prejudices I have not, I like UML. Just looking for a way to model functions :) – WeNeedAnswers Mar 20 '10 at 01:28
-
I came to this discussion a year too late but I agree with you WeNeedAnswers. If your 'objects' are without state, then your functions are static transitions. They alter the global state, and the global space is the negative space of what is modeled in any of the UML diagrams. – David Apr 08 '11 at 12:32
-
We used UML also for procedural code (C...). Each .h and .c "module" was modeled as a class with static functions. Since we did embedded and did not use "global variables", the functions were re-entrant (somewhat similar to "pure", only that they can modify sometimes what they get passed as arguments). Then we could make our customers happy and even draw sequence diagrams etc. if they asked for it. But even in this case, usually I wrote the header file, then imported it into the UML tool - which is faster (less work) usually compared to the reverse. Same could work for functional languages. – BitTickler Sep 12 '20 at 22:09
Functional programmers have their own version of UML, it is called Category Theory.
(There is a certain truth to this, but it is meant to be read with a touch of humour).

- 1,785
- 1
- 13
- 13
-
6I know you did it with a bit of humor, but for the uninitiated, modeling is not just about... modeling. It's about managing complexity (specially large amounts of it) as well as managing potentially large code bases to comprehensible levels. I'm sure FP'programmers do their own large scale modeling in various ways; it will just take years for it to become formalized and standardized (for the purpose of complexity management, not formal correctness.) – luis.espinal Mar 07 '11 at 20:54
-
1
UML is a compendium of different types of modeling. If you are talking about the Object Diagram (Class Diagram), well you are not going to find anything that fits your desired use. But if you are talking about an Interaction Diagram (Activity Diagram) or Requirements Diagram (Use Case Diagram), of course they will help you and are part of the UML base.

- 1,200
- 6
- 16
-
3A "requirements diagram" doesn't exist in UML (it's from SysML) – Michael Borgwardt Mar 16 '10 at 20:41
-
1Nope, UML is Objects all the way. Everything is an Object, interaction diagrams model Objects, unless you treat a function as an object? I tried that a couple of years ago with a c# delegate which is a real object, it didn't work. – WeNeedAnswers Mar 16 '10 at 20:48
-
I kindly suggest some reading. http://www.springerlink.com/content/wwvwbt5wbm35cq1r/ should fit your interests. – jpabluz Mar 16 '10 at 20:55
-
To model a functional program, using a diagram, and not textual representation, you can use notation like the one used to program in Viskell or Luna

- 6,268
- 2
- 28
- 31
-
1Isn’t this exactly the kind of representation that you can do in UML with activity diagrams (making use of object flows to show how activities/functions feed each other)? – Christophe May 09 '21 at 10:18
I realize this is an old thread but I'm not understanding the issue here.
A class is merely an abstraction of a concept that ties the functionality of it's methods together in a more human friendly way. For instance, the class WaveGenerator might include the methods Sine, Sawtooth and SquareWave. All three methods are clearly related to the class Generator. However, all three are also stateless. If designed correctly, they don't need to store state information outside of the method. This makes them stateless objects which - if I understand correctly - makes them immutable functions which are a core concept in the functional paradigm.
From a conceptual perspective I don't see any difference between
let Envelope Sine = ...
and
let Envelope Generator.Sine = ...
other than the fact that the latter might provide greater insight into the purpose of the function.

- 31
- 1
-
3Objects are all about encapsulating state. In Functional Programming, as you correctly ascertain state is not an issue as everything should be immutable. It is far better in functional programming to separate the state from the function or even better, reduce the state to a function. Its this difference in core philosphy that is the problem. In OOP you program to hide state and localise the changes to mutable objects, whilst in FOP your reducing the state issue to immutable structures. – WeNeedAnswers Nov 10 '10 at 11:08
-
1A good example would be a person object, With the usual state of Name, Age, Address etc. In FOP this type would be immutable, to change a value, you would pass this into a function, where it would produce a new person object. In OOP you would use property setters/getters and the state is mutable. – WeNeedAnswers Nov 10 '10 at 11:13
-
1Possibly I wasn't clear in making my point. The original thread asked about modeling the functional programming style. While it's true that OOP is designed around an imperative style of programming, it is not restricted to imperative programming. A class that restricts itself to static methods is equivalent to a set of encapsulated functions. Just as C is not an OOP language, it's not restricted to a procedural style. – Charley Savage Nov 10 '10 at 21:08
-
1A class modeled using the functional paradigm is still an OOP program but with added constraints on its design. And, as such, it can be modeled in UML using the same diagrams and procedures used in OOP minus state diagrams of course. Class, activity, and sequence diagrams. A case might even be made for using component, deployment, and even collaboration diagrams tailored to a functional style. – Charley Savage Nov 10 '10 at 21:21
-
2@WeNeedAnswers - uh, no. Objects, and more precisely classes they belong to are for encapsulating behavior and/or state (and interfaces to represent types or "roles"). Without behavior, a class is just a struct. Without state (or more precisely, without a changeable state), a class is just an immutable loci of behavior, a collection of one of more function types. So, **no**. They are not **all** about encapsulating state, not unless you model them for that purpose. – luis.espinal Mar 07 '11 at 20:51
-
Luis' last answer makes me chuckle. I can't find a dedicated OO programmer who isn't obsessed with his getters and setters, and nonchalant about the rest. – David Apr 08 '11 at 12:36
-
@CharleySavage I see your point and I agree to some extent. But the point is that UML provides encapsulation for state and behavior at the same time so it is well-suited for OOD. but in functional languages we usually have functions and types. we can model functions by UML but not types, and this makes UML unfit for FP design. – milad zahedi Jan 21 '17 at 16:02
UML is an object approach because at graphical level you can not define functional modeling. A trick is to directly add constraints and notes at model and not in the diagram levels. I mean that you can write a full functional documentation on each model element directly in the metamodel and only display an object view using the UML editor. This is maybe stupid but I found this demo in French language exactly on the same subject and using EclipseUML Omondo : OCL and UML 2.2 (demo in French language 3mn): http://www.download-omondo.com/regle_ocl.swf
This demo explains how to add constraints directly on methods at metamodel level. The interesting point of this demo is that using a single model for the entire project allows to be flexible enough to extend traditional UML and avoid SysML, BPMN, DSL additionals models because all information is built on the top of the UML 2.2 metamodel. I don't know if it will be a success but this initiative is very interesting because reduce modeling complexity and open new frontiers !!

- 1,452
- 1
- 9
- 7
-
Hey all answers welcomed with open arms, the only stupid questions are the ones not asked. :) Thank you. I looked at constraints, think they are great, but if your feeling purest say using a language such as Haskell, it gets very awkward to apply something that has no state. The constraints become guards, but guards are so much more than constraints. – WeNeedAnswers Mar 17 '10 at 12:48
-
I fail to see why the following (practical) aspects would not apply to functional programming: 1. Division of labor for teams 2. Design for test 3. Design for reuse. Call it a class with only static member functions or a module or however. Beyond the level of functions, higher level organizational levels are needed for practical reasons. System components in non-monolithic applications (e.g. Client, sever) also require statements of what shall be built to be made and communicated to teams and stake holders. UML offers facilities to express such structure, independent of language. – BitTickler Jan 16 '16 at 01:41
I haven't actually tried modelling a large system in UML and then going for a functional implementation, but I don't see why it shouldn't work.
Assuming the implementation was going to be Haskell, I would start by defining the types and their relationships using a class diagram. Allocate functions to classes by their main argument, but bear in mind that this is just an artefact of UML. If it was easier to create a fictional singleton object just to hold all the functions, that would be fine too. If the application needs state then I would have no problem with modelling that in a state chart or sequence diagram. If I needed a custom monad for application-specific sequencing semantics then that might become a stereotype; the goal would be to describe what the application does in domain terms.
The main point is that UML could be used to model a program for functional implementation. You have to keep in mind a mapping to the implementation (and it wouldn't hurt to document it), and the fit is far from exact. But it could be done, and it might even add value.

- 17,438
- 3
- 42
- 59
I guess you could create a class called noclass
and put in functions as methods. Also, you might want to split noclass
into multiple categories of functions.

- 9
- 2