# Modelling a DAG, Visual Programming Interface

**URL:** <https://discourse.elm-lang.org/t/modelling-a-dag-visual-programming-interface/3593>\
**Category:** Learn\
**Created:** [May 2, 2019, 8:42pm UTC](https://discourse.elm-lang.org/t/modelling-a-dag-visual-programming-interface/3593 "2019-05-02T20:42:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nonphoto](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/nonphoto/32/2104_2.png) [@nonphoto](https://discourse.elm-lang.org/u/nonphoto)\
**Post date:** [May 2, 2019, 8:42pm UTC](https://discourse.elm-lang.org/t/modelling-a-dag-visual-programming-interface/3593/1 "2019-05-02T20:42:25Z")

</div>

I thought it would be interesting to build a visual programming interface such as [Grasshopper](https://www.grasshopper3d.com/) or [Max](https://cycling74.com/products/max). The key components of such an interface:

- The user creates a program by creating nodes and connecting them together, creating a _directed graph_.
- There are various types of node that each have their own behavior, and a set number of inputs, and outputs.
- Inputs and outputs are strongly typed; you can’t connect a `String` output to an `Int` input for example.
- Any number of instances of a node can be created.
- The created graph must be acyclic; you might call it a _Directed Acyclic Graph_.

How would you model such an interface in Elm? I want to use the type system to create this as much as possible. Nodes, inputs, and outputs seem to lend themselves well to union types, but it’s unclear how I can model the state of each node in a way that makes it easy to query all of the nodes connected to it, without having double references where adjacent nodes have to “agree” that they are connected to each other.

---

<div class="post-metadata">

**Author:** ![antew](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/antew/32/2102_2.png) [@antew](https://discourse.elm-lang.org/u/antew)\
**Post date:** [May 3, 2019, 12:31am UTC](https://discourse.elm-lang.org/t/modelling-a-dag-visual-programming-interface/3593/2 "2019-05-03T00:31:28Z")

</div>

For the visual part at least I would check out [https://github.com/erkal/kite](https://github.com/erkal/kite) (demo: [https://erkal.github.io/kite/](https://erkal.github.io/kite/)), it’s beautiful and all Elm.

From the data structure side there is a DOT files ([https://package.elm-lang.org/packages/brandly/elm-dot-lang/latest/DotLang](https://package.elm-lang.org/packages/brandly/elm-dot-lang/latest/DotLang)), and [https://github.com/elm-community/graph](https://github.com/elm-community/graph) that may be good for inspiration on structuring them.

---

<div class="post-metadata">

**Author:** ![nonphoto](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/nonphoto/32/2104_2.png) [@nonphoto](https://discourse.elm-lang.org/u/nonphoto)\
**Post date:** [May 6, 2019, 2:25am UTC](https://discourse.elm-lang.org/t/modelling-a-dag-visual-programming-interface/3593/3 "2019-05-06T02:25:59Z")

</div>

These are great resources, thank you! I’m not that worried about the view implementation yet, but I’m sure looking at Kite’s source will help later.

After thinking about this for a while, I decided to write my own `Graph` with `elm-community/graph` as a reference. Something like this:

```auto
type alias Graph = Dict NodeId Node

type alias Port = { id : NodeId, index : Int }

type Edge
  = Float Port Port
  | Texture Port Port
  | ...

type alias Node n =
  { id : NodeId
  , nodeType : NodeType
  , incoming : Array (Maybe Edge)
  , outgoing : Array (Maybe Edge)
  }

```

where `NodeType` is an enum of all the different possible “types” of nodes.  
While I think this would work fine, I was hoping to use the type system to enforce that you can’t connect nodes via incompatible edges or create multiple instances of the same node “type” that are different. For instance, with this system I could potentially make two instances of a node with `nodeType = BlurTexture`, where one has inputs `[Float, Texture]` and the other has `[Texture, Texture]`. This shouldn’t be possible because only one set of input types is valid.

I also thought about defining nodes like this:

```auto
type Node
  = NodeImageIn NodeId () EdgeTexture
  | NodeImageOut NodeId EdgeTexture ()
  | NodeBlur NodeId (EdgeTexture, EdgeFloat) EdgeTexture

```

But I’m not even sure how I would begin to implement an `addNode` function for something like this.

---

<div class="post-metadata">

**Author:** ![Jess\_Bromley](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jess_bromley/32/3396_2.png) [@Jess\_Bromley](https://discourse.elm-lang.org/u/Jess_Bromley)\
**Post date:** [May 6, 2019, 12:28pm UTC](https://discourse.elm-lang.org/t/modelling-a-dag-visual-programming-interface/3593/4 "2019-05-06T12:28:19Z")

</div>

Since I’m assuming that you never have the situation of one node having more than one parent, you are actually only looking at a subset of DAGs, namely trees. You might also get some inspiration from looking at the way [the-sett/tea-tree](https://package.elm-lang.org/packages/the-sett/tea-tree/latest/) was written… Although that was possibly designed for much larger structures than you had in mind.

---

<div class="post-metadata">

**Author:** ![nonphoto](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/nonphoto/32/2104_2.png) [@nonphoto](https://discourse.elm-lang.org/u/nonphoto)\
**Post date:** [May 6, 2019, 3:41pm UTC](https://discourse.elm-lang.org/t/modelling-a-dag-visual-programming-interface/3593/5 "2019-05-06T15:41:48Z")

</div>

Actually nodes can have any number of parents and any number of children, so I can’t model this structure as a tree. For example you might have a `NodeAdd` that has two `Float` inputs and one `Float` output, but also a `NodeRGBSplit` that has one `Texture` input and three `Texture` outputs. However, the `tea-tree` that you linked looks interesting—maybe I can approach the API in a similar way.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [May 16, 2019, 3:41pm UTC](https://discourse.elm-lang.org/t/modelling-a-dag-visual-programming-interface/3593/6 "2019-05-16T15:41:53Z")

</div>

Closed on May 16, 2019, 3:41pm UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
