Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Artem Prisyaznuk <tema@sit.kiev.ua>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Typing trouble with PXP
Date: Wed, 3 Apr 2002 22:59:11 +0200	[thread overview]
Message-ID: <20020403225911.Q4820@ice.gerd-stolpmann.de> (raw)
In-Reply-To: <20020403130056.GA3524@sit.kiev.ua>; from tema@sit.kiev.ua on Wed, Apr 03, 2002 at 15:00:56 +0200

Hi,
On 2002.04.03 15:00 Artem Prisyaznuk wrote:
> 
> The problem is summarized in the following code:
> 
> 	open Pxp_yacc;;
> 
> 	let rec foo_fun node=
> 		node#write (`Out_channel stderr) `Enc_koi8r;;
> 	let  _ =
> 		let doc = parse_wfdocument_entity default_config (from_file "my.xml") default_spec in
> 		let root = doc#root in
> 		begin
> 				root#write (`Out_channel stderr) `Enc_koi8r;
> 				foo_fun root    
> 		end;;
> 
> This example doesn't compile and the compiler error message is:
> 
> 	File "tmp.ml", line 10, characters 11-15:
> 	This expression has type
> 	  (('a Pxp_document.node as 'b) Pxp_document.extension as 'a)
> 	  Pxp_document.node =
> 		< add_node : ?force:bool -> 'b -> unit;
> 		  ........skip........
> 		  write : ?prefixes:string list ->
> 				  ?default:string ->
> 				  Pxp_types.output_stream -> Pxp_types.encoding -> unit >
> 	but is here used with type
> 	  < add_node : ?force:bool -> 'b -> unit;
> 		..........skip.............
> 		write : [> `Out_channel of out_channel] -> [> `Enc_koi8r] -> 'c >
> 

The missing optional arguments cause the type incompatibility. The "node" argument
of the "foo_fun" function gets a type that only depends on its usage, and I guess
it is

< write : [> `Out_channel of out_channel] -> [> `Enc_koi8r] -> 'c; ..> -> 'c

On the other hand, the type of the variable "root" depends on the result of
parse_wfdocument_entity, and thus is completely known. The method "write"
is typed _with_ the two optional arguments, but "foo_fun" is not.

The short form of this incompatibility is shown in the following example:

# let write1 ?x y = ();;
val write1 : ?x:'a -> 'b -> unit = <fun>
# let write2 y = ();;
val write2 : 'a -> unit = <fun>
# let do_something (write,x) = write x;;
val do_something : ('a -> 'b) * 'a -> 'b = <fun>
# do_something (write2,5);;
- : unit = ()
# do_something (write1,5);;
This expression has type (?x:'a -> 'b -> unit) * int
but is here used with type ('c -> 'd) * 'c

(The really interesting thing is that if I define do_something as
  # let do_something write x = write x;;
  val do_something : ('a -> 'b) -> 'a -> 'b = <fun>
_both_ applications are allowed, i.e. both expressions
do_something write1 5 and do_something write2 5 can be typed. I suppose
this case is handled by the type checker in a special way to simplify
the usage of labels.)

The solution: Change foo_fun such that there are the missing optional
arguments. You could do it by writing

let rec foo_fun node=
  node#write ?prefixes:None ?default:None (`Out_channel stderr) `Enc_koi8r;;

or, better, by

let rec foo_fun (node : 'ext Pxp_document.node) =
  node#write (`Out_channel stderr) `Enc_koi8r;;

I hope this helps,

Gerd
-- 
----------------------------------------------------------------------------
Gerd Stolpmann      Telefon: +49 6151 997705 (privat)
Viktoriastr. 45             
64293 Darmstadt     EMail:   gerd@gerd-stolpmann.de
Germany                     
----------------------------------------------------------------------------
-------------------
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


      reply	other threads:[~2002-04-03 20:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-04-03 13:00 Artem Prisyaznuk
2002-04-03 20:59 ` Gerd Stolpmann [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020403225911.Q4820@ice.gerd-stolpmann.de \
    --to=info@gerd-stolpmann.de \
    --cc=caml-list@inria.fr \
    --cc=tema@sit.kiev.ua \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox