45

UML is a standard aimed at the modeling of software which will be written in OO languages, and goes hand in hand with Java. Still, could it possibly be used to model software meant to be written in the functional programming paradigm? Which diagrams would be rendered useful given the embedded visual elements?

Is there a modeling language aimed at functional programming, more specifically Haskell? What tools for putting together diagrams would you recommend?

Edited by OP Sept 02, 2009:

What I'm looking for is the most visual, lightest representation of what goes on in the code. Easy to follow diagrams, visual models not necessarily aimed at other programmers. I'll be developing a game in Haskell very soon but because this project is for my graduation conclusion work I need to introduce some sort of formalization of the proposed solution. I was wondering if there is an equivalent to the UML+Java standard, but for Haskell. Should I just stick to storyboards, written descriptions, non-formalized diagrams (some shallow flow-chart-like images), non-formalized use case descriptions?

Edited by jcolebrand June 21, 2012:

Note that the asker originally wanted a visual metphor, and now that we've had three years, we're looking for more/better tools. None of the original answers really addressed the concept of "visual metaphor design tool" so ... that's what the new bounty is looking to provide for.

Community
  • 1
  • 1
codnik
  • 461
  • 5
  • 5
  • the question is lacking a description of what is modeling. I know I can guess or google it, but your specific viewpoint is more relevant to your question. – yairchu Sep 02 '09 at 16:08
  • 2
    I start off by describing UML as a modeling language and then I mention diagrams and visual elements. That should give people a hint of what I'm looking for. I'll add a note to my question anyway. Thanks. – codnik Sep 02 '09 at 16:38
  • I think part of the issue is what exactly would you model? In java you use UML to demonstrate Objects and their interactions. You could probably break up something as complex as a game into "phases," "modules," or even some crude states and diagram each of those. Really, I think storyboarding and flowcharts would serve best here. – CodexArcanum Sep 23 '09 at 22:22
  • Is there a visual modeling tool / language or style which supports Scala profile or can say that it supports the object oriented programming paradigm and the functional programming paradigm, both. Follow-up question: For an Enterprise scale Scala project how do / with what modeling tool - the business analyst prepare Visual Models for Logical (conceptual) view and Development view? The logical view is concerned with the functionality that the system provides to end-users. The development view illustrates a system from a programmer's perspective and is concerned with software management. – Optimight Jun 22 '12 at 03:16
  • 2
    Here are some Function Model **(Visual)** samples. http://en.wikipedia.org/wiki/Function_model – Optimight Jun 22 '12 at 07:13

11 Answers11

46

See archived version with outlinks here.

As of May 26, 2023, there is no standard modeling language for pure functional programming, but there are alternatives:

1. UML

UML has traditionally been associated with object-oriented programming (OOP), mostly because of its historical roots1, 2, but one can certainly attempt to use it for different programming paradigms (or for something other than modeling software, for that matter, such as use-case diagrams, ontology engineering, manufacture and other systems & processes, etc.).

With that said, there don't seem to be many examples around for this use case. A noteworthy few:

The answers above mention that there are UML diagram types other than class diagrams, whose use would benefit any program, independent of paradigm. See 1 and the Modeling applications of UML using various diagrams section of the Applications of UML wikipedia page, along with a diagram:

Hierarchy of diagrams in UML 2.2 source

[1]: UML Diagrams: History, Types, Characteristics, Versions, Tools
[2]: https://stackoverflow.com/a/27861475/1498178

1.1 Statecharts

From the Statecharts: a visual formalism for complex systems paper:

[statecharts] extend conventional state-transition diagrams with essentially three elements, dealing, respectively, with the notions of hierarchy, concurrency and communication. These transform the language of state diagrams into a highly structured and economical description language.

If the program doesn't deal with state, then this is not very useful but it seems to be a noteworthy tool otherwise.

More info and discussions:

2. Diagrams

2.1 Wiring diagram

From Seven Sketches in Compositionality: An Invitation to Applied Category Theory, section 2.2.2 Introducing wiring diagrams:

Wiring diagrams are visual representations for building new relationships from old.

Examples:

  • enter image description here
  • enter image description here

2.2 String diagram

Seven Sketches in Compositionality: An Invitation to Applied Category Theory mentions string diagrams, but they seem to be different from wiring diagrams. From the paper Category Theory Using String Diagrams:

String diagrams are a graphical formalism for working in category theory, providing a convenient tool for describing morphisms within bicategories [B ́enabou, 1967], some background on string diagrams can be found in [Street, 1995].

A visual proof as a string diagram

2.3 Data flow diagrams

This section expands on this answer.

2.3.1 "Henderson diagram"

"These show how individual functions are "plugged together"". From section 3.5.2 Infinite Streams of SICP:

Figure 3.31: The prime sieve viewed as a signal-processing system.

2.3.2 Diagrams used in the Arrows documentation

enter image description here

class Arrow a where
  arr :: (b -> c) -> a b c
-- Each function may be treated as
 a computation.

enter image description here

  (>>>) :: a b c -> a c d -> a b d
-- Computations may be composed, by
-- connecting the output of the first
-- to the input of the second.

enter image description here

  first :: a b c -> a (b,d) (c,d)
-- A computation may be applied to part
-- of the input, with the rest copied
-- through to the output.

enter image description here

2.4 Hasse diagram (wikipedia)

"Category theory is all about organizing and layering structures"3 so Hasse diagrams may be useful. A UML class diagram is "a form of Hasse diagram", so it can be used in a more general sense to convey dependency relations.

[3] Seven Sketches in Compositionality: An Invitation to Applied Category Theory, section "1.1.2 Ordering systems"

2.5 Commutative diagram (wikipedia)

These can be used to describe how functions and structures interact with each other; however, they rather define invariants/laws than behaviour.

Examples:

3. Formal methods

3.1 Universal Systems Language (USL) (proprietary?)

Universal systems language (USL) is a systems modeling language and formal method for the specification and design of software and other complex systems. It was designed by Margaret Hamilton based on her experiences writing flight software for the Apollo program. The language is implemented through the 001 Tool Suite software by Hamilton Technologies, Inc.

See articles on Hamilton Technologies, Inc.'s homepage.

3.2 TLA+

TLA+ is a formal specification language developed by Leslie Lamport. It is used for designing, modeling, documentation, and verification of programs, especially concurrent systems and distributed systems.

It felt prudent to mention TLA+ here as well.

4. Tools

4.1 ghc-vis

Similar to Python Tutor,

ghc-vis is a tool to visualize live Haskell data structures in GHCi. Evaluation is not forced and you can interact with the visualized data structures. This allows seeing Haskell’s lazy evaluation and sharing in action

Visualization of the execution of a Haskell program

4.2 Visual Haskell

This thesis presents and justifies a framework for programming real-time signal processing systems. The framework extends the existing "block-diagram" programming model; it has three components: a very high-level textual language, a visual language, and the dataflow process network model of computation.

From section 4.2 An introduction to Visual Haskell:

The factorial function in Visual Haskell

4.3 Viskell (tool; last commit: 2017)

Viskell is an experimental visual programming environment for a typed (Haskell-like) functional programming language. This project is exploring the possibilities and challenges of interactive visual programming in combination with the strengths and weaknesses of functional languages.

Screenshot of Viskell's user interface

4.4 graphmod Haskell package

Present the module dependencies of a program as a "dot" graph.

4.5 HOPS: The Higher Object Programming System

HOPS is a graphically interactive program development and program transformation system based on term graphs.

Seems unmaintained, but looked interesting.

enter image description here See more images / animations here.


Other interesting resources:

toraritte
  • 6,300
  • 3
  • 46
  • 67
John Millikin
  • 197,344
  • 39
  • 212
  • 226
  • 1
    For example, would game theory be useful in modelling the game you said you were developing? I don't know, I'm a grad student in computational linguistics, and my statistical and linguistic models do a pretty good job at capturing the high-level view of my Haskell (and C++!) code. – Nathan Shively-Sanders Sep 02 '09 at 01:44
  • 11
    @Nathan Sanders: Game theory doesn't really have much to do with computer games. It has to do with many other things in the world. – yairchu Sep 02 '09 at 16:10
  • 7
    I bet half of game developers don't even know what Game Theory is, much less use it. – Catharsis Feb 05 '10 at 17:22
  • 1
    @NathanShively-Sanders game theory teaches you how players will play complex strategy games (since game theory is about playing games). You don't need it except in so far you are concerned about game play. – PyRulez Apr 16 '16 at 19:52
33

Yes, there are widely used modeling/specification languages/techniques for Haskell. They're not visual.

In Haskell, types give a partial specification. Sometimes, this specification fully determines the meaning/outcome while leaving various implementation choices. Going beyond Haskell to languages with dependent types, as in Agda & Coq (among others), types are much more often useful as a complete specification.

Where types aren't enough, add formal specifications, which often take a simple functional form. (Hence, I believe, the answers that the modeling language of choice for Haskell is Haskell itself or "math".) In such a form, you give a functional definition that is optimized for clarity and simplicity and not all for efficiency. The definition might even involve uncomputable operations such as function equality over infinite domains. Then, step by step, you transform the specification into the form of an efficiently computable functional program. Every step preserves the semantics (denotation), and so the final form ("implementation") is guaranteed to be semantically equivalent to the original form ("specification"). You'll see this process referred to by various names, including "program transformation", "program derivation", and "program calculation".

The steps in a typical derivation are mostly applications of "equational reasoning", augmented with a few applications of mathematical induction (and co-induction). Being able to perform such simple and useful reasoning was a primary motivation for functional programming languages in the first place, and they owe their validity to the "denotative" nature of "genuinely functional programming". (The terms "denotative" and "genuinely functional" are from Peter Landin's seminal paper The Next 700 Programming languages.) Thus the rallying cry for pure functional programming used to be "good for equational reasoning", though I don't hear that description nearly as often these days. In Haskell, denotative corresponds to types other than IO and types that rely on IO (such as STM). While the denotative/non-IO types are good for correct equational reasoning, the IO/non-denotative types are designed to be bad for incorrect equational reasoning.

A specific version of derivation-from-specification that I use as often as possible in my Haskell work is what I call "semantic type class morphisms" (TCMs). The idea there is to give a semantics/interpretation for a data type, and then use the TCM principle to determine (often uniquely) the meaning of most or all of the type's functionality via type class instances. For instance, I say that the meaning of an Image type is as a function from 2D space. The TCM principle then tells me the meaning of the Monoid, Functor, Applicative, Monad, Contrafunctor, and Comonad instances, as corresponding to those instances for functions. That's a lot of useful functionality on images with very succinct and compelling specifications! (The specification is the semantic function plus a list of standard type classes for which the semantic TCM principle must hold.) And yet I have tremendous freedom of how to represent images, and the semantic TCM principle eliminates abstraction leaks. If you're curious to see some examples of this principle in action, check out the paper Denotational design with type class morphisms.

toraritte
  • 6,300
  • 3
  • 46
  • 67
Conal
  • 18,517
  • 2
  • 37
  • 40
  • It might just be me, but it seems to me the summary of TCM you've given here is the clearest I've seen the core notion presented yet. – sclv Jun 22 '12 at 02:27
  • Sir, Can you please give your feedback for the requirement described in this comment: http://stackoverflow.com/questions/1364237/is-there-a-visual-modeling-language-or-style-for-the-functional-programming-para/1366134#comment14618790_1364237 – Optimight Jun 22 '12 at 05:48
  • Awarding the bounty to you, because it's the highest-voted new answer. – Pekka Jun 27 '12 at 19:45
21

We use theorem provers to do formal modelling (with verification), such as Isabelle or Coq. Sometimes we use domain specific languages (e.g. Cryptol) to do the high level design, before deriving the "low level" Haskell implementation.

Often we just use Haskell as the modelling language, and derive the actual implementation via rewriting.

QuickCheck properties also play a part in the design document, along with type and module decompositions.

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
  • Tools like [graphmod](http://hackage.haskell.org/package/graphmod) can be used to generate documentation artifacts. Visual modelling/prototyping is just not something I see done though. – Don Stewart Jun 21 '12 at 18:53
12

Yes, Haskell.

I get the impression that programmers using functional languages don't feel the need to simplify their language of choice away when thinking about their design, which is one (rather glib) way of viewing what UML does for you.

Ori Pessach
  • 6,777
  • 6
  • 36
  • 51
  • 3
    You seem to miss the point of formal modeling. In certain circles of software development, formal modeling is a requirement, no matter how bureaucratic you consider this to be. There are several levels of involvement from different teams on the conclusion of a project and many of the people involved have no idea about the code. They don't need to have, it'd be a major hassle if that was asked of them. The formal modeling is there to assure that they'll get a better view of how things work so that they can perform their roles in the development. – codnik Sep 01 '09 at 19:53
  • 1
    Oh, but don't think I totally disagree with you (although I believe your answer adds nothing). I particularly think it's a bore, but I need to have some sort of formal modeling in my graduation conclusion work (using Haskell to develop a game) or a very good excuse for the lack of it. – codnik Sep 01 '09 at 19:54
  • I agree that modeling disciplines try to do everything you describe. I just haven't seen any evidence that they achieve any of that in the last 20 years. I actually like the answer that lists "math" as a good modeling language for functional languages. You can't get more formal than that. – Ori Pessach Sep 01 '09 at 20:01
  • Oh, but don't think I totally disagree with YOU - I would just point out that finding something a bore is a good indication that that something isn't particularly useful. People have an innate ability to recognize useful things around them - just look at any toddler holding a stick for a universal example. There's nothing about UML that suggests it would be of any use to man or beast, and I think your reaction to it is a reflection of that. – Ori Pessach Sep 01 '09 at 20:05
  • 2
    In that case, just delete the implementations of all the functions, and leave only the signatures. To an intelligent functional programmer, that leaves the implementation reasonably apparent, but looks like a simplified 'model' to someone less knowledgeable. Bonus points for arranging these in some visualization. Also, http://portal.acm.org/citation.cfm?id=99370.99404 describes how to generate some code based just on the types in the 'model' :-). – Phil Miller Sep 02 '09 at 06:56
  • You got a good point there Ori. I just don't go acknowledging that all the time because people might get the impression that I'm a bureaucracy hater just for the sake of it. I'm not against all rules, but I like to be practical. Novelocrat, I understand what you mean, I've come across similar comments about this issue. What I'm looking for is the most visual, lightest representation of what goes on in the code. I guess I should just go with a colorful storyboard and forget about the cold diagrams? – codnik Sep 02 '09 at 14:28
  • codnik: Never underestimate the importance of external factors when assessing requirements. In your situation, choosing and using a modeling language is entirely appropriate. In day to day practice of the craft, my impression so far has been that it's often counterproductive and that the people who insist on it are less effective than people who are happy to do without it. My evidence is all anecdotal, of course. – Ori Pessach Sep 02 '09 at 16:45
7

I have watched a few video interviews, and read some interviews, with the likes of erik meijer and simon peyton-jones. It seems as when it comes to modelling and understanding ones problem domain, they use type signatures, especially function signatures.

Sequence diagrams (UML) could be related to the composition of functions. A static class diagram (UML) could be related to type signatures.

fsl
  • 831
  • 1
  • 11
  • 24
5

Although not a recommendation to use (as it appears to be not available for download), but the HOPS system visualizes term graphs, which are often a convenient representation of functional programs.

HOPS screenshot

It may be also considered a design tool as it supports documenting the programs as well as constructing them; I believe it can also step through the rewriting of the terms if you want it to so you can see them unfold.

Unfortunately, I believe it is no longer actively developed though.

ScottWest
  • 1,936
  • 1
  • 13
  • 18
4

In Haskell, you model by types.

Just begin with writing your function-, class- and data signatures without any implementation and try to make the types fit. Next step is QuickCheck.

E.g. to model a sort:

class Ord a where
    compare :: a -> a -> Ordering

sort :: Ord a => [a] -> [a]
sort = undefined

then tests

prop_preservesLength l = (length l) == (length $ sort l)
...

and finally the implementation ...

Dario
  • 48,658
  • 8
  • 97
  • 130
  • 1
    Yes, we specify & test. Tests, however, can only demonstrate incorrectness. We also prove when we want to demonstrate correctness. – Conal Jun 22 '12 at 03:32
3

I realize I'm late to the party, but I'll still give my answer in case someone would find it useful.

I think I'd go for systemic methodologies of the likes of SADT/IDEF0.

Such diagrams can be made with the Dia program that is available on both Linux, Windows and MacOS.

Guillaume Ponce
  • 2,503
  • 2
  • 12
  • 9
3

You can a data flow process network model as described in Realtime Signal Processing: Dataflow, Visual, and Functional Programming by Hideki John Reekie

For example for code like (Haskell):

fact n | n == 0    = 1
       | otherwise = n * fact (n - 1)

The visual representation would be:

enter image description here

Luxspes
  • 6,268
  • 2
  • 28
  • 31
1

I use USL - Universal Systems Language. I'm learning Erlang and I think it's a perfect fit.

Too bad the documentation is very limited and nobody uses it. More information here.

GxFlint
  • 294
  • 3
  • 15
  • It seems to be undocumented because the only implementation I could find is the website owner's own proprietary tool... – toraritte May 26 '23 at 13:39
1

What would be the point in modelling Haskell with Maths? I thought the whole point of Haskell was that it related so closely to Maths that Mathematicians could pick it up and run with it. Why would you translate a language into itself?

When working with another functional programming language (f#) I used diagrams on a white board describing the large blocks and then modelled the system in an OO way using UML, using classes. There was a slight miss match in the building blocks in f# (split the classes up into data structures and functions that acted upon them). But for the understanding from a business perspective it worked a treat. I would add mind that the problem was business/Web oriented and don't know how well the technique would work for something a bit more financial. I think I would probably capture the functions as objects without state and they should fit in nicely.

It all depends on the domain your working in.

WeNeedAnswers
  • 4,620
  • 2
  • 32
  • 47
  • "What would be the point in modelling Haskell with Maths?" Because not all math is computable. "Why would you translate a language into itself?" Because not all denotationally equivalent functional programs have equivalent complexity. So we specify in elegant but inefficient (even uncomputable) form and derive (or otherwise prove equivalent) a denotationally equivalent but more efficient form. More detail in my answer. – Conal Jun 22 '12 at 03:30