caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: peterfrey <pjfrey@sympatico.ca>
To: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: [Caml-list] Repacking modules gives unexpected results
Date: Tue, 12 May 2015 17:47:12 -0400	[thread overview]
Message-ID: <BLU436-SMTP3178025F8A7ABA7A18CA4BA3DA0@phx.gbl> (raw)

(* I am running into a very strange problem with re-packed modules:
   re-packed instances of variables take on their new values, but internal
   functions that depend on those values are still tied to the initial 
values.

   module Make is initialized to buf = "foo" and len = (Bytes.length buf)

   re_pack substitutes buf = "aBar" and len = 4
   gives the result:

E.show():len:3    buf:foo    E.buf:foo    E.len:3
E.show():len:3    buf:foo    E.buf:aBar    E.len:4

   i.e.: the data reported by show() is not changed.
   E.buf and E.len reflect the results of re_pack
   The second line above SHOULD be as follows:

E.show():len:4    buf:aBar    E.buf:aBar    E.len:4

   In fact it is, IFF the line
       let show() = sprintf"len:%i\tbuf:%s" len buf
   is also included int the repacking.

   Perhaps I am trying to do something that is not supported.
*)

open Printf

module type ELT = sig
   val buf : bytes
   val len : int
   val show : unit -> string
end

module Make ( Data: sig val initial : string end) : ELT = struct
   let buf  = Data.initial
   let len  = (Bytes.length Data.initial)
   let show() = sprintf"len:%i\tbuf:%s" len buf
end

module Reg = struct
   let show (module E:ELT) =
   sprintf"E.show():%s\tE.buf:%s\tE.len:%i\n" (E.show()) E.buf E.len

   let re_pack env =
     (module struct include (val env:ELT) let buf = "aBar"
                                          let len = 4
       (* let show() = sprintf"len:%i\tbuf:%s" len buf *)
       (* with this it will 'work' ... *)
     end : ELT )
end

let byte_source_of_string initial =
   (module struct include Make(struct let initial = initial end) end : 
ELT )
;;
open Reg;;
let s = byte_source_of_string "foo";;
print_endline (show s);;
let s' = re_pack s;;
print_endline (show s');;

(*  show s' prints 0 - 2 demonstrating that ofs=2 and E.show() = 0 *)

Peter Frey


             reply	other threads:[~2015-05-12 21:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-12 21:47 peterfrey [this message]
2015-05-12 22:36 ` Jeremy Yallop
2015-05-15 22:28 ` peterfrey

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=BLU436-SMTP3178025F8A7ABA7A18CA4BA3DA0@phx.gbl \
    --to=pjfrey@sympatico.ca \
    --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).