Loading ...
Sorry, an error occurred while loading the content.

How to hide a parametrized type ?

Expand Messages
  • Sébastien Dailly
    Hello, It is possible to declare a parametrized type, but hiding how the type is parametrized in the signature ? Here is a exemple of I want declare : module
    Message 1 of 3 , Mar 3, 2013
    • 0 Attachment
      Hello,

      It is possible to declare a parametrized type, but hiding how the type is parametrized in the signature ?

      Here is a exemple of I want declare :

      module type SIG =
      sig
      type t
      type group =
      | G1
      end

      module Impl:SIG =
      struct
      type t
      type group =
      | G1 of int
      end

      But this does not compile with the following error :

      File "test.ml", line 9, characters 2-61:
      Error: Signature mismatch:
      Modules do not match:
      sig type t type group = G1 of int end
      is not included in
      SIG
      Type declarations do not match:
      type group = G1 of int
      is not included in
      type group = G1
      File "test.ml", line 4, characters 9-27: Expected declaration
      File "test.ml", line 11, characters 9-34: Actual declaration
      The arities for field G1 differ.

      Thanks !
    • Lukasz Stafiniak
      On Sun, Mar 3, 2013 at 1:28 PM, Sébastien Dailly
      Message 2 of 3 , Mar 3, 2013
      • 0 Attachment
        On Sun, Mar 3, 2013 at 1:28 PM, S�bastien Dailly <
        sebastien-ocaml@...> wrote:

        > **
        >
        >
        > Hello,
        >
        > It is possible to declare a parametrized type, but hiding how the type is
        > parametrized in the signature ?
        >
        > Here is a exemple of I want declare :
        >
        > module type SIG =
        > sig
        > type t
        > type group =
        > | G1
        > end
        >
        > module Impl:SIG =
        > struct
        > type t
        > type group =
        > | G1 of int
        > end
        >
        >
        This is not possible since you define "G1" as no-argument variant in SIG
        which you don't want. But it is possible to have private variant types,
        where the programmer can see what the variants (and their arguments) are
        but cannot build them.
        http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual021.html#toc76


        > But this does not compile with the following error :
        >
        > File "test.ml", line 9, characters 2-61:
        > Error: Signature mismatch:
        > Modules do not match:
        > sig type t type group = G1 of int end
        > is not included in
        > SIG
        > Type declarations do not match:
        > type group = G1 of int
        > is not included in
        > type group = G1
        > File "test.ml", line 4, characters 9-27: Expected declaration
        > File "test.ml", line 11, characters 9-34: Actual declaration
        > The arities for field G1 differ.
        >
        > Thanks !
        >
        >


        [Non-text portions of this message have been removed]
      • Sébastien Dailly
        ... Thanks for pointing private type; this is not exactly what I need, however I could get what I want with an abstract type : module type SIG = sig type t
        Message 3 of 3 , Mar 3, 2013
        • 0 Attachment
          Le 03/03/2013 14:14, Lukasz Stafiniak a écrit :
          > This is not possible since you define "G1" as no-argument variant in SIG
          > which you don't want. But it is possible to have private variant types,
          > where the programmer can see what the variants (and their arguments) are
          > but cannot build them.
          > http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual021.html#toc76

          Thanks for pointing private type; this is not exactly what I need, however I could get what I want with an abstract type :

          module type SIG =
          sig
          type t
          type g1
          type group =
          | G1 of g1
          end

          module Impl:SIG =
          struct
          type t
          type g1 = int
          type group =
          | G1 of g1
          end

          but I'm not complety satisfated : this means that I have a given signature, I can't implement it by adding some type without changing the type itself, and this add unecessary type, just for syntaxic sugar… I'll keep it for now…
        Your message has been successfully submitted and would be delivered to recipients shortly.