Skip to content

Instantly share code, notes, and snippets.

@pchiusano
Last active August 29, 2015 14:03
Show Gist options
  • Save pchiusano/f9e78cd778cb6d9506fb to your computer and use it in GitHub Desktop.
Save pchiusano/f9e78cd778cb6d9506fb to your computer and use it in GitHub Desktop.
Using tagged dictionaries to ensure coherency in Haskell
-- Demonstrates how to ensure coherence when representing typeclasses with first-class dictionaries
newtype Tag t a = Tag { untag :: a } -- constructor kept private
use :: a -> (forall t . Tag t a -> b) -> b -- tagged values introduced in a stack discipline
-- Example, a 'first-class' Ord
newtype Ord a = Ord { ordering :: a -> a -> Ordering }
-- Ensure coherence when using this with Set
empty :: Tag t (Set a)
fromList :: [a] -> Tag t (Order a) -> Tag t (Set a)
-- Common tag ensures both sets built with same `Order`, which is introduced in `use`
union :: Tag t (Set a) -> Tag t (Set a) -> Tag t (Set a)
-- Left as an exercise to make this general idea not horrible to use :)
@pchiusano
Copy link
Author

By the way, I don't endorse actually doing this in Haskell. I just wanted to point out that it is in fact possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment