[Haskell-cafe] [Haskell] Higher types in contexts

AntC anthony_clayden at clear.net.nz
Mon Mar 5 23:13:11 CET 2012

>I don't know what you want to do, but you may wrap the (forall a. [a] -> 
[a]) in an existential type: 

>data ListFunc = forall a. ListFunc ([a] -> [a]) 

>class Has r Rev ListFunc => HRClass r where 
>  setHRClass :: ListFunc -> r -> r 

Thanks Henning,

What we're wanting to do is set the Higher-ranked function into a record
type, then get it and apply it polymorphically. SPJ's example is:

data HR = HR { rev :: forall a. [a] -> [a] }          -- where rev is the
label for the H-R function

f :: HR -> ([Bool], [Char])
f r = (r.rev [True], r.rev "hello")                         -- where r.rev
is new syntax to get the func from HR

I've tried that ListFunc wrapping you suggest:

data HR = HR { rev :: ListFunc }

rHR1 = HR{ rev = ListFunc reverse }                  -- put the `reverse`
function into the record type
the setHRClass method would do this

But I can't 'dig out' the H-R function and apply it (not even

case (rev rHR1) of { (ListFunc fn) -> fn "hello" }

==>     Couldn't match type `a' with `Char'
      `a' is a rigid type variable bound by
          a pattern with constructor
            ListFunc :: forall a. ([a] -> [a]) -> ListFunc,
          in a case alternative
          at <interactive>:0:25
    Expected type: [a]
      Actual type: [Char]

SPJ's approach (without a wrapper, but with some fancy instance constraints)
can 'dig out' the function and apply it polymorphically, but he can't get
the function into the record without using an explicit data constructor.

What am I doing wrong?


View this message in context: http://haskell.1045720.n5.nabble.com/Re-Haskell-Higher-types-in-contexts-tp5537428p5539147.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

More information about the Haskell-Cafe mailing list