On Thu, 2006-08-31 at 17:50 -0700, Erick Tryzelaar wrote:
> skaller wrote:
> >> fun compose[T,U,V] (f:T->U, g:U->V) => fun (x:T) => g(f(x));
> >>
> >
> > .. teaches me something new. It can do that??
> >
> How so, other than me explicitly currying, verses your implicit style?
Precisely. I didn't realise it could parse that.
> Is it the returning of the generic function?
It isn't returning a generic function, Felix cannot do that.
The quantifiers are all leading: the 'for all' is at the beginning
of the declaration. Really, there are no polymorphic functions
in Felix at all. A parameterised definition just defines
an infinite set: the typing is to make a type schema,
using meta types, NOT using genuine type variables.
This only allows first order polymorphism.
The above should work, because within
f[int, long, float] ..
the fun (x:T) .. is actually of type int -> float.
If you had written:
fun compose[U, V] ... => fun[T] (x:T) => ..
it would have failed. you can't assign a variable
a polymorphic functions (only an instance of it).
It may LOOK like you can .. within a function for
which the type variable is instantiated already.
But there are polymorphic variables in Felix:
the local variables of a polymorphic functions,
even if they have type T, are actually monomorphic.
Ocaml supports real polymorphic functions, Felix doesn't.
let f x = x,x;;
Unfortunately, Ocaml typing is Hindley Milner, which only allows
first order polymorphism, the same as Felix. But the run time
system supports real polymorphic functions, you can do this:
let g f x = f x;;
g f 1; (* return 1,1 *)
g f 1.0; (* returns 1.0, 1.0 *)
but you cannot do this:
let g f x y = f x, f y
because inside the function g, the function f cannot be
polymorphic.
However Ocaml DOES allow genuine polymorphic functions
to be used in records and classes: method can be
polymorphic:
class X ...
method f: 'a . 'a -> 'a * 'a
.. works!
Basically .. Hindley Milner type inference is crap,
more generally, type inference is crap. Once you get
to higher order polymorphism you have to annotate the
types anyhow.
> If so, I guess it's a good
> thing when the type system does something unexpectantly correct :)
>
> Also, if you change the "x:T" to just "x", you get this ocaml exception:
>
> #FLX_INSTALL_DIR=.
> #PKGCONFIG=./bin/flx_pkgconfig
> env PATH=./bin:$PATH ./bin/flxg -q --inline=10
> --elkhound=./bin/flx_elkhound -I./lib projects/foo
> SYSTEM FAILURE
> Unexpected `TYP_none in bind type
If you omit the type of a parameter, it defaults to a fresh
type variable:
fun f(x)=>x,x;
is just fine, it means:
fun f[t] (x:t):t * t=> x,x;
where 't' is created by the compiler. Although you don't
know the name of this type, you can refer to it in the
body of the function:
fun f(x)= { typedef xtype = typeof x; .. return x, x; }
and maybe even in the signature the same way (not sure ..):
fun f(x, y: typeof x) ...
which is similar to Eiffel 'like' feature.
--
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Felix-language mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/felix-language