
I often wish to be able to define local types and instances, so, for example: import Data.Set as Set ordNubBy :: (a -> a -> Ordering) -> [a] -> [a] ordNubBy cmp = go Set.empty where newtype T = T a instance Ord T where T x `compare` T y = cmp x y go _ [] = [] go ys (x:xs) = bool (x:) id (T x ∈ ys) $ go (Set.insert (T x) ys) xs The notion is that type and instance declarations would become legal in `let` or `where` bonds; they would effectively declare new types and instances in that scope for each use of that term; and such local types and instances could never leave the scope they were defined in, by enforcement, so consistency of instances would be preserved. I know some related work which proposes local instances [0] but none i know proposes also local types. I'm seeking some feedback here before i formalize further and potentially start making implements. Thoughts? Questions? Better ideas? Critical flaws? [0] http://okmij.org/ftp/Haskell/TypeClass.html#local