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

How to restrict a Map's value to string ref?

Expand Messages
  • Clemens Hintze
    Hello, while I am trying to learn OCaml, I am implementing a small interpreter of a toy language, to get comfortable with OCamls features and gotchas. I seem
    Message 1 of 3 , Nov 27, 2002
    View Source
    • 0 Attachment
      Hello,

      while I am trying to learn OCaml, I am implementing a small
      interpreter of a toy language, to get comfortable with OCamls features
      and gotchas. I seem to make nice progress regarding that
      interpreter. At least I thought so till yesterday. Now I've got a
      problem that may be very basicly, but I cannot solve myself. Not with
      reading documentation (refman, O'Reilly book) nor with searching
      archives via Google.

      So I beg pardon in advance for my probably stupid questions, but I
      really need some advice. And here we go:

      I was implementing a function 'interpreter' that delivers a function
      that is able to evaluate streams and return a string as result. The
      interface was (in an own .mli file):

      val interpreter : unit -> char Stream.t -> string

      Internal I use reference to Map for representing the variable
      bindings, the interpreter will work with. The variable name (string)
      was the key and a reference to string was the value in the Map,
      representing also the value of that specific variable.

      Then I had the "clever" idea to let the user know about that variable
      binding map, calling it environment. So, I thought, the user of that
      interpreter could pass an initial environment to the interpreter. Easy
      done, I thought, so I changed the interface to:

      val interpreter : unit -> environment -> char Stream.t -> string

      and now I am stuck :-(

      I cannot define that environment within the interpreter's .mli
      file. I had tried to tell OCaml that environment is a Map with key =
      string and 'a t = string ref, but whatever syntax I tried, I could not
      get it compile.

      So that would be my main question: How to tell OCaml via the .mli
      file, that there is a type environment, that is a reference to a Map
      with keys of type string and values of type reference to string?

      I would not bother, that the user could know and use that Map, but I
      would also be interested, how to implement the type environment
      abstract, probably within an own module but defined in the same .mli
      file as the function 'interpreter'.

      And last but not least, if I create an Map like:

      module MyMap = Map.Make (struct type t=string let compare=compare)
      let m = MyMap.empty

      I get a Map, whose value is bound, but unknown at this
      time. I.e. during my first addition I could add any type to that Map
      as value. How can I restrict the possible values of 'm' to e.g. string
      ref *before* I added my first 'real' and desired value of correct
      type?

      Sorry for those many questions, but I hope some of you wizzards are
      able and willing to enlight me here :-)


      Thanks in advance,
      Clemens Hintze.

      --
      Clemens Hintze mailto: c.hintze@...
    • Remi VANICAT
      Clemens Hintze writes: [...] ... Well, you should write something as : module MyMap : Map.S with type key = string type
      Message 2 of 3 , Nov 27, 2002
      View Source
      • 0 Attachment
        Clemens Hintze <ml-ocaml_beginners@...-berlin.de> writes:


        [...]


        > Then I had the "clever" idea to let the user know about that variable
        > binding map, calling it environment. So, I thought, the user of that
        > interpreter could pass an initial environment to the interpreter. Easy
        > done, I thought, so I changed the interface to:
        >
        > val interpreter : unit -> environment -> char Stream.t -> string
        >
        > and now I am stuck :-(
        >
        > I cannot define that environment within the interpreter's .mli
        > file. I had tried to tell OCaml that environment is a Map with key =
        > string and 'a t = string ref, but whatever syntax I tried, I could not
        > get it compile.
        >
        > So that would be my main question: How to tell OCaml via the .mli
        > file, that there is a type environment, that is a reference to a Map
        > with keys of type string and values of type reference to string?

        Well, you should write something as :

        module MyMap : Map.S with type key = string

        type environment = (string ref) MyMap.t

        >
        > I would not bother, that the user could know and use that Map, but I
        > would also be interested, how to implement the type environment
        > abstract, probably within an own module but defined in the same .mli
        > file as the function 'interpreter'.

        the you should write :

        type environment

        val empty : environment
        val add : environment -> string -> string ref -> environment

        ....

        (you need to put in your mli some function to manipulate the
        environment. I would probably find better to make it in another
        .ml/.mli couple).


        >
        > And last but not least, if I create an Map like:
        >
        > module MyMap = Map.Make (struct type t=string let compare=compare)
        > let m = MyMap.empty
        >
        > I get a Map, whose value is bound, but unknown at this
        > time. I.e. during my first addition I could add any type to that Map
        > as value. How can I restrict the possible values of 'm' to e.g. string
        > ref *before* I added my first 'real' and desired value of correct
        > type?

        well :

        let m = (MyMap.empty : (string ref) MyMap.t)

        --
        RĂ©mi Vanicat
        vanicat@labri.u-bordeaux.fr
        http://dept-info.labri.u-bordeaux.fr/~vanicat
      • Clemens Hintze
        ... Hallo Remi, [...] ... Ouch! [...] ... Ouch! Uh! [...] ... Ouch! That hurts! Bummer! [...] That is far too easy! Now I am really looking very stupid! And
        Message 3 of 3 , Nov 27, 2002
        View Source
        • 0 Attachment
          In mailing-list.ocaml_beginners, you wrote:
          > Clemens Hintze <ml-ocaml_beginners@...-berlin.de> writes:
          >

          Hallo Remi,

          [...]

          > Well, you should write something as :
          >
          > module MyMap : Map.S with type key = string
          >
          > type environment = (string ref) MyMap.t

          Ouch!

          [...]

          > the you should write :
          >
          > type environment
          >
          > val empty : environment
          > val add : environment -> string -> string ref -> environment

          Ouch! Uh!

          [...]

          > well :
          >
          > let m = (MyMap.empty : (string ref) MyMap.t)

          Ouch! That hurts! Bummer!

          [...]

          That is far too easy! Now I am really looking very stupid! And this is
          your fault, as you have answered me. To compensate this fault, I would
          propose you to support me filing a CRQ to the OCaml core team,
          proposing to complexify that silly simple and consistent syntax to a
          more complex one involving usage of signatures, functors and some nice
          add-ones like some <:;*'> sprinkled in this kind of declaration, which
          has to be written down in *one* line (of course)! Do you agree ;-)

          Okay, all jokes aside! Thank you *very* much for your help! I really
          would *not* be able to figure that out; I was on the totally wrong
          way, having thought way too complex and never was coming even close to
          that simple solution ... I really do not dare to show you, what I've
          tried so far :-(

          It seems my defiencies concerning modules (and esp. functors) are
          still much larger than I had thought. I will have to re-read the
          manual and the book about this (it seems not to be my theme. I have
          already read these all at least five times, but there is still
          something about modules, signatures and functors that flee my
          grasping).

          Ironically, once seen your proposals, I had understood *at*once*, why
          your examples are the way you wrote them ... :-/

          After all, I really appreciate the way OCaml has solved this. I like
          this language more and more :-)))

          Let me thank you again, and wish you a nice evening ...


          Best regards,
          Clemens Hintze.

          --
          Clemens Hintze mailto: c.hintze@...
        Your message has been successfully submitted and would be delivered to recipients shortly.