2016-05-23 09:19:06 +03:00
|
|
|
{-Welcome to Glance! My goal for this tutorial is teach you how to read
|
|
|
|
Glance drawings, but also to get you thinking about visual programming languages.
|
|
|
|
Why is Glance designed the way it is? What are other ways it could work?
|
|
|
|
How could it be extended? I feel that we are just at the very beginning of
|
2018-11-11 14:17:06 +03:00
|
|
|
visual programming languages, and that there is a huge universe of visual
|
2016-05-23 09:19:06 +03:00
|
|
|
programming language designs waiting to be discovered.
|
|
|
|
|
2017-01-06 04:52:14 +03:00
|
|
|
This tutorial assumes that the reader has some familiarity with the basics
|
|
|
|
of Haskell.
|
|
|
|
|
|
|
|
|
|
|
|
Function Application:
|
|
|
|
|
2016-05-23 09:19:06 +03:00
|
|
|
Let's start with y = f x.
|
2016-12-08 09:09:52 +03:00
|
|
|
The function f is applied to the argument x, and the result is bound to y.
|
2016-05-23 09:19:06 +03:00
|
|
|
-}
|
|
|
|
y = f x
|
|
|
|
|
|
|
|
{-More explicitly-}
|
|
|
|
result = function argument
|
|
|
|
|
|
|
|
{-For example, multiply 3 and 5 and bind the result to y.
|
|
|
|
y = (*) 3 5
|
|
|
|
-}
|
|
|
|
y = (*) 3 5
|
|
|
|
|
|
|
|
{-Infix function application is displayed the same.
|
|
|
|
y = 3 * 5
|
|
|
|
-}
|
|
|
|
y = 3 * 5
|
|
|
|
|
|
|
|
{-You might be thinking that so far it looks like Glance has just decorated
|
|
|
|
the program text with some boxes, lines, triangles and circles. Don't worry,
|
|
|
|
it will start looking much stranger from here on. Keep in mind that the design
|
2016-12-29 06:05:43 +03:00
|
|
|
of the icons does not have much meaning. In fact, as long as the
|
2016-05-23 09:19:06 +03:00
|
|
|
icons (the non-text parts) are distinguishable from each other, how they are drawn,
|
|
|
|
even if they are rotated, reflected, squashed or squished does not matter.
|
|
|
|
The meaning of Glance is entirely in how the icons are connected to each other.
|
|
|
|
Lines in Glance are largely equivalent to names (variable names, function names,
|
|
|
|
etc.) in textual Haskell. Thus if two things are connected with a line in Glance,
|
2016-12-08 23:21:32 +03:00
|
|
|
it means they have the same value. Line colors are randomized.
|
2016-05-23 09:19:06 +03:00
|
|
|
|
2016-12-08 09:09:52 +03:00
|
|
|
Let's make things a bit more interesting with a nested function application.
|
2016-05-23 09:19:06 +03:00
|
|
|
y = (*) ((+) 8 7) 2
|
|
|
|
-}
|
|
|
|
y = (*) ((+) 8 7) 2
|
|
|
|
|
2017-01-06 04:52:14 +03:00
|
|
|
{-
|
|
|
|
|
|
|
|
The Mystery Icon:
|
|
|
|
|
|
|
|
That was probably not too surprising. How about this? What does the green icon
|
|
|
|
represent?
|
|
|
|
-}
|
2016-05-23 09:19:06 +03:00
|
|
|
f x = 3 * x
|
|
|
|
|
2017-01-04 12:47:44 +03:00
|
|
|
{-Let's try to figure it out. First look at the f. It's green and in an orange
|
2016-05-23 09:19:06 +03:00
|
|
|
box which indicates that it is bound to the result. Now look at the
|
2016-12-08 09:09:52 +03:00
|
|
|
function application. It looks like 3 is being multiplied by something, but what?
|
2016-05-23 09:19:06 +03:00
|
|
|
There is a line connecting the second argument of * to the dot in the green icon,
|
2016-12-29 06:05:43 +03:00
|
|
|
so the second argument probably comes from the dot in the green icon.
|
2016-05-23 09:19:06 +03:00
|
|
|
The result of the function application is also connected to the blue square
|
|
|
|
in the green icon. So the green icon represents a relationship between the
|
|
|
|
overall result f, the second argument to *, and the result of the multiplication.
|
|
|
|
|
2016-12-29 06:05:43 +03:00
|
|
|
What construct in Haskell has three ingredients: a name, a variable,
|
2016-05-23 09:19:06 +03:00
|
|
|
and a result that derives from the variable? Here's a hint.
|
2016-12-08 09:09:52 +03:00
|
|
|
y has a value of 21.
|
2016-05-23 09:19:06 +03:00
|
|
|
-}
|
|
|
|
y = (\x -> 3 * x) 7
|
|
|
|
|
|
|
|
{-Now for the answer. As you might have suspected. The green icon defines a
|
|
|
|
function. The dot inside the icon is the formal parameter. The blue square
|
2017-01-06 04:52:14 +03:00
|
|
|
represents the value returned by function (i.e. what's on the right side
|
2016-05-23 09:19:06 +03:00
|
|
|
of the -> in a lambda expression). The green circle represents the function
|
|
|
|
that has been defined.
|
|
|
|
|
2018-11-11 14:17:06 +03:00
|
|
|
A dashed boundary is drawn around all the icons inside a function, including the
|
|
|
|
function definition icon itself. This makes it easier to tell what icons belong
|
|
|
|
to which function definition.
|
|
|
|
|
2016-05-23 09:19:06 +03:00
|
|
|
In this case, the formal parameter x is the dot inside the green lambda icon,
|
|
|
|
and the return value 3 * x is the red circle in the function application icon,
|
|
|
|
which is connected to the blue square. The function itself is bound to the name f.
|
|
|
|
|
|
|
|
f = (\x -> 3 * x)
|
|
|
|
-}
|
|
|
|
f = (\x -> 3 * x)
|
|
|
|
|
|
|
|
{-
|
|
|
|
Here, the new function is applied to the argument 7, and the result of the
|
|
|
|
function application is bound to y.
|
2016-12-08 09:09:52 +03:00
|
|
|
y = (\x -> 3 * x) 7
|
2016-05-23 09:19:06 +03:00
|
|
|
-}
|
|
|
|
y = (\x -> 3 * x) 7
|
|
|
|
|
|
|
|
{-A more complex example:
|
2016-12-29 06:05:43 +03:00
|
|
|
f x y = max (2 * y) (1 + x)
|
|
|
|
|
2016-05-23 09:19:06 +03:00
|
|
|
If you have some drawing implements handy, you might want to try drawing this
|
|
|
|
yourself before scrolling down. One place to start is to figure out how functions
|
|
|
|
with "multiple parameters" are defined (which is of course syntactic sugar since
|
|
|
|
Haskell functions only have one parameter).
|
|
|
|
Scroll down for the drawing.
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
Drawing below:
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
2016-12-29 06:05:43 +03:00
|
|
|
f x y = max (2 * y) (1 + x)
|
2016-05-23 09:19:06 +03:00
|
|
|
-}
|
2016-12-29 06:05:43 +03:00
|
|
|
f x y = max (2 * y) (1 + x)
|
2016-05-23 09:19:06 +03:00
|
|
|
|
2017-01-06 04:52:14 +03:00
|
|
|
{-
|
2018-11-11 14:17:06 +03:00
|
|
|
Here are two examples of nested functions:
|
2016-05-23 09:19:06 +03:00
|
|
|
|
|
|
|
f1 = (\x1 -> (\x2 -> (\x3 -> sum [x1, x2, x3])))
|
|
|
|
-}
|
|
|
|
f1 = (\x1 -> (\x2 -> (\x3 -> sum [x1, x2, x3])))
|
|
|
|
|
2018-11-11 14:17:06 +03:00
|
|
|
{-
|
2016-05-23 09:19:06 +03:00
|
|
|
f1 x y = (\z -> x + z) y
|
|
|
|
-}
|
|
|
|
f1 x y = (\z -> x + z) y
|
2016-05-27 08:27:10 +03:00
|
|
|
|
2016-12-29 06:05:43 +03:00
|
|
|
{-
|
2017-01-06 04:52:14 +03:00
|
|
|
|
|
|
|
Graph and Tree Topology:
|
|
|
|
|
2016-12-29 06:05:43 +03:00
|
|
|
Let's go back to the function apply icon. If you are used to other graphical
|
2017-01-06 04:52:14 +03:00
|
|
|
programming languages, you may be thinking that the drawing below
|
2016-12-29 06:05:43 +03:00
|
|
|
does not look very graphical. Here graphical is referring to a graph topology,
|
2016-05-27 08:27:10 +03:00
|
|
|
and no it does not look graphical. The core idea is that nested function
|
|
|
|
application has a tree topology, not a graph topology. The result of each
|
2016-11-02 04:31:28 +03:00
|
|
|
sub-expression is only used once. Glance takes advantage of this tree topology
|
2016-05-27 08:27:10 +03:00
|
|
|
to make its drawings more compact.
|
2016-12-08 09:09:52 +03:00
|
|
|
|
2016-12-29 06:05:43 +03:00
|
|
|
y = foo (3 + (baz 2)) (8 * (baz 2))
|
2016-05-27 08:27:10 +03:00
|
|
|
-}
|
2016-12-29 06:05:43 +03:00
|
|
|
y = foo (3 + (baz 2)) (8 * (baz 2))
|
2016-05-27 08:27:10 +03:00
|
|
|
|
|
|
|
{-As soon as an expression is used more than once, the tree topology is lost,
|
2016-12-08 09:09:52 +03:00
|
|
|
and Glance extracts the sub-expression into a separate (non-nested) icon.
|
|
|
|
|
2018-11-11 14:17:06 +03:00
|
|
|
y = foo (3 + bazOf2) (8 * bazOf2) where bazOf2 = baz 2
|
2016-05-27 08:27:10 +03:00
|
|
|
-}
|
2018-11-11 14:17:06 +03:00
|
|
|
y = foo (3 + bazOf2) (8 * bazOf2) where bazOf2 = baz 2
|
2016-05-27 08:27:10 +03:00
|
|
|
|
|
|
|
{-There are many different ways that function application trees can be represented.
|
|
|
|
The linear layout Glance currently uses is just the simplest. Large expressions
|
|
|
|
(just like long lines of code) become hard to read with the linear layout.
|
2016-12-09 11:35:19 +03:00
|
|
|
Other tree layouts could make these large expressions much more readable.
|
2016-05-27 08:27:10 +03:00
|
|
|
|
2018-11-11 14:17:06 +03:00
|
|
|
y = (((2 + 4 * 4) - (7 + 2 + baz) * 8) / 21)
|
2016-05-27 08:27:10 +03:00
|
|
|
-}
|
2018-11-11 14:17:06 +03:00
|
|
|
y = (((2 + 4 * 4) - (7 + 2 + baz) * 8) / 21)
|
2016-05-27 08:27:10 +03:00
|
|
|
|
2017-01-06 04:52:14 +03:00
|
|
|
{-
|
|
|
|
Patterns:
|
|
|
|
|
|
|
|
Continuing on, here is a simple pattern match.
|
2016-05-27 08:27:10 +03:00
|
|
|
(Just x) = Just 3
|
|
|
|
-}
|
|
|
|
(Just x) = Just 3
|
|
|
|
|
|
|
|
{-
|
2017-01-06 04:52:14 +03:00
|
|
|
Since data constructors are functions, the match icon has a topology similar to
|
2016-12-29 06:05:43 +03:00
|
|
|
the apply icon.
|
2016-05-27 08:27:10 +03:00
|
|
|
|
2016-12-08 09:09:52 +03:00
|
|
|
Now that you are familiar with matches, here's a simple case expression.
|
2017-01-04 12:47:44 +03:00
|
|
|
y = case maybeInt of
|
|
|
|
Nothing -> 0
|
|
|
|
Just x -> x + 1
|
2016-05-27 08:27:10 +03:00
|
|
|
-}
|
|
|
|
y = case maybeInt of
|
|
|
|
Nothing -> 0
|
2016-12-29 06:05:43 +03:00
|
|
|
Just x -> x + 1
|
2016-05-27 08:27:10 +03:00
|
|
|
|
2016-12-08 09:09:52 +03:00
|
|
|
{-The case icon is the magenta icon with three yellow circles next to it.
|
2017-01-06 04:52:14 +03:00
|
|
|
The patterns connect to the triangles, and the result for each match is indicated
|
|
|
|
with the yellow circle.
|
2016-12-29 06:05:43 +03:00
|
|
|
|
|
|
|
The result for the first match (Nothing -> 0)
|
2016-05-27 08:27:10 +03:00
|
|
|
connects to the circle on the case icon since the right hand side (0) does not
|
2016-12-29 06:05:43 +03:00
|
|
|
use any value from the pattern (Nothing). For the second match, since the result
|
|
|
|
(x + 1) is connected to its pattern (Just x), Glance can connect
|
2017-01-06 04:52:14 +03:00
|
|
|
the result of (x + 1) to a new yellow circle.
|
2016-12-29 06:05:43 +03:00
|
|
|
|
2017-01-06 04:52:14 +03:00
|
|
|
|
|
|
|
Guards:
|
2016-05-27 08:27:10 +03:00
|
|
|
|
|
|
|
Guards and if expressions look like this:
|
2017-01-04 12:47:44 +03:00
|
|
|
y | x == 0 = 1
|
|
|
|
| otherwise = x + 1
|
2016-05-27 08:27:10 +03:00
|
|
|
-}
|
|
|
|
y | x == 0 = 1
|
|
|
|
| otherwise = x + 1
|
|
|
|
|
2016-12-08 09:09:52 +03:00
|
|
|
{-The Boolean expressions (e.g. x == 0) connect to the orange Ls, and the
|
2016-12-29 06:05:43 +03:00
|
|
|
corresponding result expressions (e.g. x + 1) connect to the triangles on the
|
2016-12-08 09:09:52 +03:00
|
|
|
other side of the mid-line. The overall result that the guard is bound to connects
|
2016-12-29 06:05:43 +03:00
|
|
|
to the bottom of the mid-line.
|
2016-05-27 08:27:10 +03:00
|
|
|
|
|
|
|
Currently, the guard icon and the case icon look similar since they have similar
|
2016-12-29 06:05:43 +03:00
|
|
|
topology, but they should look less similar in better icon versions.
|
2016-05-27 08:27:10 +03:00
|
|
|
|
2016-12-08 09:09:52 +03:00
|
|
|
"If" expressions are rendered the same as a guard with only one Boolean.
|
2016-05-29 05:43:10 +03:00
|
|
|
|
2017-01-04 12:47:44 +03:00
|
|
|
factorial x =
|
|
|
|
if x == 0
|
|
|
|
then 1
|
|
|
|
else factorial (x - 1) * x
|
2016-05-27 08:27:10 +03:00
|
|
|
-}
|
|
|
|
factorial x =
|
|
|
|
if x == 0
|
|
|
|
then 1
|
2016-12-09 11:56:40 +03:00
|
|
|
else factorial (x - 1) * x
|
2016-12-10 00:35:51 +03:00
|
|
|
|
2017-01-06 04:52:14 +03:00
|
|
|
{-
|
|
|
|
Compose (bonus section):
|
2016-12-10 00:35:51 +03:00
|
|
|
|
|
|
|
The depth of an icon's application tree is called the nesting depth.
|
|
|
|
For example, The icon representing "factorial (x - 1) * x" above has a nesting
|
2017-01-06 04:52:14 +03:00
|
|
|
depth of 3, since it is an apply icon (depth=1), inside an apply icon (depth=2),
|
|
|
|
inside an apply icon (depth=3).
|
2016-12-10 00:35:51 +03:00
|
|
|
|
|
|
|
To reduce nesting depth, Glance has an icon that represents an argument applied
|
|
|
|
to a composition of functions.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
y = f (g x)
|
2016-12-29 06:05:43 +03:00
|
|
|
which is the same as
|
|
|
|
y = (f . g) x
|
2016-12-10 00:35:51 +03:00
|
|
|
-}
|
|
|
|
y = f (g x)
|
|
|
|
|
2016-12-29 06:05:43 +03:00
|
|
|
{-
|
|
|
|
With a composition of three functions:
|
|
|
|
y = f (g (h x))
|
|
|
|
which is the same as
|
|
|
|
y = (f . g . h) x
|
|
|
|
-}
|
|
|
|
y = f (g (h x))
|
|
|
|
|
2016-12-10 00:35:51 +03:00
|
|
|
{-
|
|
|
|
Glance figures out automatically when to use the compose icon in order to
|
|
|
|
reduce the nesting depth.
|
|
|
|
|
|
|
|
For example, if we slightly rewrite the factorial function above, Glance
|
|
|
|
uses a compose icon for the else expression.
|
|
|
|
|
|
|
|
To enable the compose icon, we change the expression
|
|
|
|
factorial (x - 1) * x
|
|
|
|
to
|
|
|
|
x * factorial (x - 1)
|
|
|
|
|
2016-12-29 06:05:43 +03:00
|
|
|
Glance essentially rewrites the second expression as:
|
|
|
|
(x *) . factorial . (x -) $ 1
|
|
|
|
|
|
|
|
Notice that the nesting depth has been reduced from 3 to 2.
|
2016-12-10 00:35:51 +03:00
|
|
|
|
2017-01-04 12:47:44 +03:00
|
|
|
factorial x =
|
|
|
|
if x == 0
|
|
|
|
then 1
|
|
|
|
else x * factorial (x - 1)
|
2016-12-10 00:35:51 +03:00
|
|
|
-}
|
|
|
|
factorial x =
|
|
|
|
if x == 0
|
|
|
|
then 1
|
|
|
|
else x * factorial (x - 1)
|