* [Caml-list] Infer least upper bound for arguments to functions in rows
@ 2016-03-30 10:28 Christoph Höger
2016-03-31 10:41 ` Goswin von Brederlow
0 siblings, 1 reply; 2+ messages in thread
From: Christoph Höger @ 2016-03-30 10:28 UTC (permalink / raw)
To: caml users
Dear experts,
is there a way to infer a compatible type for the argument to t#x in the
function:
let a t = ignore (t#x t) ; t#x (object end) ;;
I can easily provide a compatible t, e.g.:
let t = object method x _ = 23 end
I can also use a coercion:
let a t = ignore (t#x (t :> < >)) ; t#x (object end) ;;
Since < > is the common supertype of both arguments.
My problem is that in general < > won't suffice for the common type, and
finding it is a rather complicated process. So I wonder if there is some
clever rewriting of the function that makes ocamlc infer that type for me?
Is there any expression that for two (expressions with) different input
types yields (an expression of) the largest common supertype?
regards,
Christoph
--
Christoph Höger
Technische Universität Berlin
Fakultät IV - Elektrotechnik und Informatik
Übersetzerbau und Programmiersprachen
Sekr. TEL12-2, Ernst-Reuter-Platz 7, 10587 Berlin
Tel.: +49 (30) 314-24890
E-Mail: christoph.hoeger@tu-berlin.de
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] Infer least upper bound for arguments to functions in rows
2016-03-30 10:28 [Caml-list] Infer least upper bound for arguments to functions in rows Christoph Höger
@ 2016-03-31 10:41 ` Goswin von Brederlow
0 siblings, 0 replies; 2+ messages in thread
From: Goswin von Brederlow @ 2016-03-31 10:41 UTC (permalink / raw)
To: caml-list
On Wed, Mar 30, 2016 at 12:28:07PM +0200, Christoph Höger wrote:
> Dear experts,
>
> is there a way to infer a compatible type for the argument to t#x in the
> function:
>
> let a t = ignore (t#x t) ; t#x (object end) ;;
>
> I can easily provide a compatible t, e.g.:
>
> let t = object method x _ = 23 end
>
> I can also use a coercion:
>
> let a t = ignore (t#x (t :> < >)) ; t#x (object end) ;;
>
> Since < > is the common supertype of both arguments.
>
> My problem is that in general < > won't suffice for the common type, and
> finding it is a rather complicated process.
I found that this is often a huge problem, esspecially for methods.
You can't always coerce the type and type inference won't infere
polymorphic methods (like t#x above). I've spend hours trying
different ways to not run into "self type may not escape it's class"
or "'a is unbound" errors. In the end I found a quite simple way:
For each class (type) define a method as_<name> that returns
(self :> name).
That way you function or method does not take a supertype of some long
winded, complex and possibly recursive class type but a simple
supertype of a class having the as_<name> method and nothing else, e.g.
method add_widget : 'a . (<as_widget : OWidget.oWidget; ..> as 'a) -> unit
Coming up with the right as_<name> is straight forward and the type
never gets longer than this no matter how complex classes are.
> So I wonder if there is some
> clever rewriting of the function that makes ocamlc infer that type for me?
>
> Is there any expression that for two (expressions with) different input
> types yields (an expression of) the largest common supertype?
>
> regards,
>
> Christoph
I don't think there is any way to get type inference to magically do
the right thing here. The inferecne of polymorphic methods simply
doesn't work that way.
MfG
Goswin
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2016-03-31 10:41 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-30 10:28 [Caml-list] Infer least upper bound for arguments to functions in rows Christoph Höger
2016-03-31 10:41 ` Goswin von Brederlow
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox