[Haskell-cafe] Local types
shumovichy at gmail.com
Mon Jan 25 13:37:19 UTC 2016
On Mon, 2016-01-25 at 00:11 -0800, M Farkas-Dyck wrote:
> I often wish to be able to define local types and instances, so, for
> 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
I like local types a lot.
I'm not sure how your example works w.r.t. type variable scopes. Are
you assuming ScopedTypeVariables? It will probably require explicit
quantification to work:
ordNubBy :: forall a. (a -> a -> Ordering) -> [a] -> [a]
Without ScopedTypeVariables `a` in `newtype T = T a` is different from
the one in top level type signature, is it? At least that is how type
variables work in local function signatures.
In general, I see increasing demand for change scoping in haskell. For
Probably all of that should be addressed in a complex.
> 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  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?
>  http://okmij.org/ftp/Haskell/TypeClass.html#local
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe