Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What if function application was a typeclass?

Suppose Haskell's function application (the "space" operator) were in a typeclass instead of baked into the language. I imagine it would look something like

class Apply f where
    ($) :: f a r -> a -> r

instance Apply (->) where
   ($) = builtinFnApply#

And f a would desugar to f $ a. The idea is that this would let you define other types that act like functions, ie

instance Apply LinearMap where
    ($) = matrixVectorMult

and so on.

Does this make type inference undecidable? My instinct says that it does, but my understanding of type inference stops at plain Hindley-Milner. As a follow up, if it is undecidable, can it be made decidable by outlawing certain pathological instances?

like image 961
Dan Avatar asked Feb 09 '16 08:02

Dan


1 Answers

If you can envision this as a syntactic sugar on top of Haskell (replacing the "space operator" with yours), I can't see why this should make type inference any worse than it already is.

I can however see that code might be more ambiguous with this change, e.g.

class C a where get :: a
instance C (Int -> Int) where get = id
instance C Linearmap where get = ...

test = get (5 :: Int) -- actually being (get $ (5 :: Int))

Above get could be picked from both instances, while such ambiguity does not arise in plain Haskell.

like image 138
chi Avatar answered Sep 28 '22 15:09

chi