Skip to content

Instantly share code, notes, and snippets.

@mwotton
Created August 8, 2016 15:09
Show Gist options
  • Save mwotton/3f32477102ebc3f104bae8a37493919d to your computer and use it in GitHub Desktop.
Save mwotton/3f32477102ebc3f104bae8a37493919d to your computer and use it in GitHub Desktop.
Alternatively (and equivalently), local implicit module bindings for Ord and Ord_int_rev make it possible
to override the behaviour at ints while using the automatic resolution behaviour to locate and use
the Ord_pair functor:
```
let sort_both_ways ( items : ( int * int ) list ) =
let ord =
let implicit module Ord = Ord_int in
sort items
in
let rev =
let implicit module Ord = Ord_int_rev in
sort items
in
ord , rev
```
In Haskell, which lacks both local instances and a way of explicitly instantiating type class dictionary
arguments, neither option is available, and programmers are advised to define library functions in pairs,
with one function (such as sort) that uses type classes to instantiate arguments automatically, and one
function (such as sortBy) that accepts a regular argument in place of a dictionary:
```
sort :: Ord a = > [ a ] -> [ a ]
sortBy :: ( a -> a -> Ordering ) -> [ a ] -> [ a ]
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment