[MLton] Extending the SML Basis library (Library project)
Matthew Fluet
fluet at cs.cornell.edu
Tue Oct 17 12:47:04 PDT 2006
>>>> type ('a, 'b) iso = ('a -> 'b) * ('b -> 'a)
>>>> type ('a, 'b) emb = ('a -> 'b) * ('b -> 'a option)
>>>
>>> Honestly, I'm not a huge fan of the iso/emb types and functions.
>
> I'm not sure what you mean here. Is it the specific form of the iso/emb
> types & values or something more fundamental?
I just don't find it intuitive to understand the meaning of:
#1 Int32.int
Whereas, I find it easy to understand the meaning of:
Int32.toInt
It is true that type inference will complain if you get things too wrong.
And, the isomorphism values may be very useful with combinators to build
up type-indexed values and other isomorphisms. I'm just worried that I
would be throwing in Iso.swap blindly to get the types to work out.
But, you seem to have some significant examples, so it's not a strong
complaint.
>>> I would revise the types to:
>>>
>>> type ('a, 'b) iso = {to: 'a -> 'b, from: 'b -> 'a}
>>> type ('a, 'b) emb = {to: 'a -> 'b, from: 'b -> 'a option}
>>>
>>> Then I can write:
>>>
>>> #to Char.int
>>>
>>> and have a reminder of the direction of the coercion.
>
> Note that direction is quite context dependent. When you supply an
> isomorphism to some function, you need to be aware of the direction.
Good point. And, that's partly my concern, that using isomorphisms are
very context dependent, requiring more context than a reader is likely to
have internalized when looking at new code.
>> [...] I'd recommend
>>
>> structure Iso:> ISO
>>
>> Here, for example, is the ISO signature that I quickly hacked up for
>> use in my recent stuff on type-indexed values.
>>
>> signature ISO = sig
>> type ('a, 'b) t
> [...]
>> val make: ('a -> 'b) * ('b -> 'a) -> ('a, 'b) t
> [...]
>> end
>
> The major problem (sorry for the repetition if you already spotted this
> from my earlier post) with this design (making Iso.t an abstract type) is
> that one can no longer make polymorphic isomorphisms (courtesy of the
> value restriction).
There is no harm in having:
signature ISO = sig
type ('a, 'b) t = ('a -> 'b) * ('b -> 'a)
...
val make : ('a -> 'b) * ('b -> 'a) -> ('a, 'b) t
end
structure Iso :> ISO
The opaque signature match is redundant, as the only type in the signature
has a transparent type definition; nonetheless, one has a 'home' structure
and a distinguished Iso.t type.
More information about the MLton
mailing list