caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Keiko Nakata <keiko@kurims.kyoto-u.ac.jp>
To: caml-list@inria.fr
Subject: private rows question
Date: Thu, 10 Nov 2005 12:50:00 +0900 (JST)	[thread overview]
Message-ID: <20051110.125000.68545682.keiko@kurims.kyoto-u.ac.jp> (raw)

Hello.

Why the following functor G is not typable?
I thought that g could have a type something like int -> [< `A | `B ].

module G(X:sig type t = private [< `A ] val f : int -> t end) = struct
  let g x = if x = 0 then `B else X.f x 
end

This question comes from a wish to reuse my program codes 
using private rows in the following way:
(In my actual program, each branches of the function g of F 
performs much more verbose task. Thus, writing duplicate codes 
is a bit painful.)


module F(X:sig 
  type t = private [< `A | `B | `C] 
  val f : [`III of string] -> t   end) =
  struct
    let rec g binds = function
	[] -> []
      | `Var name :: tl -> (List.assoc name binds) :: (g binds tl)
      | (`I name) :: tl -> `A :: (g ((name, `A) :: binds) tl)
      | (`II name) :: tl -> `B :: (g ((name, `B) :: binds ) tl)
      | (`III _ as iii) :: tl  -> 
	  let iii' = X.f iii in
	  iii' :: (g ((name, iii') :: binds) tl)
  end

module ABC = struct 
  type t = [`A | `B | `C]
  let f = function `III name -> `C
end


module AB = struct
  type t = [`A | `B]
  let f = function `III name -> `A
end

module FABC = F(ABC)
module FAB = F(AB)

let abc : [`I | `II | `III] list -> [`A | `B | `C] list = FABC.g
      
let ab : [`I | `II | `III] list -> [`A | `B ] list = FBC.g


I thought that the reuse might be possible 
by making the function g take the function f as an argument, as in follows:


let rec g f binds = function
    [] -> []
  | `Var name :: tl -> (List.assoc name binds) :: (g f binds tl)
  | (`I name) :: tl -> `A :: (g f ((name, `A) :: binds) tl)
  | (`II name) :: tl -> `B :: (g f ((name, `B) :: binds ) tl)
  | (`III name as iii) :: tl  -> 
      let iii' = f iii in
      iii' :: (g f ((name, iii') :: binds) tl)

let f = function `III name -> `C
let abc binds ls  : [ `A | `B | `C]  = g f binds ls

However, this attempt also failed...
(Anyway, I do not find the addition of f to g's arguments pleasant,
since this could increase the number of g's formal parameters 
unnecessarily when I define more complicated functions.)



Best regards,
Keiko NAKATA


             reply	other threads:[~2005-11-10  3:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-10  3:50 Keiko Nakata [this message]
2005-11-11  5:12 ` [Caml-list] " Jacques Garrigue
2005-11-11  5:42   ` Keiko Nakata
2005-11-11  6:27     ` [Caml-list] again " Keiko Nakata

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=20051110.125000.68545682.keiko@kurims.kyoto-u.ac.jp \
    --to=keiko@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    /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).