caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] tpyping question
@ 2003-08-11  1:13 skaller
  2003-08-11  2:46 ` Jacques Garrigue
  2003-08-11  2:51 ` Oleg Trott
  0 siblings, 2 replies; 12+ messages in thread
From: skaller @ 2003-08-11  1:13 UTC (permalink / raw)
  To: caml-list

type x = [`A | `B of int | `V of float];;
let g (a:x) = a;;
let f a = match a with
| `A -> g a
(* | `B _ *) | _ -> g a;;

Compiles fine, but gives an error if the comments are
removed.

File "xx.ml", line 4, characters 10-11:
This expression has type [< `A | `B of 'a ] but is here used with type
  x = [ `A | `B of int | `V of float ]
The first variant type does not allow tag(s) `V

I can fix this with a coercion/annotation:

| (`A:x) -> g a

but that's a bit messy. Of course an annotation on the
argument doesn't work (since such annotations are only
consulted *after* the body type is infered).

Is there a better way to do this?


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] tpyping question
  2003-08-11  1:13 [Caml-list] tpyping question skaller
@ 2003-08-11  2:46 ` Jacques Garrigue
  2003-08-11  4:30   ` skaller
  2003-08-12  3:24   ` Jacques Garrigue
  2003-08-11  2:51 ` Oleg Trott
  1 sibling, 2 replies; 12+ messages in thread
From: Jacques Garrigue @ 2003-08-11  2:46 UTC (permalink / raw)
  To: skaller; +Cc: caml-list

From: skaller <skaller@ozemail.com.au>

> type x = [`A | `B of int | `V of float];;
> let g (a:x) = a;;
> let f a = match a with
> | `A -> g a
> (* | `B _ *) | _ -> g a;;
> 
> Compiles fine, but gives an error if the comments are
> removed.

Indeed, there seems to be a bug here, since the non-commented version
should be equivalent to

# let f a = match a with
  | `A ->  g a
  | `B _ -> g a
  | _ -> g a;;
val f : x -> x = <fun>

I will investigate this.
On the other hand, this code is meaningless, as _ is more general
than `B _, and the order of or-patterns is not significant.

If the code you really intended was

  let f a = match a with
    | `A ->  g a
    | `B _ -> g a

then the answer is in the manual:

  let f = function `A | `B _ as a -> g a

Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] tpyping question
  2003-08-11  1:13 [Caml-list] tpyping question skaller
  2003-08-11  2:46 ` Jacques Garrigue
@ 2003-08-11  2:51 ` Oleg Trott
  2003-08-11  4:39   ` skaller
  1 sibling, 1 reply; 12+ messages in thread
From: Oleg Trott @ 2003-08-11  2:51 UTC (permalink / raw)
  To: skaller, caml-list

On Sunday 10 August 2003 09:13 pm, skaller wrote:
> type x = [`A | `B of int | `V of float];;
> let g (a:x) = a;;
> let f a = match a with
>
> | `A -> g a
>
> (* | `B _ *) | _ -> g a;;
>
> Compiles fine, but gives an error if the comments are
> removed.
>
> File "xx.ml", line 4, characters 10-11:
> This expression has type [< `A | `B of 'a ] but is here used with type
>   x = [ `A | `B of int | `V of float ]
> The first variant type does not allow tag(s) `V
>
> I can fix this with a coercion/annotation:
> | (`A:x) -> g a
>
> but that's a bit messy. Of course an annotation on the
> argument doesn't work (since such annotations are only
> consulted *after* the body type is infered).
>
> Is there a better way to do this?

This is indeed very odd, because the equivalent

let f a = match a with `A -> g a | `B _ -> g a | _ -> g a

works fine.

-- 
Oleg Trott <oleg_trott@columbia.edu>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] tpyping question
  2003-08-11  2:46 ` Jacques Garrigue
@ 2003-08-11  4:30   ` skaller
  2003-08-12  3:24   ` Jacques Garrigue
  1 sibling, 0 replies; 12+ messages in thread
From: skaller @ 2003-08-11  4:30 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list

On Mon, 2003-08-11 at 12:46, Jacques Garrigue wrote:
> From: skaller <skaller@ozemail.com.au>
> 
> > type x = [`A | `B of int | `V of float];;
> > let g (a:x) = a;;
> > let f a = match a with
> > | `A -> g a
> > (* | `B _ *) | _ -> g a;;
> > 
> > Compiles fine, but gives an error if the comments are
> > removed.

> On the other hand, this code is meaningless, as _ is more general
> than `B _, and the order of or-patterns is not significant.

Yup. However, meaningless is not the same as wrong.
The extra case arose because I had a bug and 
wanted to print a diagnostic. When I commented the
print out, I got the error: I wanted to leave the
pattern match there as a stub.

I have had some other perplexing problems with
multiple tags, perhaps if you fix the above problem
they'll go a way .. sorry, I worked around them
so don't have an example at the moment.


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] tpyping question
  2003-08-11  2:51 ` Oleg Trott
@ 2003-08-11  4:39   ` skaller
  2003-08-11  5:18     ` Karl Zilles
  0 siblings, 1 reply; 12+ messages in thread
From: skaller @ 2003-08-11  4:39 UTC (permalink / raw)
  To: Oleg Trott; +Cc: caml-list

On Mon, 2003-08-11 at 12:51, Oleg Trott wrote:

> This is indeed very odd, because the equivalent
> 
> let f a = match a with `A -> g a | `B _ -> g a | _ -> g a
> 
> works fine.

Yes, but there are some strange things happen with
polymorphic variants -- I mean sometimes you need a
coercion and sometimes a double coercion, and I
don't have that strong a grasp on the typing to really
be sure what's a limitation of the inference engine
and whats not: as a human I sometimes read 'intent'
into something that the compiler doesn't :-)

BTW: something I've needed is like this:

type [`A of int * int | `B of int]
let f x = match x with
| `A (i,j)
| `B i with j=0 -> ....
(*     ********   *)

At the moment, I have to code this as:

let f x =
  let g i j = ... in
  match x with
  | `A (i,j) -> g i j
  | `B i -> g i 0

delocalising the case handling code.


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] tpyping question
  2003-08-11  4:39   ` skaller
@ 2003-08-11  5:18     ` Karl Zilles
  2003-08-11 18:13       ` Karl Zilles
  2003-08-11 18:16       ` skaller
  0 siblings, 2 replies; 12+ messages in thread
From: Karl Zilles @ 2003-08-11  5:18 UTC (permalink / raw)
  To: skaller; +Cc: Oleg Trott, caml-list

skaller wrote:
> BTW: something I've needed is like this:
> 
> type [`A of int * int | `B of int]
> let f x = match x with
> | `A (i,j)
> | `B i with j=0 -> ....
> (*     ********   *)
> 
> At the moment, I have to code this as:
> 
> let f x =
>   let g i j = ... in
>   match x with
>   | `A (i,j) -> g i j
>   | `B i -> g i 0
> 
> delocalising the case handling code.
> 

Wouldn't it be more natural to write:

let f x =
    let (i,j) = match x with
	| `A v -> v
         | `B u -> (u,0) in
    ...


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] tpyping question
  2003-08-11  5:18     ` Karl Zilles
@ 2003-08-11 18:13       ` Karl Zilles
  2003-08-11 18:16       ` skaller
  1 sibling, 0 replies; 12+ messages in thread
From: Karl Zilles @ 2003-08-11 18:13 UTC (permalink / raw)
  To: Karl Zilles; +Cc: skaller, Oleg Trott, caml-list

Karl Zilles wrote:
> 
> Wouldn't it be more natural to write:
> 
> let f x =
>    let (i,j) = match x with
>     | `A v -> v
>         | `B u -> (u,0) in
>    ...

Nevermind, this fails to deal with
       | `C -> do something different


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] tpyping question
  2003-08-11  5:18     ` Karl Zilles
  2003-08-11 18:13       ` Karl Zilles
@ 2003-08-11 18:16       ` skaller
  2003-08-11 19:02         ` Remi Vanicat
  2003-08-11 19:02         ` Tiphaine Turpin
  1 sibling, 2 replies; 12+ messages in thread
From: skaller @ 2003-08-11 18:16 UTC (permalink / raw)
  To: Karl Zilles; +Cc: caml-list

On Mon, 2003-08-11 at 15:18, Karl Zilles wrote:
> skaller wrote:
> > BTW: something I've needed is like this:
> > 
> > type [`A of int * int | `B of int]
> > let f x = match x with
> > | `A (i,j)
> > | `B i with j=0 -> ....
> > (*     ********   *)
> > 
> > At the moment, I have to code this as:
> > 
> > let f x =
> >   let g i j = ... in
> >   match x with
> >   | `A (i,j) -> g i j
> >   | `B i -> g i 0
> > 
> > delocalising the case handling code.
> > 
> 
> Wouldn't it be more natural to write:
> 
> let f x =
>     let (i,j) = match x with
> 	| `A v -> v
>          | `B u -> (u,0) in
>     ...

In reality there are 20 tags, not just the 2 shown.
So I'd have to write:

match x with
| .....
| `A _ as y 
| `B _ as y -> let (i,j) = match y with `A v->v | `B u -> (u,0) in ...
| ......

repeating the tags, In fact I do this sometimes, but it's also poor
style repeating the tags. So I have 2 choices of poor style:
repeat tags or delocalise handler, hence my suggestion,
in which the handler is local and there's no repetition.



-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] tpyping question
  2003-08-11 18:16       ` skaller
@ 2003-08-11 19:02         ` Remi Vanicat
  2003-08-12  4:25           ` skaller
  2003-08-11 19:02         ` Tiphaine Turpin
  1 sibling, 1 reply; 12+ messages in thread
From: Remi Vanicat @ 2003-08-11 19:02 UTC (permalink / raw)
  To: caml-list

skaller <skaller@ozemail.com.au> writes:

> In reality there are 20 tags, not just the 2 shown.
> So I'd have to write:
>
> match x with
> | .....
> | `A _ as y 
> | `B _ as y -> let (i,j) = match y with `A v->v | `B u -> (u,0) in ...
> | ......

If `A (x,y) and `B x have a link (as it seem in your
example) I would have define the type 

type aorB = [`A of int * int | `B of int]

and then your code become :

match x with
| .....
| #aorB as y -> let (i,j) = match y with `A v->v | `B u -> (u,0) in ...
| ......

by the way, you only need one as for the | `A ... | `B ... pattern
there is only one as, not two . 

>
> repeating the tags, In fact I do this sometimes, but it's also poor
> style repeating the tags. So I have 2 choices of poor style:
> repeat tags or delocalise handler, 

repeat tags is the best way IMHO (to avoid to correct the bugs in only
one of the two branch).

-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] tpyping question
  2003-08-11 18:16       ` skaller
  2003-08-11 19:02         ` Remi Vanicat
@ 2003-08-11 19:02         ` Tiphaine Turpin
  1 sibling, 0 replies; 12+ messages in thread
From: Tiphaine Turpin @ 2003-08-11 19:02 UTC (permalink / raw)
  Cc: caml-list

Hello,

 here is another solution, but it's not better if there are many cases


match x, 0 with
  |  `A n, _
  | `B, n -> g n
  | `C x, _ -> ...
;;




skaller wrote:

>On Mon, 2003-08-11 at 15:18, Karl Zilles wrote:
>  
>
>>skaller wrote:
>>    
>>
>>>BTW: something I've needed is like this:
>>>
>>>type [`A of int * int | `B of int]
>>>let f x = match x with
>>>| `A (i,j)
>>>| `B i with j=0 -> ....
>>>(*     ********   *)
>>>
>>>At the moment, I have to code this as:
>>>
>>>let f x =
>>>  let g i j = ... in
>>>  match x with
>>>  | `A (i,j) -> g i j
>>>  | `B i -> g i 0
>>>
>>>delocalising the case handling code.
>>>
>>>      
>>>
>>Wouldn't it be more natural to write:
>>
>>let f x =
>>    let (i,j) = match x with
>>	| `A v -> v
>>         | `B u -> (u,0) in
>>    ...
>>    
>>
>
>In reality there are 20 tags, not just the 2 shown.
>So I'd have to write:
>
>match x with
>| .....
>| `A _ as y 
>| `B _ as y -> let (i,j) = match y with `A v->v | `B u -> (u,0) in ...
>| ......
>
>repeating the tags, In fact I do this sometimes, but it's also poor
>style repeating the tags. So I have 2 choices of poor style:
>repeat tags or delocalise handler, hence my suggestion,
>in which the handler is local and there's no repetition.
>
>
>
>-------------------
>To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
>Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
>Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>  
>



-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] tpyping question
  2003-08-11  2:46 ` Jacques Garrigue
  2003-08-11  4:30   ` skaller
@ 2003-08-12  3:24   ` Jacques Garrigue
  1 sibling, 0 replies; 12+ messages in thread
From: Jacques Garrigue @ 2003-08-12  3:24 UTC (permalink / raw)
  To: skaller; +Cc: caml-list

> From: skaller <skaller@ozemail.com.au>
> 
> > type x = [`A | `B of int | `V of float];;
> > let g (a:x) = a;;
> > let f a = match a with
> > | `A -> g a
> > (* | `B _ *) | _ -> g a;;
> > 
> > Compiles fine, but gives an error if the comments are
> > removed.
> 
> Indeed, there seems to be a bug here, since the non-commented version
> should be equivalent to
> 
> # let f a = match a with
>   | `A ->  g a
>   | `B _ -> g a
>   | _ -> g a;;
> val f : x -> x = <fun>

This is now fixed in CVS.
Redundant cases of or-patterns (i.e. subsumed by another case) are now
ignored in exhaustivity check.
This confirms what I said before: this was only a problem with
redundant patterns. These are completely superfluous now, as pattern
matching does not respect the order of patterns in or-patterns, but
this may indeed change in the future:
  let f = function
    | `A -> g `A
    | `B a | a -> g a
could become meaningful.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] tpyping question
  2003-08-11 19:02         ` Remi Vanicat
@ 2003-08-12  4:25           ` skaller
  0 siblings, 0 replies; 12+ messages in thread
From: skaller @ 2003-08-12  4:25 UTC (permalink / raw)
  To: Remi Vanicat; +Cc: caml-list

On Tue, 2003-08-12 at 05:02, Remi Vanicat wrote:
> skaller <skaller@ozemail.com.au> writes:
> 
> > In reality there are 20 tags, not just the 2 shown.
> > So I'd have to write:
> >
> > match x with
> > | .....
> > | `A _ as y 
> > | `B _ as y -> let (i,j) = match y with `A v->v | `B u -> (u,0) in ...
> > | ......
> 
> If `A (x,y) and `B x have a link (as it seem in your
> example) I would have define the type 
> 
> type aorB = [`A of int * int | `B of int]

I'd consider that if the relationship were strong
enough to abstract into a named type, meaning,
it applies to more than one or two switchings (match).

Unfortunately, the nature of compilers seem to be that
in each function the groupings are different. The reason for
this is probably that if they were not, then you'd not *just*
make a type unifying the two tags .. you'd just use a single
tag :-)



-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2003-08-12  4:25 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-08-11  1:13 [Caml-list] tpyping question skaller
2003-08-11  2:46 ` Jacques Garrigue
2003-08-11  4:30   ` skaller
2003-08-12  3:24   ` Jacques Garrigue
2003-08-11  2:51 ` Oleg Trott
2003-08-11  4:39   ` skaller
2003-08-11  5:18     ` Karl Zilles
2003-08-11 18:13       ` Karl Zilles
2003-08-11 18:16       ` skaller
2003-08-11 19:02         ` Remi Vanicat
2003-08-12  4:25           ` skaller
2003-08-11 19:02         ` Tiphaine Turpin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).