<div dir="ltr">Hi all,<br><br>I've been playing around with what might be described as type-directed <br>functions. One example is a list-like structure of phantom-typed values<br><br>{-# LANGUAGE TypeOperators #-}<br>{-# LANGUAGE DataKinds #-}<br>{-# LANGUAGE PolyKinds #-}<br><br>import GHC.TypeLits<br><br>infixr 6 :::<br>data a ::: b = a ::: b<br>             deriving (Show, Eq)<br><br>data Tag b = Tag String<br>           deriving (Show, Eq)<br><br>ex1 :: Tag 5 ::: Tag 3 ::: Tag 7<br>ex1 = Tag "Alice" ::: Tag "Bob" ::: Tag "Carol"<br><br><br>And then sorting 'ex1' based on the Nats, such that <br><br>sort ex1 :: Tag 3 ::: Tag 5 ::: Tag 7<br>sort ex1 = Tag "Bob" ::: Tag "Alice" ::: Tag "Carol"<br><br>Notice how it's the types, not the values, that determine the result, but <br>that the value-level also changes.<br><br>I know how to do this using classes, but it's a little excruciating - it's<br>like programming in a verbose and very restricted Prolog. With type families<br>it's much easier to get the result *type* (pattern matching is simple,<br>recursive calls are natural, and it all looks a lot more like Haskell), but <br>I haven't yet seen a way of effectively using type families to direct <br>the value-level component of the calculation. <br><br>Are there any examples of how this might be done? Or are there other <br>alternatives to using type-classes that I am missing? Or, alternatively, are<br>there libraries to reduce the boilerplate of this type-class code?<br><br>Thanks,<br>  Julian<br><br></div>