Hi,
the answer is not that obvious... Here are the types of the two map functions:

1
2
3
4
5
 
> Seq.map;;
val it : (('a -> 'b) -> #seq<'a> -> seq<'b>) = <fun:clo@0_1>
> List.map;;
val it : (('a -> 'b) -> 'a list -> 'b list) = <fun:clo@0_2>

The key difference between these two is that the first one takes #seq<'a> as a second argument, while th second one uses 'a list (which can be also written as list<'a>). The difference is the # sign, which means that the type can be any type that implements the seq<'a> interface, so actually the signature can be written like this:

1
2
 
Seq.map : (('a -> 'b) -> 'c -> seq<'b>) when 'c :> seq<'a>

The problem with currying in this case is that if you write Seq.map foo, then you fix the type arguments 'a and 'b, but 'c still remains a free type variable, which causes value restriction (because you can't declare a generic value). Adding a constraint (as you mentioned) fixes the 'c type variable, which resolves this issue.

By on 10/23/2007 12:38 PM ()

that makes sense. i hadn't realised that #seq produces a free type variable
in the same way that 'a does. that's actually quite restrictive, isn't it?
maybe i've been getting exposed to too much haskell, trying to
understand the monads references in the computation expression
stuff...

By on 10/23/2007 2:36 PM ()

The type signature itself is not more restrictive -- it is actually more flexible than the OO approach of allowing subsumption at call sites. E.g., you can constrain an argument to implement two or more interfaces.

1
val f : 'a -> ... when 'a :> IFoo and 'a :> IBar

The thing that makes it more restrictive is the dreaded value restriction. This is something F# inherits from OCaml. It has practical benefits for language implementation but is the bane of any Haskell programmer starting out in F#.

Also in your example, if this were a full program you would more likely get around the value restriction by using a syntactic function. E.g.,

1
let f x = Seq.map myquote x
By on 10/24/2007 7:13 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper