From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id C50AE82355 for ; Tue, 28 Nov 2017 19:09:51 +0100 (CET) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=ivg@ieee.org; spf=Pass smtp.mailfrom=ivg@ieee.org; spf=None smtp.helo=postmaster@mail-qt0-f173.google.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of ivg@ieee.org) identity=pra; client-ip=209.85.216.173; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="ivg@ieee.org"; x-sender="ivg@ieee.org"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of ivg@ieee.org designates 209.85.216.173 as permitted sender) identity=mailfrom; client-ip=209.85.216.173; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="ivg@ieee.org"; x-sender="ivg@ieee.org"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-qt0-f173.google.com) identity=helo; client-ip=209.85.216.173; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="ivg@ieee.org"; x-sender="postmaster@mail-qt0-f173.google.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3A6OvDaBJxMblA+VSjF9mcpTZWNBhigK39O0sv0rFi?= =?us-ascii?q?tYgXI/zxwZ3uMQTl6Ol3ixeRBMOAtKIC1rKempujcFJDyK7JiGoFfp1IWk1Nou?= =?us-ascii?q?QttCtkPvS4D1bmJuXhdS0wEZcKflZk+3amLRodQ56mNBX660e/5j8KGxj5KRE9?= =?us-ascii?q?ZqGsQtaT3IyL0LWJ9ofcbk1zhSS+Zq06eA6ttUPUrNgfnKNtL68wzl3CpX4eKM?= =?us-ascii?q?pMwmY9BEyamV7T4du34pVj8jhL86Yg6cFoUKj3cuI/V7MOX2duCHw8+MC+7UqL?= =?us-ascii?q?dgCI/HZJFzxOyhc=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0CLAQCDpR1ahq3YVdFdHQEBBQELAYJKg?= =?us-ascii?q?VhuJweDeIE2mAKBfYhojzwDXAobhSAChH8HQxQBAQEBAQEBAQEBEgEBAQgLCwg?= =?us-ascii?q?oL4I4BQEeAQWCPgECAgEjBBkBATcBBAsJAgsDNAICIQESAQUBHAYTG4lvAwgFC?= =?us-ascii?q?IomkRtAiyJugW06gwkBAQWEIw2DJgEBAQEBBQEBAQEBARoIEoMqggmBVYUUgmu?= =?us-ascii?q?FSYJjikaITI5/PYdziCGEeYQTjzyNMk2ILBQFH4EWNoFzfAg6MgaBcQmCOh+CE?= =?us-ascii?q?SM2AYkLgVgBAQE?= X-IPAS-Result: =?us-ascii?q?A0CLAQCDpR1ahq3YVdFdHQEBBQELAYJKgVhuJweDeIE2mAK?= =?us-ascii?q?BfYhojzwDXAobhSAChH8HQxQBAQEBAQEBAQEBEgEBAQgLCwgoL4I4BQEeAQWCP?= =?us-ascii?q?gECAgEjBBkBATcBBAsJAgsDNAICIQESAQUBHAYTG4lvAwgFCIomkRtAiyJugW0?= =?us-ascii?q?6gwkBAQWEIw2DJgEBAQEBBQEBAQEBARoIEoMqggmBVYUUgmuFSYJjikaITI5/P?= =?us-ascii?q?YdziCGEeYQTjzyNMk2ILBQFH4EWNoFzfAg6MgaBcQmCOh+CESM2AYkLgVgBAQE?= X-IronPort-AV: E=Sophos;i="5.44,468,1505772000"; d="scan'208,217";a="246422362" Received: from mail-qt0-f173.google.com ([209.85.216.173]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 28 Nov 2017 19:09:50 +0100 Received: by mail-qt0-f173.google.com with SMTP id u10so1009108qtg.2 for ; Tue, 28 Nov 2017 10:09:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ieee-org.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=hH9QtQ0we2KfoiEM2lr8gRyyX8Sb4Hho6mz9VV8qUnM=; b=oau8HYFAFRFjpuXQnboY2B+GZwJn8CtC6KSjYmZTfv/CqDPVAToTrwgIxr9ymWLlJV 9e7DBebbDjwDM7rVP5rF3Kx9tPwvA/IceoDsQmy/ZlhmLyVIK62JmTSAvbG12SwDjcXt SKHYPTG10qHhmOZupgdKaAUDD6GzBgCsheP0uF4vsrvx3upackT2Srq0rnfpdpKojdr/ tckbZOs/Il6hAtz+dU4gBbN5peNulpFnvOO+TG9vHih6ptz6cM6cR3/OLJ6gDUY3687g Nzm+UEZp6v/ftfOHMeUr7Aq6ZUDiHXhK7t7+waXXOJU3jpNUalqm5X7nmDJtJiTIjAd6 b+qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=hH9QtQ0we2KfoiEM2lr8gRyyX8Sb4Hho6mz9VV8qUnM=; b=PnsISYR2iOoU8z5wZ1cG3dDNOhoslzvC4aToYfN3HosEOzmIkQ3j+S/4kZY1m0F9lz Q+FZKPbQeSPBCCuxpKCUGcIxPOOSAg18b8RkI+j9Gb5VeR2wf1JqgBodZRbU+FhKHz39 h+6ZHQihOHrkEmTFAmaiaYr4I0urjaUEYbPPAg4x6NoVf6ZY1xfoBJolaTgGIDtzeoIY a6eq2byjb++TprCqgK2RBGFH4OFt2A/4FEUtMDNOVIe7vcLNvk0gWQ1y6rjMjHOy2eAs gajwc2WZ9R9LpfW6u2QrSzjetvMNHlWUspp4p+PcANuFlSUTl5bqDGIGGDYE0IHbomPm RTOg== X-Gm-Message-State: AJaThX6/aAPdVMa4omiDdzVzZ6wXMPntl+g1/4zZeHykCAvg4r5vIfr7 fEYtjmcmx0XJPhhVv0oIhewVEu3bbt0nYucA77xZHA== X-Google-Smtp-Source: AGs4zMaIUSqAdJ5yyczQ4nT3Bk2uRbkAkDP8fsvu5bh+BKsAsRiV3fnC7HQzhmfjKpKUGefYrmPxorBBJBpJ0IXmqCA= X-Received: by 10.200.19.129 with SMTP id h1mr67771430qtj.233.1511892589231; Tue, 28 Nov 2017 10:09:49 -0800 (PST) MIME-Version: 1.0 Received: by 10.12.210.49 with HTTP; Tue, 28 Nov 2017 10:09:48 -0800 (PST) In-Reply-To: References: From: Ivan Gotovchits Date: Tue, 28 Nov 2017 13:09:48 -0500 Message-ID: To: Serge Sivkov Cc: OCaml Mailing List Content-Type: multipart/alternative; boundary="089e08268f4cb87cdb055f0eecee" Subject: Re: [Caml-list] using module with types as record parameter --089e08268f4cb87cdb055f0eecee Content-Type: text/plain; charset="UTF-8" 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 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 > --089e08268f4cb87cdb055f0eecee Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Let me first explain what is wrong with your code.=C2= =A0

The message type in `Cmdlface` is defined as a= n abstract type with only one operation `cmd : message -> unit`. The mod= ule `Cmd` provides a possible implementation for this abstract type.=C2=A0<= /div>
The `R.cmd 1` expression tries to break the abstraction by assumi= ng that the `R` implementation is using the `int` type as an underlying imp= lementation. Imagine, what will happen if the `Net.cvt` field contained ano= ther implementation,=C2=A0
for example=C2=A0

=
=C2=A0 =C2=A0 module Cmd =3D struct=C2=A0=C2=A0
=C2=A0 =C2= =A0 =C2=A0 type message =3D string=C2=A0
=C2=A0 =C2=A0 =C2=A0 let= cmd =3D print_endline
=C2=A0 =C2=A0 end=C2=A0

=C2=A0 =C2=A0 let i =3D { Net.cvt =3D (module Cmd: CmdIface) }
=
=C2=A0 =C2=A0 Net.net i

This will finally lead to a = call `print_endline 1` if the type system won't stop you before.=C2=A0<= /div>


The solution is either to uncover t= he abstractions and stick to `int` as a concrete representation of the mess= age abstraction=C2=A0or to extend the message abstraction interface with fu= nctions that will allow you to create the messages, e.g.,=C2=A0
<= br>

=C2=A0 =C2=A0 module type CmdIface =3D sig=C2= =A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0type message
=C2=A0 =C2= =A0 =C2=A0 =C2=A0val=C2=A0of_int : int -> message
=C2=A0 =C2= =A0 =C2=A0 =C2=A0val cmd : message -> unit
=C2=A0 =C2=A0 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 =3D sig
=C2=A0 =C2=A0 =C2=A0 =C2=A0 type mes= sage
=C2=A0 =C2=A0 =C2=A0 =C2=A0 val cmd : message -> unit
end

module type NetIface =3D sig
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 type init
=C2=A0 =C2=A0 =C2=A0 =C2=A0= val net : init -> init
end

module Cm= d =3D struct
=C2=A0 =C2=A0 =C2=A0 =C2=A0 type message =3D int
=C2=A0 =C2=A0 =C2=A0 =C2=A0 let cmd v =3D v+1
end

module Net =3D struct
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 type init =3D { cvt: (module CmdIface) }

=C2= =A0 =C2=A0 =C2=A0 =C2=A0 let net init =3D
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 let cvt =3D init.cvt in
=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 let module R =3D (val cvt= : CmdIface) in=C2=A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 R.cmd 1 (* line 22 with error *)
end

=
let i =3D { Net.cvt =3D (module Cmd: CmdIface) }
Net.n= et i

I got error:
File "= t.ml", line 22, characte= rs 22-23:
Error: This expression has type int but an expression w= as expected of type
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0R.message

In case I do not use types in function signat= ure code works well:
module = type I =3D sig val f : int -> int end;;
module M =3D struct
let f v =3D v+1
end;;
type t = =3D {cvt: (module M); v: int};;
let module R =3D (val i.cvt: I) in R.f i= .v;;
- : int =3D 1
WBR, ssp

--089e08268f4cb87cdb055f0eecee--