[Haskell-cafe] Local types

Yuras Shumovich 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
> 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

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
example see

https://ghc.haskell.org/trac/ghc/wiki/Proposal/OpenImportExtension 

https://ghc.haskell.org/trac/ghc/wiki/Records/NestedModules

https://ghc.haskell.org/trac/ghc/ticket/10478

http://blog.haskell-exists.com/yuras/posts/namespaces-modules-qualified-imports-and-a-constant-pain.html

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 [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
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe


More information about the Haskell-Cafe mailing list