User loginNavigation |
Those pesky higher-rank types. Or how to type \f x y. (f x, f y)As people might have noticed I've been somewhat interested in how to type g = \f x y. (f x, f y) Now you might think HM, as implemented in various languages, does a nice job typing that. Ocaml gives the most readable result as # let g f x y = (f x, f y);; val g : ('a -> 'b) -> 'a -> 'a -> 'b * 'b = Or you could go higher rank and type it g :: (forall a b. a -> b) -> a -> b -> (c, d) g = \f x y -> (f x, f y) But, for various reasons, I want to type it def g: (a -> f a) -> b -> c -> (f b, f c) = [f, x, y -> (f x, f y)] Since I am lazy and assume everything has been done in literature somewhere, does anyone have a reference with a typing system like that? PS. 'Solved' in Haskell. Courtesy of Sjoerd Visscher, Haskell allows for quantification over type constructors; though I want to avoid explicit quantifcation. g :: (forall a. a -> f a) -> b -> c -> (f b, f c) g f x y = (f x, f y) PPS. Gashe noticed that Haskell is unable to recognize that f should sometimes be unificated with 'id = /\a. a' *Main> g (+ 2) 3 4 :27:4: Couldn't match type `a' with `f0 a' `a' is a rigid type variable bound by a type expected by the context: a -> f0 a at :27:1 Expected type: a -> f0 a Actual type: a -> a In the first argument of `g', namely `(+ 2)' In the expression: g (+ 2) 3 4 In an equation for `it': it = g (+ 2) 3 4 By marco at 2014-05-19 11:21 | LtU Forum | previous forum topic | next forum topic | other blogs | 11216 reads
|
Browse archives
Active forum topics |
Recent comments
20 weeks 1 day ago
20 weeks 1 day ago
20 weeks 1 day ago
42 weeks 2 days ago
46 weeks 4 days ago
48 weeks 1 day ago
48 weeks 1 day ago
50 weeks 6 days ago
1 year 3 weeks ago
1 year 3 weeks ago