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

(Hashtbl) type specialization (or how is it called in OCaml ?)

Expand Messages
  • Sergei Steshenko
    Hello, here is a short interactive session: sergei@amdam2:~/ocaml_work/plds /mnt/sdb8/sergei/AFSWD_debug/20121021/rlwrap-0.37/binsh/rlwrap
    Message 1 of 4 , Feb 24, 2013
      Hello,

      here is a short interactive session:

      "
      sergei@amdam2:~/ocaml_work/plds> /mnt/sdb8/sergei/AFSWD_debug/20121021/rlwrap-0.37/binsh/rlwrap /mnt/sdb8/sergei/AFSWD_debug/20121021/ocaml-3.12.1/binsh/ocaml
      Objective Caml version 3.12.1

      # type myht = (string, int) Hashtbl.t;;
      type myht = (string, int) Hashtbl.t
      # let hh:myht = Hashtbl.create 16;;
      val hh : myht = <abstr>
      # Hashtbl.add hh 1.0 1;;
      Error: This expression has type float but an expression was expected of type
      string
      # Hashtbl.add hh "one" 1;;
      - : unit = ()
      # Hashtbl.add hh "two" 2.0;;
      Error: This expression has type float but an expression was expected of type
      int
      #
      ".

      It works as expected. I.e. it limits Hashtbl key type to 'string', and it also limits Hashtbl value type to 'int'.

      The funny thing is that I came to '(string, int) Hashtbl.t' notation by trial and error, and by stumbling upon http://www.prologin.org/docs/ocaml/libref/type_Hashtbl.html .

      I.e. I was inspired by, say,

      val create : int -> ('a, 'b) Hashtbl.t

      line from there.

      So, my questions are:

      1) how is that type specialization/limiting/narrowing (from ('a, 'b) to (string, int) in this case) is officially called ? ... Type instance ? Using type constructor ?

      2) where can I read about it in documentation, tutorials, etc ?


      Thanks,
      Sergei.
    • Francois Berenger
      ... I think it is type annotation .
      Message 2 of 4 , Feb 24, 2013
        On 02/25/2013 10:12 AM, Sergei Steshenko wrote:
        > Hello,
        >
        > here is a short interactive session:
        >
        > "
        > sergei@amdam2:~/ocaml_work/plds>
        > /mnt/sdb8/sergei/AFSWD_debug/20121021/rlwrap-0.37/binsh/rlwrap
        > /mnt/sdb8/sergei/AFSWD_debug/20121021/ocaml-3.12.1/binsh/ocaml
        > Objective Caml version 3.12.1
        >
        > # type myht = (string, int) Hashtbl.t;;
        > type myht = (string, int) Hashtbl.t
        > # let hh:myht = Hashtbl.create 16;;
        > val hh : myht =
        > # Hashtbl.add hh 1.0 1;;
        > Error: This expression has type float but an expression was expected of type
        > string
        > # Hashtbl.add hh "one" 1;;
        > - : unit = ()
        > # Hashtbl.add hh "two" 2.0;;
        > Error: This expression has type float but an expression was expected of type
        > int
        > #
        > ".
        >
        > It works as expected. I.e. it limits Hashtbl key type to 'string', and
        > it also limits Hashtbl value type to 'int'.
        >
        > The funny thing is that I came to '(string, int) Hashtbl.t' notation by
        > trial and error, and by stumbling upon
        > http://www.prologin.org/docs/ocaml/libref/type_Hashtbl.html .
        >
        > I.e. I was inspired by, say,
        >
        > val create : int -> ('a, 'b) Hashtbl.t
        >
        > line from there.
        >
        > So, my questions are:
        >
        > 1) how is that type specialization/limiting/narrowing (from ('a, 'b) to
        > (string, int) in this case) is officially called ? ... Type instance ?
        > Using type constructor ?

        I think it is "type annotation".

        > 2) where can I read about it in documentation, tutorials, etc ?
        >
        > Thanks,
        > Sergei.
        >
        >
      • Ashish Agarwal
        You may want to read starting here: http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora015.html#toc12 ... [Non-text portions of this message have been
        Message 3 of 4 , Feb 24, 2013
          You may want to read starting here:
          http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora015.html#toc12

          On Sun, Feb 24, 2013 at 8:12 PM, Sergei Steshenko <sergstesh@...>wrote:

          > **
          >
          >
          > Hello,
          >
          > here is a short interactive session:
          >
          > "
          > sergei@amdam2:~/ocaml_work/plds>
          > /mnt/sdb8/sergei/AFSWD_debug/20121021/rlwrap-0.37/binsh/rlwrap
          > /mnt/sdb8/sergei/AFSWD_debug/20121021/ocaml-3.12.1/binsh/ocaml
          > Objective Caml version 3.12.1
          >
          > # type myht = (string, int) Hashtbl.t;;
          > type myht = (string, int) Hashtbl.t
          > # let hh:myht = Hashtbl.create 16;;
          > val hh : myht =
          > # Hashtbl.add hh 1.0 1;;
          > Error: This expression has type float but an expression was expected of
          > type
          > string
          > # Hashtbl.add hh "one" 1;;
          > - : unit = ()
          > # Hashtbl.add hh "two" 2.0;;
          > Error: This expression has type float but an expression was expected of
          > type
          > int
          > #
          > ".
          >
          > It works as expected. I.e. it limits Hashtbl key type to 'string', and it
          > also limits Hashtbl value type to 'int'.
          >
          > The funny thing is that I came to '(string, int) Hashtbl.t' notation by
          > trial and error, and by stumbling upon
          > http://www.prologin.org/docs/ocaml/libref/type_Hashtbl.html .
          >
          > I.e. I was inspired by, say,
          >
          > val create : int -> ('a, 'b) Hashtbl.t
          >
          > line from there.
          >
          > So, my questions are:
          >
          > 1) how is that type specialization/limiting/narrowing (from ('a, 'b) to
          > (string, int) in this case) is officially called ? ... Type instance ?
          > Using type constructor ?
          >
          > 2) where can I read about it in documentation, tutorials, etc ?
          >
          > Thanks,
          > Sergei.
          >
          >
          >


          [Non-text portions of this message have been removed]
        • Sergei Steshenko
          ... Thanks to all who responded - now I know that it is of type constraints and type annotation categories/search phrases :). Regards,   Sergei.
          Message 4 of 4 , Feb 24, 2013
            ----- Original Message -----
            > From: Ashish Agarwal <agarwal1975@...>
            > To: ocaml_beginners@yahoogroups.com
            > Cc:
            > Sent: Monday, February 25, 2013 3:45 AM
            > Subject: Re: "ocaml_beginners"::[] (Hashtbl) type specialization (or how is it called in OCaml ?)
            >
            > You may want to read starting here:
            > http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora015.html#toc12
            >

            Thanks to all who responded - now I know that it is of "type constraints" and "type annotation" categories/search phrases :).

            Regards,
              Sergei.
          Your message has been successfully submitted and would be delivered to recipients shortly.