On Wed, 2006-10-04 at 23:56 +1000, Jonathan Kelly wrote:
> Hi,

BTW: since you're a developer now you should register for
the felix-impl mailing list.

> I sort of understand, but still puzzled. In the following code, because 
> the member function is generic, the parameters are generic, and are 
> resolved when it's called. I was thinking I should be able to have a non 
> generic "eq(itm, head)" but I'm required to have a generic version of 
> eq, even though the function is only called with T is int, 

Your function isn't allowed to typecheck,
and then fail for some values of T and not others,
as C++ can do. If Felix polymorphic functions bind and
type check .. they will work for all T.

> and there 
> exists an eq:int*int->bool so it should be ok. So that means the 
> arguements aren't generic when it's being type checked, 

The body of the function is type checked independently of any call 
to it. There are no arguments .. but the parameters are certainly
polymorphic.

> So that's why generic functions never really do anything, all the "work" 
> is done by a function that's passed in with a signature appropriate to 
> the context ...

It's called extensional polymorphism: the functions are 'monomorphised'
or 'specialised' just before code generation.. in fact the
monomorphisation is done ONLY by the code generator. Even the
instantiator .. which tracks the monomorphic instances .. doesn't
actually create monomorphised versions of them, it just calculates
which specialisations WILL be needed at C++ compile time: the
code generator plugs in the values of the type variables
and generates C++ at the same time (on the fly).

Ocaml does intensional polymorphism: everything is a pointer,
so one function can work for all T (since T is really a T*,
the function just uses a void* at run time).

Felix may be able to do that one day, it saves code bloat,
but can reduce performance.

> //------------
> #import <flx.flxh>;
> 
> open List;
> 
> fun eq[T] : T * T -> bool = "$1==$2";
> 
> fun member[T] (itm:T) (lst:list[T]) : bool =
> {
>     return
>     match lst with
>         | Empty[T] => false
>         | Cons[T] (?head, ?tail) =>
>             if eq[T](itm, head) then
>                 true
>             else
>                 member itm tail
>             endif
>     endmatch;
> }
> 
> var x1 = 1;
> var x2 = 2;
> var x3 = 3;
> var x4 = 4;
> 
> var ilst = Cons( x1, Cons( x2, Cons( x3, Cons( x4, Empty[ int]))));

I think you can write:

        list(1,2,3,4)

The argument is a tuple, but the list function converts it
to a list.

> print (member (x3) ilst);
> endl;

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Felix-language mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to