Urgent.News

What's breaking now, across thousands of outlets.

Tech

Sticking Functions Where They Donʼt Belong

The concept of "pure data" refers to a type that can implement certain interfaces such as Show, NFData, Ord, and Read. These interfaces ensure that the data structure is fully serialized, evaluated, ordered, and comparable, respectively. The article discusses the debate around whether a shallow WHNF instance for functions should be allowed and proposes a potential solution to this problem.

The author argues that, semantically, NFData should normalize the closure, including all the data that helps the function implementation. However, the data that sticks around to help the function often cannot be normalized, as it is hidden and may not be fully evaluable. The Ord interface is used to order values, often with partial orders in mind. The article also mentions Eq and Read as additional interfaces that can be implemented by pure data.

The key takeaway is that a type implementing all these interfaces should have no hidden surprises, making it fully legible and straightforward. However, this also means that extensibility in data is more challenging. While embedding functions in data is not possible theoretically or practically, the author proposes a workaround using defunctionalization.

By smuggling functions into otherwise pure data, the author aims to maintain the purity of the code while still allowing for extensible behavior. This is achieved by using typeclass synonyms, dictionaries, and existential types that wrap any such data with its corresponding typeclass dictionary, including Typeable, Eq, Ord, and Semantics.

This approach allows for injecting arbitrary foreign semantics into code without spoiling deriving for Show, NFData, Eq, or Ord.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at blog.veritates.love →

More in Tech

More from Monday 14 September →