Monday, January 4, 2016

About Subtext

I watch a video, which I highly recommend to everyone:

I decided to write some thoughts on three issues discussed in the video.

  1. Program logic as text versus table
  2. Fibonacci and readability/editability
  3. Melee example and maintainability
Let me emphasize that I really liked the video and I agree with most of the things in there. The visualized ability to restrict function argument was especially interesting.

Program logic as text versus table

I wrote the code in Haskell just to get some feeling out of it.
-- If I forget a case:
-- warning| Pattern match(es) are non-exhaustive...
-- Like the red column in the table.
foo1 :: Bool -> Bool -> Bool -> Int
foo1 a b c
   | a         = 1
   | b || c    = 2
-- | otherwise = 3

-- Original working solution.
foo2 :: Bool -> Bool -> Bool -> Int
foo2 a b c
 | a         = 1
 | b || c    = 2
 | otherwise = 3

-- The problem:
-- add b && c -> 3

-- First try. Carelessly slipped it to the end.
-- No warning for overlap, probably since the guard is a runtime check?
foo3 :: Bool -> Bool -> Bool -> Int
foo3 a b c
 | a         = 1
 | b || c    = 2
 | otherwise = 3
 | b && c    = 3

-- If written without guards:
-- warning| Pattern match(es) are overlapped...
-- Like the conflict in the table.
foo4 :: Bool -> Bool -> Bool -> Int
foo4 True _    _    = 1
foo4 _    True _    = 2
foo4 _    _    True = 2
foo4 _    _    _    = 3
foo4 _    True True = 3

-- So, gotta think what we actually wanted to do.
-- The right place is probably as the first rule:

foo5 :: Bool -> Bool -> Bool -> Int
foo5 _    True True = 3
foo5 True _    _    = 1
foo5 _    True _    = 2
foo5 _    _    True = 2
foo5 _    _    _    = 3

-- So, no need for a table... but there's some code duplication.
-- Could do with guards, but since dynamic, the compiler could not
-- refactor the expression like the semantic table did in the video.
-- I guess the compiler could refactor the code to use guards, but it
-- would lose the knowledge of the patterns.

-- If Haskell had syntax like the following, I guess it could work.
-- Something like this has been proposed:
-- The compiler could warn about incomplete/overlapping patterns.
-- The compiler would "know" about the logic and thus could
-- refactor the expressions.

foo6 :: Bool -> Bool -> Bool -> Int
foo6 _    True True = 3
foo6 True _    _    = 1
foo6 _    True _
   | _    _    True = 2
foo6 _    _    _    = 3

So, I don't think it's about "semantic tables" but just a language feature.

Fibonacci and readability/editability

-- IMHO this is more readable than a table.
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n | n >= 2 = fib (n-1) + fib (n-2)

-- The assertion for the natural number could even be lifted to
-- type level, even though it is for some reason stated in the
-- video that it's not a type but an assertion.
-- I don't know what the author means by that since
-- they are all "just assertions" in the end...

type Nat = Int -- well, something else here...
fib2 :: Nat -> Nat
fib2 0 = 0
fib2 1 = 1
fib2 n = fib2 (n-1) + fib2 (n-2)

The compiler can offer various suggestions on what is applicable and where.
Taken further, for example in Agda, the compiler can even output placeholders where the programmer inserts suitable values. Pretty much like to the table.

So, I don't think it's about "semantic tables" but just a language feature.

Melee example and maintainability

-- here's one possible implementation.

data Attack = Magic | Melee
data Surprise = Surprise | NoSurprise

power Magic = 5
power Melee = 4

effectiveness Surprise   attack = power attack * 3
effectiveness NoSurprise attack = power attack * 2

damage :: (Fractional a, Ord a) => Attack -> Surprise -> a -> a
damage attack@Magic surprise defense
  | eff >= defense = eff - defense
  | otherwise      = 0
  where eff = effectiveness surprise attack
damage attack@Melee surprise defense = (eff / defense) * 2
  where eff = effectiveness surprise attack

The concepts of power and effectiveness can be separated from damage, so the problem might not be as big as the video hinted.
But, as the video said, text is linear by nature.
It would be awesome to be able to interactively restrict function arguments to dim out expressions that are never executed.

Visualizing the execution flow is something I actually don't like. I'm a functional programmer and I try to think in space and not in time. I think declaratively, I usually don't care how the code executes.

So, I don't think it's about "semantic tables" but just a language feature.

To summarise

Since the GHC represents code as a syntax tree, it could surely
  1. print out a table representation of the code, right?
  2. provide an editable table to edit the expressions, right?
  3. visualize the code execution in that table, right?
So, again, I don't think it's about semantic tables, but just another tool to interact with the code.

I like a text editor, but I certainly agree that we should be editing a syntax tree instead of a plain-text-representation of it. 

How about creating a text editor, that only allows one to write expressions that form a valid syntax tree in the places where they are written?


  1. Wow amazing i saw the article with execution models you had posted. It was such informative. Really its a wonderful article. Thank you for sharing and please keep update like this type of article because i want to learn more relevant to this topic.

    SEO Company in Chennai

  2. Thanks for sharing article like this. The way you have stated everything above is quite awesome. Keep blogging like this. Thanks a lot.

    DotNet Training in Chennai Adyar

  3. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us.
    seo company in chennai

  4. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care.As always, we appreciate you confidence and trust in us.

    Email Marketing Chennai

  5. Thank you for your post. This was really an appreciating one. You done a good job. Keep on blogging like this unique information with us.
    Branding Services in Chennai

  6. Great... Excellent sharing.. This is very helpful for beginers. Read that provide me more enthusiastic. This helps me get a more knowledge about this topic. Thanks for this.
    SMO Services in Chennai

  7. Provides great information about the concept.It helps an individual to gain knowledge on new techniques.Keep on giving this type of information.
    Dot net Training in Chennai

  8. It's like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a little bit, but instead of that, this is fantastic blog. A great read. I will definitely be back.

    Corporate Training in Chennai

  9. this is really too useful and have more ideas from yours. keep sharing many techniques. eagerly waiting for your new blog and useful information. keep doing more.
    Java Training Institute in Chennai

  10. This blog is having a wonderful talk. The technology are discussed and provide a great knowledge toall. This helps to learn more details about technology. All this details are important for this technology. Thank you for this blog.
    Digital Marketing Company in Chennai

  11. very useful information provided in this blog. concepts were explained in a detailed manner. Keep giving these types of information.
    SEO training in Chennai

  12. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.
    Digital Marketing Company in India