List Info

Thread: "ocaml_beginners"::[] Asserting polymorphism with annotations




"ocaml_beginners"::[] Asserting polymorphism with annotations
country flaguser name
United States
2007-06-16 11:59:24

Is there anyway to force the compiler to check that a function is
as polymorphic as its annotation?

I've large function that ocaml infers a less general type than I
expect, so I'd like to annotate some subexpressions until I can see
where the extra unification is happening.

E.g. say there's a function I expect to be polymorphic with a type
like 'a -> 'b -> bool, but in the body of the function I have an
expression which forces 'a='b. Can somehow I stop 'a and 'b from
being unified? Can I force a compile time error for something like "let
f (x:'a) (y:'b) : bool = x = y" instead of inferring "f: 'a -> 'a ->
bool"?

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Asserting polymorphism with annotations
country flaguser name
United Kingdom
2007-06-16 12:05:07

On Saturday 16 June 2007 17:59:24 ethan.aubin wrote:
> E.g. say there's a function I expect to be polymorphic with a type
>; like 'a -> 'b -> bool, but in the body of the function I have an
> expression which forces 'a='b. Can somehow I stop 'a and 'b from
>; being unified? Can I force a compile time error for something like "let
> f (x:'a) (y:'b) : bool = x = y" instead of inferring "f: 'a -> 'a ->
> bool"?

I don't believe you can. Your best bet is probably to break the large function
into many smaller functions, which is better ML style anyway.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/?e

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Asserting polymorphism with annotations
country flaguser name
Germany
2007-06-16 12:37:13

Zitat von Jon Harrop < jon%40ffconsultancy.com">jonffconsultancy.com&gt;:

&gt; On Saturday 16 June 2007 17:59:24 ethan.aubin wrote:
&gt; > E.g. say there's a function I expect to be polymorphic with a type
>; > like 'a -> 'b -> bool, but in the body of the function I have an
> > expression which forces 'a='b. Can somehow I stop 'a and 'b from
>; > being unified? Can I force a compile time error for something like "let
> > f (x:'a) (y:'b) : bool = x = y" instead of inferring "f: 'a -> 'a ->
&gt; > bool"?
>
> I don't believe you can.
[...]

And I see no reason why it should be possible or necessary...

...otherwise you could use Perl:

====================
oliversiouxsie ~ $ cat useperl.pl
#!/usr/bin/perl

print "use perl!n&quot; if "7&quot; == 7;
oliversiouxsie ~ $ perl useperl.pl
use perl!
oliversiouxsie ~ $
====================

Ciao,
Oliver

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Asserting polymorphism with annotations
country flaguser name
Belgium
2007-06-16 14:43:35
On Sat, 16 Jun 2007, "ethan.aubin"
<ethan.aubin+yahoogmail.com> wrote:
> 
> I've large function that ocaml infers a less general
type than I
> expect, so I'd like to annotate some subexpressions
until I can see
> where the extra unification is happening.

Compile with -dtypes and use Emacs (with an ocaml mode) to
display the
types of the subexpressions you want.

My 0.02¤,
ChriS


Archives up to November 11, 2006 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/

The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    ht
tp://groups.yahoo.com/group/ocaml_beginners/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:ocaml_beginners-digest@yahoogroups.com 
    mailto:ocaml_beginners-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 

Re: "ocaml_beginners"::[] Asserting polymorphism with annotations
country flaguser name
Belgium
2007-06-16 15:05:33

On Sat, 16 Jun 2007, "ethan.aubin&quot; < ethan.aubin%2Byahoo%40gmail.com">ethan.aubin+yahoogmail.com> wrote:
&gt;
> E.g. say there's a function I expect to be polymorphic with a type
>; like 'a -> 'b -> bool, but in the body of the function I have an
> expression which forces 'a='b. Can somehow I stop 'a and 'b from
>; being unified? Can I force a compile time error for something like "let
> f (x:'a) (y:'b) : bool = x = y" instead of inferring "f: 'a -> 'a ->
&gt; bool"?

No. If it was possible, then you could write [f 1 "1&quot;]. What should
OCaml do when you reach [1 = "1&quot;] ? Return true or false? According
to what you expect, define you type and the equality on it. You can
also pass an 'equality' (sic) function with type [eq: 'a -> 'b ->
bool] and the inferred type will be the one you want.

ChriS

__._,_.___
.

__,_._,___
[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )