
What the most beautiful piece of code you ever written? - sam217
https://blog.hellojs.org/code-show-off-526eb6ed649#.6um2e3yyy
======
tazjin
Oh, I have one. It's in a Haskell project that never actually went anywhere,
but that's not important for this snippet.

The snippet is here:
[https://github.com/tazjin/herbert/blob/master/test/Herbert/S...](https://github.com/tazjin/herbert/blob/master/test/Herbert/Storage/Test.hs#L28)
(the function called `acidTest`). I use that regularly as a way to explain
complex constraints that can be expressed in "proper" type systems.

A small dissection for those who are unfamiliar with Haskell syntax:

acid-state[1] is a library to turn arbitrary, serialisable Haskell data
structures into a sort of in-memory database that is persisted to disk by
writing a log of the transactions applied to it. I wanted a generic way to
concisely write tests against transactions on the data-store.

    
    
      acidTest :: forall state event query result.
             (IsAcidic state, UpdateEvent event, MethodState event ~ state,
              QueryEvent query, MethodState query ~ state,
              MethodResult query ~ result)
             => String           -- ^ Test description
             -> state            -- ^ Initial state for test
             -> event            -- ^ Event to test
             -> query            -- ^ Query to check
             -> (result -> Bool) -- ^ Function to test query result
    

There are three distinct sections in here:

    
    
      forall state event query result.
    

This existentially quantifies `state`, `event`, `query` and `result` as type
variables. At this point they could be any type. This is followed by type
class constraints that define relationships between these types:

    
    
      IsAcidic state
    

The `state` type is a type for which acid-state has been implemented.

    
    
      UpdateEvent event,
      MethodState event ~ state,
    

The `event` type is an update that can be applied to an acid-state. The
`state` type is equivalent to the state type on which this update operates.

    
    
      QueryEvent query,
      MethodState query ~ state
    

Same thing, but with a `query` which is a query operating on this specific
`state` type.

    
    
      MethodResult query ~ result
    

The `result` type is equivalent to the type returned by this query.

At this point we've defined all necessary relationships and can proceed with
the actual function signature:

    
    
             => String           -- ^ Test description
             -> state            -- ^ Initial state for test
             -> event            -- ^ Event to test
             -> query            -- ^ Query to check
             -> (result -> Bool) -- ^ Function to test query result
    

These are already documented :-)

Essentially this ends up being a function that can take an initial test state,
any update that is applicable to this test state, any query that can be run
against this test state and then a predicate that checks the query result.
Together with the test description this is turned into an assertion.

The actual implementation is trivial, the important bits here are all in the
types!

Now back to work ...

[1]: [https://hackage.haskell.org/package/acid-
state](https://hackage.haskell.org/package/acid-state)

~~~
sam217
Very cool

