Version française
Home     About     Download     Resources     Contact us    
Browse thread
killing a cat
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Warren Harris <warren@m...>
Subject: killing a cat

--Apple-Mail-2--73796428
Content-Type: text/plain;
	charset=ISO-8859-1;
	format=flowed;
	delsp=yes
Content-Transfer-Encoding: quoted-printable

I've encountered a complicated issue that I'm wondering if anyone can =20=

shed some light on here. It relates to polymorphic types what I like =20
to call Schr=F6dinger types (sorry, I don't know the real name for =20
these, but they're the dreaded polymorphic variables with underscores =20=

that, like Schr=F6dinger's cat, are undefined until you use them for the =
=20
first time). I'll try to distill it down to its simplest form here, =20
but the issue arises from desiring to implement something based on =20
"type-indexed values" such as described here: =
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=3D10.1.1.53.1977

In its simplest form, I have a writer type and a dispatching handler =20
like this:

type ('a, 'b) writer =3D ((unit -> 'a) -> 'b)
type ('a, 'b) handler =3D { handle : 'a -> (string -> 'a -> 'b, 'b) =20
writer }

Then I wish to define a family of handlers that ultimately call the =20
writer's continuation. In the very simplest form, a handler might be =20
expressed as:

let f =3D { handle =3D fun a k -> write_int_field "f" a k }

which would have type "(int, 'a) handler" as desired. (In the real =20
implementation, there will be more than one handler, hence the need =20
for the record.) However, I wish to define a family of handlers that =20
all share common implementation, so I define a helper function (again, =20=

vastly simplified):

let helper name wf =3D { handle =3D fun a k -> wf name a k }
let f =3D helper "f" write_int_field

However, here the type of "f" is now "('int, '_a) handler" and fails =20
to unify with the desired type expressed in the interface.

Now, I could get around this by making "f" be a function that when =20
evaluated returns the result of the helper. However, I'm trying to =20
avoid that because in the real implementation, the work of the helper =20=

is somewhat expensive and should only be performed once. An alternate =20=

solution can be achieved by pushing the universal qualifier for the =20
result type down into the handler:

type 'a handler =3D { handle : 'b . 'a -> (string -> 'a -> 'b, 'b) =20
writer }

However, this doesn't allow me to define the helper function as before:

   let helper name wf =3D { handle =3D fun a k -> wf name a k };;
                                   ^^^^^^^^^^^^^^^^^^^^^^
This field value has type 'a -> (string -> 'a -> 'b, 'b) writer
which is less general than 'c. 'd -> (string -> 'd -> 'c, 'c) writer

I am forced into reworking the helper parameter function, wf, to also =20=

become a record in order for it to be universally qualified:

type 'a field_handler =3D { write_field : 'b . string -> 'a -> (unit -> =20=

string -> 'a -> 'b) -> 'b }

This works:

let helper name wf =3D { handle =3D fun a k -> wf.write_field name a k }
let f =3D helper "f" write_int_field

Here "f" has type "int handler" as desired, although this solution =20
seems somewhat round-about, and was difficult to arrive at. Can anyone =20=

suggest another approach that prevents the dreaded underscores? (Note =20=

that using objects instead of records exhibits the exact same behavior.)

Warren Harris
Metaweb Technologies




--Apple-Mail-2--73796428
Content-Disposition: attachment;
	filename=smime.p7s
Content-Type: application/pkcs7-signature;
	name=smime.p7s
Content-Transfer-Encoding: base64

MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIIKlzCCBREw
ggP5oAMCAQICAQEwDQYJKoZIhvcNAQEEBQAwgb8xCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxp
Zm9ybmlhMRYwFAYDVQQHEw1TYW4gRnJhbmNpc2NvMSMwIQYDVQQKExpNZXRhd2ViIFRlY2hub2xv
Z2llcywgSW5jLjEbMBkGA1UECxMSTWV0YXdlYiBPcGVyYXRpb25zMSEwHwYDVQQDExhNZXRhd2Vi
IFJvb3QgQ2VydGlmaWNhdGUxHjAcBgkqhkiG9w0BCQEWD29wc0BtZXRhd2ViLmNvbTAeFw0wNjEw
MTcxNjAyNTRaFw0xNjEwMTQxNjAyNTRaMIGrMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv
cm5pYTEjMCEGA1UEChMaTWV0YXdlYiBUZWNobm9sb2dpZXMsIEluYy4xGzAZBgNVBAsTEk1ldGF3
ZWIgT3BlcmF0aW9uczElMCMGA1UEAxMcTWV0YXdlYiBW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