Re: Problem with information hiding.

"Hendrik Boom [email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>
On Fri, Oct 02, 2015 at 11:57:17PM +0200, Gabriel Scherer [email protected] [ocaml_beginners] wrote:
> Signatures in OCaml have two purpose: they check compatibility, and
> they enforce type abstract. They check, and they hide.
> When you write
> 
>   module Chargram : GRAMMAR = struct
>      ...
>   end
> 
> you are enforcing the signature of Chargram to be GRAMMAR, and in
> particular to have an *abstract* token type (this is a sealing
> signature ascription). You could fix the type error by not enforcing a
> signature there:
> 
>   module Chargram = struct
>     ...
>   end
> 
> This removes hiding. You can still check by doing it separately, with
> for example:
> 
>   let () = let module C : GRAMMAR = Chargram in ()
> 
> You can also use the GRAMMAR signature, but relaxed to have the
> equality (type token = char), so that you hide a bit but keep what you
> need:
> 
> module Chargram : (GRAMMAR with type token = char) = struct
>    ...
> end

This looks like what I wanted.

In the complete program, Chargram is passed as a parameter to a functor 
that takes a GRAMMAR, so the first solution you propose won't work in 
context.

Other modules of type GRAMMAR will have a different type for token, and 
will also be passed to this functor.

I couldn't just leave out the typing of Chargram as GRAMMAR. 

I tried the second approach, and it works fine in the complete 
program.  I needed to know how to relax the signature.  It now 
type-checks and runs correctly.

Thank you.

-- hendrik

> 
> Either change makes your example code type-check on my machine.
> 
> On Fri, Oct 2, 2015 at 11:49 PM, Hendrik Boom [email protected]
> [ocaml_beginners] <[email protected]> wrote:
> > On Wed, Sep 30, 2015 at 09:41:20PM +0200, Gabriel Scherer [email protected] [ocaml_beginners] wrote:
> >> Could you provide a self-contained piece of code that fails to compile? It
> >> is easier to debug such problems when we see the error source, and when it
> >> is possible to experiment (check that a fix suggestion work) before sending
> >> a reply. Note that you don't need to make it minimal.
> >
> > I made it minimal, and it no longer looks like what I wanted to do -- I
> > managed to preserve the error while removing an entire module.
> >
> > But it still fails in the sme way:
> >
> > open Stream
> >
> > module type GRAMMAR =
> >   sig
> >     type token
> >   end;;
> >
> >
> > module Chargram : GRAMMAR =
> >   struct
> >     type token = char;; (* Should really be a module's type parameter *)
> >   end;;
> >
> > let testx : Chargram.token Stream.t = Stream.of_string "str";;
> >
> >
> > hendrik@notlookedfor:~/dv/parse/priority$ ocamlc ko1.ml
> > File "ko1.ml", line 15, characters 38-60:
> > Error: This expression has type char Stream.t
> >        but an expression was expected of type Chargram.token Stream.t
> >        Type char is not compatible with type Chargram.token
> > hendrik@notlookedfor:~/dv/parse/priority$
> >
> >
> >
> > I want to use chargram in a way that requires that token = char.
> >
> > I also want to have other modules of type GRAMMAR with different  types
> > for token.
> >
> >
> > -- hendrik
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.