Assume, you have a class:
class AClass a where
func:: Int
instance AClass SomeTree where
func = 0
instance AClass Double where
func = 1
How do I call the function func?
{-# LANGUAGE AllowAmbiguousTypes, TypeApplications #-}
class AClass a where
func :: Int
instance AClass SomeTree where
func = 0
instance AClass Double where
func = 1
foo :: Int
foo = func @SomeTree + func @Double
{-# LANGUAGE ScopedTypeVariables, UnicodeSyntax #-}
bar :: ∀ a . AClass a => a -> Int
bar _ = func @a
∀ a . is just what you implicitly have in functions like sum :: Num a => [a] -> a, which is actually shorthand for sum :: ∀ a . Num a => [a] -> a. The difference is that the explicit ∀ (aka forall) brings the a into scope in such a way that it can be used in the function body, as here with func @a.
neutral :: Int, should work, since information both is gathered from the input type and output type.funchere is just anInt. Use it like any otherInt. The bigger issue is that you can't definefuncto have a type that doesn't involveain some way.TypeApplications:func @Double.{-# LANGUAGE TypeApplications #-}to your file, which enables a new@notation for providing type arguments. Then, you can writefunc @SomeTree, which will yield 0, orfunc @Double, which will yield 1. See the docs