caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Stéphane Glondu" <steph@glondu.net>
To: Dario Teixeira <darioteixeira@yahoo.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Structural subtyping problem
Date: Sun, 28 Mar 2010 19:32:37 +0200	[thread overview]
Message-ID: <4BAF92B5.1060502@glondu.net> (raw)
In-Reply-To: <61102.44294.qm@web111503.mail.gq1.yahoo.com>

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


  parent reply	other threads:[~2010-03-28 17:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-28 16:38 Dario Teixeira
2010-03-28 17:03 ` [Caml-list] " Vincent Aravantinos
2010-03-28 17:07 ` Andreas Rossberg
2010-03-28 17:32 ` Stéphane Glondu [this message]
2010-03-28 19:20   ` Christophe TROESTLER
2010-03-28 20:17 ` Dario Teixeira

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4BAF92B5.1060502@glondu.net \
    --to=steph@glondu.net \
    --cc=caml-list@yquem.inria.fr \
    --cc=darioteixeira@yahoo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).