Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic defaulting in Haskell

Tags:

haskell

ghc

Given two functions:

f :: (A a, B b) => a -> b
g :: (B b, C c) => b -> c

Is there any way (in GHC) I can make it possible to write:

h x = g (f x)

Without having to add a type signature for f x, e.g.

h x = g ((f x) :: T)

By having some "default type" that f x takes if none is specified?

I suspect I need something like Defaulting in Haskell Prime but has this been implemented in GHC (or in GHC head)?

like image 469
Clinton Avatar asked Feb 19 '15 14:02

Clinton


1 Answers

Stupid non-answer:

asT :: T -> T
asT = id

h = g . asT . f

Lack of "defaulting" is one of the pains of Haskell's flavor of generics. Things get too generic and Haskell doesn't know which instance to pick. The status quo is that "the programmer needs to explicitly resolve ambiguity." Rather than specifying top-level rules for defaulting, you just have to select the appropriate instance on a case-by-case basis. Num defaulting is a hacky exception to this rule.

The proposal you linked has a good example of why choosing defaults isn't trivial when multiple classes are involved.

default A (Int, String, ())
default B (String, Int, ())
(A t, B t) => t -- defaults to what?
like image 67
Dan Burton Avatar answered Oct 25 '22 01:10

Dan Burton