Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Typing trouble with PXP
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Gerd Stolpmann <info@g...>
Subject: Re: [Caml-list] Typing trouble with PXP
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