caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ivan Gotovchits <ivg@ieee.org>
To: Serge Sivkov <ssp.mryau@gmail.com>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] using module with types as record parameter
Date: Tue, 28 Nov 2017 13:09:48 -0500	[thread overview]
Message-ID: <CALdWJ+x4pJNaYtRAwuZACGmFN0XOr5GPTWwpVB-Gc7-ssVWk6w@mail.gmail.com> (raw)
In-Reply-To: <CAOUGqWxJ1NmSf_SExVh5gPJOA4BGN44eaZwMqgnFL3egODE3pg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2256 bytes --]

Let me first explain what is wrong with your code.

The message type in `Cmdlface` is defined as an abstract type with only one
operation `cmd : message -> unit`. The module `Cmd` provides a possible
implementation for this abstract type.
The `R.cmd 1` expression tries to break the abstraction by assuming that
the `R` implementation is using the `int` type as an underlying
implementation. Imagine, what will happen if the `Net.cvt` field contained
another implementation,
for example

    module Cmd = struct
      type message = string
      let cmd = print_endline
    end

    let i = { Net.cvt = (module Cmd: CmdIface) }
    Net.net i

This will finally lead to a call `print_endline 1` if the type system won't
stop you before.


The solution is either to uncover the abstractions and stick to `int` as a
concrete representation of the message abstraction or to extend the message
abstraction interface with functions that will allow you to create the
messages, e.g.,


    module type CmdIface = sig
       type message
       val of_int : int -> message
       val cmd : message -> unit
    end





On Tue, Nov 28, 2017 at 12:55 PM, Serge Sivkov <ssp.mryau@gmail.com> wrote:

> Hello,
>
> is there way to fix the following code:
>
> module type CmdIface = sig
>         type message
>         val cmd : message -> unit
> end
>
> module type NetIface = sig
>         type init
>         val net : init -> init
> end
>
> module Cmd = struct
>         type message = int
>         let cmd v = v+1
> end
>
> module Net = struct
>         type init = { cvt: (module CmdIface) }
>
>         let net init =
>                 let cvt = init.cvt in
>                 let module R = (val cvt: CmdIface) in
>                 R.cmd 1 (* line 22 with error *)
> end
>
> let i = { Net.cvt = (module Cmd: CmdIface) }
> Net.net i
>
> I got error:
> File "t.ml", line 22, characters 22-23:
> Error: This expression has type int but an expression was expected of type
>          R.message
>
> In case I do not use types in function signature code works well:
> module type I = sig val f : int -> int end;;
> module M = struct
> let f v = v+1
> end;;
> type t = {cvt: (module M); v: int};;
> let module R = (val i.cvt: I) in R.f i.v;;
> - : int = 1
>
> WBR, ssp
>

[-- Attachment #2: Type: text/html, Size: 3966 bytes --]

  reply	other threads:[~2017-11-28 18:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-28 17:55 Serge Sivkov
2017-11-28 18:09 ` Ivan Gotovchits [this message]
2017-11-28 18:19   ` Mikhail Mandrykin

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=CALdWJ+x4pJNaYtRAwuZACGmFN0XOr5GPTWwpVB-Gc7-ssVWk6w@mail.gmail.com \
    --to=ivg@ieee.org \
    --cc=caml-list@inria.fr \
    --cc=ssp.mryau@gmail.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).