List Info

Thread: "ocaml_beginners"::[] Type mismatch but why?




"ocaml_beginners"::[] Type mismatch but why?
user name
2007-01-05 03:03:00

Hi,
I have this snippet of code that drives me crazy. I just don't see why
my types are mismatching. It's rather ugly code anyway but I couldn't
think of a nice way of putting it so far. The error I get is

# Characters 107-578:
....match (ghostptcls) with
[] -> newghostparts
| (gp :: gplist) -> let newgp = { r = gp; v = opart.v; ... } in
let newlist = newgp :: newghostparts in newgparticles opart
gplist newlist..
This expression has type Particle.properties list but is here
used with type Particle.properties list -> Particle.properties list

The code is

(* creates new ghostparticles *)
let rec newgparticles opart ghostptcls ?(newghostparts = [opart]) =
match (ghostptcls) with
[] -> newghostparts
| (gp :: gplist) -> let newgp = { r = gp; v = opart.v; ... } in let
newlist = newgp :: newghostparts in newgparticles opart gplist newlist

(* wrapper for ghostparticles routine in Vectorop *)
let get_ghostparticles ~particle:particle =
let list_of_ghostparticles = Vectorop.ghostparticle_loc particle.r
in newgparticles particle list_of_ghostparticles []

opart should be a single particle of type Particle.properties.

Any hints what I am doing wrong?

TIA,
Christian

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Type mismatch but why?
user name
2007-01-05 03:29:42

It's because your optional argument can't be erased, confusing the type checker.

Adding an explicit type (foo list) to newghostparts,

Expecting function has type ?newghostparts:foo list -> foo list
This argument cannot be applied without label

Adding the label ~newghostparts to newlist in final line:

Warning X: this optional argument cannot be erased.
let rec newgparticles opart ghostptcls ?(newghostparts = [opart]) =
^^^^^^^
val newgparticles : foo -> foo list -> ?newghostparts:foo list -> foo list =
<fun>;

I've never quite fully understood labels. Perhaps the fully applied
function doesn't need labels thing doesn't apply to optional arguments
-- which seems a shame.

Anyways, the workaround is to label the optional argument. And to
eliminate the warning, either change the order so it's not at the end,
or add a dummy argument to the end (unit).

Jonathan

On 1/5/07, Christian Lerrahn < ocaml%40penpal4u.net">ocamlpenpal4u.net> wrote:
&gt; Hi,
> I have this snippet of code that drives me crazy. I just don't see why
> my types are mismatching. It's rather ugly code anyway but I couldn't
> think of a nice way of putting it so far. The error I get is
>
> # Characters 107-578:
> ....match (ghostptcls) with
> [] -> newghostparts
> | (gp :: gplist) -> let newgp = { r = gp; v = opart.v; ... } in
> let newlist = newgp :: newghostparts in newgparticles opart
>; gplist newlist..
> This expression has type Particle.properties list but is here
> used with type Particle.properties list -> Particle.properties list
>
> The code is
>
> (* creates new ghostparticles *)
> let rec newgparticles opart ghostptcls ?(newghostparts = [opart]) =
> match (ghostptcls) with
> [] -> newghostparts
> | (gp :: gplist) -> let newgp = { r = gp; v = opart.v; ... } in let
> newlist = newgp :: newghostparts in newgparticles opart gplist newlist
&gt;
> (* wrapper for ghostparticles routine in Vectorop *)
> let get_ghostparticles ~particle:particle =
> let list_of_ghostparticles = Vectorop.ghostparticle_loc particle.r
> in newgparticles particle list_of_ghostparticles []
>
> opart should be a single particle of type Particle.properties.
>
>; Any hints what I am doing wrong?
&gt;
> TIA,
> Christian
>
>
> 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
>;
>
&gt;
>

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Type mismatch but why?
user name
2007-01-05 04:43:22

Jonathan Roewen wrote:
&gt; ...
> I've never quite fully understood labels. Perhaps the fully applied
&gt; function doesn't need labels thing doesn't apply to optional arguments
> -- which seems a shame.
&gt;
> Anyways, the workaround is to label the optional argument. And to
> eliminate the warning, either change the order so it's not at the end,
> or add a dummy argument to the end (unit).

Just to emphasize this point, from "4.1.1 Optional arguments&quot; of the
OCaml manual we have

&quot;A function taking some optional arguments must also take at least one
non-labeled argument. This is because the criterion for deciding
whether an optional has been omitted is the application on a
non-labeled argument appearing after this optional argument in the
function type.";

While this may not directly address your "fully applied&quot; issue, it
definitely covers the ordering thing.

Robert Roessler
robertr%40rftp.com">robertrrftp.com
http://www.rftp.com

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Type mismatch but why?
user name
2007-01-05 04:41:28

Hi Jonathan,
I feel stupid but I didn't understand your remedy. I tried to
explicitly state the type of my optional argument but the error stayed
the same. Would you mind writing out what you explained? I just seem to
be too stupid to figure out a verbal explanation.

Cheers,
Christian

Am Fri, 5 Jan 2007 16:29:42 +1300
schrieb "Jonathan Roewen&quot; < jonathan.roewen%40gmail.com">jonathan.roewengmail.com>:

&gt; It's because your optional argument can't be erased, confusing the
> type checker.
>
> Adding an explicit type (foo list) to newghostparts,
>
> Expecting function has type ?newghostparts:foo list -> foo list
> This argument cannot be applied without label
>;
> Adding the label ~newghostparts to newlist in final line:
>;
> Warning X: this optional argument cannot be erased.
&gt; let rec newgparticles opart ghostptcls ?(newghostparts = [opart]) =
> ^^^^^^^
&gt; val newgparticles : foo -> foo list -> ?newghostparts:foo list -> foo
> list = <fun>;
>
> I've never quite fully understood labels. Perhaps the fully applied
&gt; function doesn't need labels thing doesn't apply to optional arguments
> -- which seems a shame.
&gt;
> Anyways, the workaround is to label the optional argument. And to
> eliminate the warning, either change the order so it's not at the end,
> or add a dummy argument to the end (unit).
&gt;
> Jonathan
>
> On 1/5/07, Christian Lerrahn < ocaml%40penpal4u.net">ocamlpenpal4u.net> wrote:
&gt; > Hi,
> > I have this snippet of code that drives me crazy. I just don't see
> > why my types are mismatching. It's rather ugly code anyway but I
> > couldn't think of a nice way of putting it so far. The error I get
> > is
> >
> > # Characters 107-578:
> > ....match (ghostptcls) with
> > [] -> newghostparts
> > | (gp :: gplist) -> let newgp = { r = gp; v = opart.v; ... } in
> > let newlist = newgp :: newghostparts in newgparticles opart
>; > gplist newlist..
> > This expression has type Particle.properties list but is here
> > used with type Particle.properties list -> Particle.properties list
> >
> > The code is
> >
> > (* creates new ghostparticles *)
> > let rec newgparticles opart ghostptcls ?(newghostparts = [opart]) =
> > match (ghostptcls) with
> > [] -> newghostparts
> > | (gp :: gplist) -> let newgp = { r = gp; v = opart.v; ... } in
> > let newlist = newgp :: newghostparts in newgparticles opart gplist
&gt; > newlist
&gt; >
> > (* wrapper for ghostparticles routine in Vectorop *)
> > let get_ghostparticles ~particle:particle =
> > let list_of_ghostparticles = Vectorop.ghostparticle_loc
>; > particle.r in newgparticles particle list_of_ghostparticles []
> >
> > opart should be a single particle of type Particle.properties.
> >
> > Any hints what I am doing wrong?
&gt; >
> > TIA,
> > Christian
> >
> >
> > 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
>; >
> >
> >
> >

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Type mismatch but why?
user name
2007-01-05 05:33:40

> > > The code is
> > >
> > > (* creates new ghostparticles *)
> > > let rec newgparticles opart ghostptcls ?(newghostparts = [opart]) =
> > > match (ghostptcls) with
> > > [] -> newghostparts
> > > | (gp :: gplist) -> let newgp = { r = gp; v = opart.v; ... } in
> > > let newlist = newgp :: newghostparts in newgparticles opart gplist
&gt; > > newlist

let newlist = ... in
newgparticles opart gplist ~newghostparts:newlist

And you'll get the warning about the optional argument not being able
to be erased...

Jonathan

__._,_.___
.

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

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