Browse thread
Structural subtyping problem
-
Dario Teixeira
- Vincent Aravantinos
- Andreas Rossberg
- Stéphane Glondu
- Dario Teixeira
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Stéphane Glondu <steph@g...> |
| Subject: | Re: [Caml-list] Structural subtyping problem |
Dario Teixeira a écrit :
> A simple nonsensical example that illustrates the problem is listed below; the
> type-checking error occurs in function "step1", where the optional parameter "story"
> is used as an object of type "< title:string; .. >". In function "step3", this
> parameter "story" is actually instantiated with objects of type "< title:string >"
> and "< title:string; count:int >".
As said elsewhere, this is because you are trying to do polymorphic
recursion. Standard tricks work here:
------------------------------------------------------------------------
type steps = {
step1 : 'a. ?story:(<title : string; ..> as 'a) -> unit -> bool;
step2 : string -> bool;
step3 : 'a. story:(<title : string; count : int; ..> as 'a) -> bool
}
let rec steps = {
step1 =
begin fun ?story () ->
match story with
| Some s -> steps.step2 s#title
| None -> steps.step2 "title1"
end;
step2 =
begin fun title ->
let story = object
method title = title
method count = 0
end in
steps.step3 story
end;
step3 =
begin fun ~story ->
match story#count with
| 0 ->
steps.step1 ~story ()
| 1 ->
let story = object
method title = "title2"
end in
steps.step1 ~story ()
| _ ->
true
end
}
let step1 = steps.step1
let step2 = steps.step2
let step3 = steps.step3
------------------------------------------------------------------------
With OCaml 3.12, I guess this should be feasible without using the
intermediate "steps" record.
Cheers,
--
Stéphane