caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Repacking modules gives unexpected results
@ 2015-05-12 21:47 peterfrey
  2015-05-12 22:36 ` Jeremy Yallop
  2015-05-15 22:28 ` peterfrey
  0 siblings, 2 replies; 3+ messages in thread
From: peterfrey @ 2015-05-12 21:47 UTC (permalink / raw)
  To: caml-list

(* 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-05-15 22:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-12 21:47 [Caml-list] Repacking modules gives unexpected results peterfrey
2015-05-12 22:36 ` Jeremy Yallop
2015-05-15 22:28 ` peterfrey

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).