caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Polymorphic variant abbreviations
@ 2001-09-18 22:48 Patrick M Doane
  2001-09-19  0:05 ` Patrick M Doane
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Patrick M Doane @ 2001-09-18 22:48 UTC (permalink / raw)
  To: caml-list

I have some code that would like to use polymorphic type abbreviations
along with labels.  It seems to be not behaving quite like I expected.
What could I do to make this shorter:

  type a = [ `A ]

  let f ~a:(#a as a) = ...

I was hoping that I could use this:

  let f ~a:#a = ...

but then it says that 'a' is unbound.


I also find that the error messages are not as useful as they could be
with the type abbreviations. E.g:

  This expression has type ... but is here used with #a as 'a = 'a

Would it be hard to expand '#a' like this so I don't have to track down
the definition? 

  This expression has type ... but is here used with #a = [ `A ]

Thanks for any help or suggestions,
Patrick

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Polymorphic variant abbreviations
  2001-09-18 22:48 [Caml-list] Polymorphic variant abbreviations Patrick M Doane
@ 2001-09-19  0:05 ` Patrick M Doane
  2001-09-19  8:37   ` Laurent Vibert
  2001-09-19  8:56   ` Remi VANICAT
  2001-09-19  8:01 ` Laurent Vibert
  2001-09-19 12:45 ` Jacques Garrigue
  2 siblings, 2 replies; 8+ messages in thread
From: Patrick M Doane @ 2001-09-19  0:05 UTC (permalink / raw)
  To: caml-list

The last 2 questions were about possible minor improvements with using
abbreviations. Now I'm stumped - can anyone bail me out?

(* Some type definitions and abbreviations *)
type t = ([ `A of 'a list | `B | `C ]) as 'a 
type b = [ `B ]
type c = [ `C ]

(* A rename for one of the abbreviations *)
type b2 = b

(* Use of abbreviation and rename work okay *)
let f1 (#b as x1) (#b as x2) = `A [x1;x2]
let f2 (#b2 as x1) (#b2 as x2) = `A [x1;x2] 

(* Only direct abbreviation works with list constructor *)
let g1 ~(xs : #b list) =
  `A xs

(*
   this doesn't compile - says unbound class b2

let g2 ~(xs : #b2 list) =
  `A xs
*)

(* The first works as I would expect, the second gives a very unexpected
type. There seems to be some kind of unexpected unification going on
between terms. *)

let h1 (#b as x1) (#c as x2) = `A [x1; x2]
let h2 (x1 : #b) (x2 : #c) = `A [x1; x2]


Any ideas?

Thanks,
Patrick


-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Polymorphic variant abbreviations
  2001-09-18 22:48 [Caml-list] Polymorphic variant abbreviations Patrick M Doane
  2001-09-19  0:05 ` Patrick M Doane
@ 2001-09-19  8:01 ` Laurent Vibert
  2001-09-19 16:35   ` Patrick M Doane
  2001-09-19 12:45 ` Jacques Garrigue
  2 siblings, 1 reply; 8+ messages in thread
From: Laurent Vibert @ 2001-09-19  8:01 UTC (permalink / raw)
  To: Patrick M Doane; +Cc: caml-list

On Tue, 18 Sep 2001, Patrick M Doane wrote:

> I have some code that would like to use polymorphic type abbreviations
> along with labels.  It seems to be not behaving quite like I expected.
> What could I do to make this shorter:
> 
>   type a = [ `A ]
> 
>   let f ~a:(#a as a) = ...
> 
> I was hoping that I could use this:
> 
>   let f ~a:#a = ...

you should write :

let f ~a : #a = ...

the problem is that #a is both a type expression and a pattern, and in
your expression, it's a pattern. For example if you write :

let f ~a:int = int

the int is not a type expression !

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Polymorphic variant abbreviations
  2001-09-19  0:05 ` Patrick M Doane
@ 2001-09-19  8:37   ` Laurent Vibert
  2001-09-19  8:56   ` Remi VANICAT
  1 sibling, 0 replies; 8+ messages in thread
From: Laurent Vibert @ 2001-09-19  8:37 UTC (permalink / raw)
  To: Patrick M Doane; +Cc: caml-list

On Tue, 18 Sep 2001, Patrick M Doane wrote:


> (* The first works as I would expect, the second gives a very unexpected
> type. There seems to be some kind of unexpected unification going on
> between terms. *)
> 
> let h1 (#b as x1) (#c as x2) = `A [x1; x2]

here #b is a pattern, it is the same as
let h1 (`B as x1) (`C as x2) = `A [x1; x2]



> let h2 (x1 : #b) (x2 : #c) = `A [x1; x2]

but here it is a type expression, and is the same as
let h2 (x1 : [< `B]) (x2 : [< `C]) = `A [x1; x2]

and in a list, all argument must have the same type, tou take the
intersection of both and obtain [ ]



-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Polymorphic variant abbreviations
  2001-09-19  0:05 ` Patrick M Doane
  2001-09-19  8:37   ` Laurent Vibert
@ 2001-09-19  8:56   ` Remi VANICAT
  1 sibling, 0 replies; 8+ messages in thread
From: Remi VANICAT @ 2001-09-19  8:56 UTC (permalink / raw)
  To: caml-list

Patrick M Doane <patrick@watson.org> writes:


> (* The first works as I would expect, the second gives a very unexpected
> type. There seems to be some kind of unexpected unification going on
> between terms. *)
> 
> let h1 (#b as x1) (#c as x2) = `A [x1; x2]
> let h2 (x1 : #b) (x2 : #c) = `A [x1; x2]
> 

there is unification, a list can only contain item of same type.
-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Polymorphic variant abbreviations
  2001-09-18 22:48 [Caml-list] Polymorphic variant abbreviations Patrick M Doane
  2001-09-19  0:05 ` Patrick M Doane
  2001-09-19  8:01 ` Laurent Vibert
@ 2001-09-19 12:45 ` Jacques Garrigue
  2001-09-19 16:58   ` Patrick M Doane
  2 siblings, 1 reply; 8+ messages in thread
From: Jacques Garrigue @ 2001-09-19 12:45 UTC (permalink / raw)
  To: patrick; +Cc: caml-list

> I have some code that would like to use polymorphic type abbreviations
> along with labels.  It seems to be not behaving quite like I expected.
> What could I do to make this shorter:
> 
>   type a = [ `A ]
> 
>   let f ~a:(#a as a) = ...

Nothing. But this doesn't strike me as a very frequent idiom.

> I was hoping that I could use this:
> 
>   let f ~a:#a = ...
> 
> but then it says that 'a' is unbound.

Sure, nothing binds a here: ~a: is a label, and #a an abbreviation for
`A.

> I also find that the error messages are not as useful as they could be
> with the type abbreviations. E.g:
> 
>   This expression has type ... but is here used with #a as 'a = 'a

Could you give me the code leading to this error message?
This is clearly a bug.

> The last 2 questions were about possible minor improvements with using
> abbreviations. Now I'm stumped - can anyone bail me out?
> 
> (* Some type definitions and abbreviations *)
> type t = ([ `A of 'a list | `B | `C ]) as 'a 
> type b = [ `B ]
> type c = [ `C ]
> 
> (* A rename for one of the abbreviations *)
> type b2 = b
>
> (*
>    this doesn't compile - says unbound class b2
> 
> let g2 ~(xs : #b2 list) =
>   `A xs
> *)

I see. This is rather unexpected.
I'll try to correct this, but the problem is a bit involved.

> (* The first works as I would expect, the second gives a very unexpected
> type. There seems to be some kind of unexpected unification going on
> between terms. *)
> 
> let h1 (#b as x1) (#c as x2) = `A [x1; x2]
> let h2 (x1 : #b) (x2 : #c) = `A [x1; x2]

(#b as x1) and (x1 : #b) have completely different meanings.
The first one is just equivalent to (`B as x1), and gives x1 the type
[> `B]. The second one constrains x1 to have type #b = [< `B].
So in h2 you unify [< `A] and [< `B], which leaves no possible
constructor...
I think that I will make it an error, because this is confusing.

Jacques Garrigue
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Polymorphic variant abbreviations
  2001-09-19  8:01 ` Laurent Vibert
@ 2001-09-19 16:35   ` Patrick M Doane
  0 siblings, 0 replies; 8+ messages in thread
From: Patrick M Doane @ 2001-09-19 16:35 UTC (permalink / raw)
  To: Laurent Vibert; +Cc: caml-list

On Wed, 19 Sep 2001, Laurent Vibert wrote:

> > I was hoping that I could use this:
> > 
> >   let f ~a:#a = ...
> 
> you should write :
> 
> let f ~a : #a = ...

But then I get a syntax error for the next argument:

  let f1 ~a : #a ~b : #b = ...

Hmm.. try adding parens as usual for type constraints:

  let f1 (~a : #a) (~b : #b) = ...

but still a syntax error.

Move the tilde outside,

  let f1 ~(a : #a) ~(b : #b) = ..

This is syntactically valid, but doesn't give the most flexible typing.

Finally, last try:

  let f1 ~a:(#a as a) ~b:(#b as b) = ...

produces the correct type:

  val f1 : a:#a -> b:#b -> ...

It would have been nice if I could achieve that with just a ~ like this:

  let f1 ~a:#a -> ~b:#b


Patrick

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Polymorphic variant abbreviations
  2001-09-19 12:45 ` Jacques Garrigue
@ 2001-09-19 16:58   ` Patrick M Doane
  0 siblings, 0 replies; 8+ messages in thread
From: Patrick M Doane @ 2001-09-19 16:58 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list

On Wed, 19 Sep 2001, Jacques Garrigue wrote:

> > I have some code that would like to use polymorphic type abbreviations
> > along with labels.  It seems to be not behaving quite like I expected.
> > What could I do to make this shorter:
> > 
> >   type a = [ `A ]
> > 
> >   let f ~a:(#a as a) = ...
> 
> Nothing. But this doesn't strike me as a very frequent idiom.

I'll motivate how I came to using this feature.

I am working with a protocol that sends messages containing data as typed
s-expressions.  A natural representation with standard variants would be:

type data =
  | List of data list
  | Int of int
  | String of string
  (* there are about a dozen or so types in reality *)

There is a limited set of messages that can be sent and they have specific
type constraints on their data.  My hope was to express many of these
constraints using polymorphic variants and then check the rest at runtime.
Without polymorphic variants, I'm forced to do all checks at runtime.

So, I start with a function like this:

  let msg1 arg1 arg2 =
    { name = "msg1";
      data = `List [arg1; arg2] }

Now, the protocol defines type constraints for the arguments to all
messages. So, I create some type abbreviation for each kind of argument
that is used:

type arg1 = [ `String of string ]
type arg2 = [ `Int ]

So now I would like to require that the arguments to msg1 have the correct
type: 

  (* this doesn't work - types are too constrained *)
  let msg1 (arg1 : #arg1) (arg2 : #arg2) = ...


  (* need to use the 'as' form like this to get better typing *)
  let msg1 (#arg1 as arg1) (#arg2 as arg2) = ...

And finally, I want the message arguments to be labeled by their name.
Hence:

  let msg1 ~arg1:(#arg1 as arg1) ~arg2:(#arg2 as arg2) = ...

Maybe I'm trying to push the system too far. I found this methodology
worked pretty well until I came to lists. I have not been able to restrict
the elements of a list to be a specific type.

> >   let f ~a:#a = ...
> > 
> > but then it says that 'a' is unbound.
> 
> Sure, nothing binds a here: ~a: is a label, and #a an abbreviation for
> `A.

Well, '~a' implies the binding '~a:a'. I don't see why it is unreasonable
to assume a similar case applies with patterns.

> > I also find that the error messages are not as useful as they could be
> > with the type abbreviations. E.g:
> > 
> >   This expression has type ... but is here used with #a as 'a = 'a
> 
> Could you give me the code leading to this error message?
> This is clearly a bug.

This seems to happen in all cases I have tried:

# type t = [`A];;
type t = [ `A]
# let f1 (x:#t) = x;;
val f1 : (#t as 'a) -> 'a = <fun>
# let f2 (#t as x) = x;;
val f2 : #t -> [> `A] = <fun>
# let f3 ~x:(#t as x) = x;;
val f3 : x:#t -> [> `A] = <fun>
# f1 `B;;
Characters 3-5:
This expression has type [> `B] but is here used with type #t as 'a = 'a
# f2 `B;;
Characters 3-5:
This expression has type [> `B] but is here used with type #t as 'a = 'a
# f3 `B;;
Characters 3-5:
This expression has type [> `B] but is here used with type #t as 'a = 'a
# 

Thanks very much for everyone's help!

Patrick

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2001-09-19 16:59 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-18 22:48 [Caml-list] Polymorphic variant abbreviations Patrick M Doane
2001-09-19  0:05 ` Patrick M Doane
2001-09-19  8:37   ` Laurent Vibert
2001-09-19  8:56   ` Remi VANICAT
2001-09-19  8:01 ` Laurent Vibert
2001-09-19 16:35   ` Patrick M Doane
2001-09-19 12:45 ` Jacques Garrigue
2001-09-19 16:58   ` Patrick M Doane

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).