From 610ad9a4eb9ba0376a6763d05df2342f71814680 Mon Sep 17 00:00:00 2001 From: Robbie Gleichman Date: Fri, 6 Jan 2017 17:29:27 -0800 Subject: [PATCH] Add advantages.hs which shows some of the advantages of a visual syntax over text. --- examples/advantages.hs | 131 +++++++++++++++++++++++++++++++++++++++++ todo.md | 2 + 2 files changed, 133 insertions(+) create mode 100644 examples/advantages.hs diff --git a/examples/advantages.hs b/examples/advantages.hs new file mode 100644 index 0000000..08b09dd --- /dev/null +++ b/examples/advantages.hs @@ -0,0 +1,131 @@ +{-Advantages of Glance: + +The following are several admittedly contrived examples demonstrating some +of the advantages of Glance over textual code. + + +1. Display out of order arguments: + +The function "syntaxGraphToTuple" is a simple function that converts the +product type "SyntaxGraph" to a tuple. + +syntaxGraphToTuple (SyntaxGraph a b c d e) = (a, b, c, d, e) +-} +syntaxGraphToTuple (SyntaxGraph a b c d e) = (a, b, c, d, e) + +{- +Below is a version of syntaxGraphToTuple where some of the items in the tuple have +been swapped. + +badSyntaxGraphToTuple (SyntaxGraph a b c d e) = (e, b, c, d, a) +-} +badSyntaxGraphToTuple (SyntaxGraph a b c d e) = (e, b, c, d, a) +{- In the Glance image of badSyntaxGraphToTuple, it is immediately apparent that +some of the tuple arguments are out of order. + + +2. Reduce obfuscation: + +Glance conveniently eliminates long and unnecessary binding chains. For example, +Glance simplifies the following obfuscated function + +y x = g where + c = p + u = 2 * x + v = c + p = u + g = v + +to just +y x = 2 * x +-} + +y x = g where + c = p + u = 2 * x + v = c + p = u + g = v + +{- +3. Make recursion visible: + +Consider the recursive definition of the Fibonacci sequence + +fibs = cons 0 (cons 1 (zipWith (+) fibs (tail fibs))) + +In Glance, the recursive usages of fibs are explicitly shown with lines +looping from the result back into the nested icon. +-} + +fibs = cons 0 (cons 1 (zipWith (+) fibs (tail fibs))) + +{- +4. Simplified syntax: + +In Haskell, there are many ways to write code that means the same thing. + +For example, all of these lines of code mean exactly the same thing: +y = f (g x) +y = f $ g x +y = f $ g $ x +y = (f . g) x +y = f . g $ x + +In Glance, these are all rendered the same. +-} +y = (f . g) x + +{- +A few more examples: + +notZero 0 = False +notZero _ = True + +and + +notZero x = case x of + 0 -> False + _ -> True + +are both rendered as +-} +notZero x = case x of + 0 -> False + _ -> True +{- + +In Haskell, guards are not expressions, so having multiple levels of guards +is quite awkward. The textual function below, for example, +requires two superfluous variables, temp1 and temp2. + +In Glance, this sort of code is no problem. + +foo x y + | x > 0 = let + temp1 + | y > 0 = 'a' + | otherwise = 'b' + in temp1 + | otherwise = let + temp2 + | y > 0 = 'c' + | otherwise = 'd' + in temp2 + +-} + +foo x y + | x > 0 = let + temp1 + | y > 0 = 'a' + | otherwise = 'b' + in temp1 + | otherwise = let + temp2 + | y > 0 = 'c' + | otherwise = 'd' + in temp2 + +{-In future version of Glance, nested guards/cases could be rendered using a 2D grid. +-} diff --git a/todo.md b/todo.md index feb6a97..ac5f8b2 100644 --- a/todo.md +++ b/todo.md @@ -11,6 +11,8 @@ * Fix the arrowheads being too big for SyntaxGraph drawings. ### Visual todos +* Use pattern colors for pattern literal text boxes. + * Draw bounding boxes for lambdas (use dashed lines) * Use different line styles (e.g. dashed, solid, wavy) in addition to colors