OCaml pattern matching scoping issues when appending to Hash Tables











up vote
0
down vote

favorite












I'm working on an interpreter assignment in OCaml in which I have to bind values. For example I might want to bind the value "A" to the value 3.



I'm trying to use the hash table module in ocaml to accomplishments for easy storage and lookups but there seem to be multiple problems related to scoping, and return types.



What I want to do is when the case in which the second value popped off the stack is of type name I want to bind NAME to whatever the first value popped was and then append UNIT to the stack. Unfortunately for some reason my solution doesn't work. Can anyone help me?



open Hashtbl;;

let bound_values = Hashtbl.create 123456
type stackVal =
INT of int
| STR of string
| BOOL of bool
| NAME of string
| UNIT of unit
| ERROR

let callBind (stk : stackVal list) : stackVal list =
match (stk) with
x::NAME(y)::stk -> UNIT(Hashtbl.add bound_values y x)::stk
| _ -> ERROR::stk









share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm working on an interpreter assignment in OCaml in which I have to bind values. For example I might want to bind the value "A" to the value 3.



    I'm trying to use the hash table module in ocaml to accomplishments for easy storage and lookups but there seem to be multiple problems related to scoping, and return types.



    What I want to do is when the case in which the second value popped off the stack is of type name I want to bind NAME to whatever the first value popped was and then append UNIT to the stack. Unfortunately for some reason my solution doesn't work. Can anyone help me?



    open Hashtbl;;

    let bound_values = Hashtbl.create 123456
    type stackVal =
    INT of int
    | STR of string
    | BOOL of bool
    | NAME of string
    | UNIT of unit
    | ERROR

    let callBind (stk : stackVal list) : stackVal list =
    match (stk) with
    x::NAME(y)::stk -> UNIT(Hashtbl.add bound_values y x)::stk
    | _ -> ERROR::stk









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm working on an interpreter assignment in OCaml in which I have to bind values. For example I might want to bind the value "A" to the value 3.



      I'm trying to use the hash table module in ocaml to accomplishments for easy storage and lookups but there seem to be multiple problems related to scoping, and return types.



      What I want to do is when the case in which the second value popped off the stack is of type name I want to bind NAME to whatever the first value popped was and then append UNIT to the stack. Unfortunately for some reason my solution doesn't work. Can anyone help me?



      open Hashtbl;;

      let bound_values = Hashtbl.create 123456
      type stackVal =
      INT of int
      | STR of string
      | BOOL of bool
      | NAME of string
      | UNIT of unit
      | ERROR

      let callBind (stk : stackVal list) : stackVal list =
      match (stk) with
      x::NAME(y)::stk -> UNIT(Hashtbl.add bound_values y x)::stk
      | _ -> ERROR::stk









      share|improve this question















      I'm working on an interpreter assignment in OCaml in which I have to bind values. For example I might want to bind the value "A" to the value 3.



      I'm trying to use the hash table module in ocaml to accomplishments for easy storage and lookups but there seem to be multiple problems related to scoping, and return types.



      What I want to do is when the case in which the second value popped off the stack is of type name I want to bind NAME to whatever the first value popped was and then append UNIT to the stack. Unfortunately for some reason my solution doesn't work. Can anyone help me?



      open Hashtbl;;

      let bound_values = Hashtbl.create 123456
      type stackVal =
      INT of int
      | STR of string
      | BOOL of bool
      | NAME of string
      | UNIT of unit
      | ERROR

      let callBind (stk : stackVal list) : stackVal list =
      match (stk) with
      x::NAME(y)::stk -> UNIT(Hashtbl.add bound_values y x)::stk
      | _ -> ERROR::stk






      design-patterns ocaml hashtable matching






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 12:34









      Brian Tompsett - 汤莱恩

      4,153133699




      4,153133699










      asked Nov 18 at 23:57









      Nick White

      913




      913
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You need to define the type stackVal before defining your hash table. Otherwise the type technically resides in an inner scope. Another way of saying it is that you are expecting the table to contain values of a type that didn't exist when the table was created.



          Here are two small examples showing the effect of the two orders:



          $ ocaml
          OCaml version 4.06.1

          # let ht = Hashtbl.create 1;;
          val ht : ('_weak1, '_weak2) Hashtbl.t = <abstr>
          # type t = A | B;;
          type t = A | B
          # let () = Hashtbl.add ht "key" A;;
          Error: This expression has type t but an expression
          was expected of type 'a
          The type constructor t would escape its scope
          #

          $ ocaml
          OCaml version 4.06.1

          # type t = A | B;;
          type t = A | B
          # let ht = Hashtbl.create 1;;
          val ht : ('_weak1, '_weak2) Hashtbl.t = <abstr>
          # let () = Hashtbl.add ht "key" A;;
          # Hashtbl.find ht "key";;
          - : t = A
          #


          As a side note, I would not open the Hashtbl module. Your code already uses full names (Hashtbl.x), so there's no benefit in opening it. And it's best to open as few modules as possible.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366678%2focaml-pattern-matching-scoping-issues-when-appending-to-hash-tables%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            You need to define the type stackVal before defining your hash table. Otherwise the type technically resides in an inner scope. Another way of saying it is that you are expecting the table to contain values of a type that didn't exist when the table was created.



            Here are two small examples showing the effect of the two orders:



            $ ocaml
            OCaml version 4.06.1

            # let ht = Hashtbl.create 1;;
            val ht : ('_weak1, '_weak2) Hashtbl.t = <abstr>
            # type t = A | B;;
            type t = A | B
            # let () = Hashtbl.add ht "key" A;;
            Error: This expression has type t but an expression
            was expected of type 'a
            The type constructor t would escape its scope
            #

            $ ocaml
            OCaml version 4.06.1

            # type t = A | B;;
            type t = A | B
            # let ht = Hashtbl.create 1;;
            val ht : ('_weak1, '_weak2) Hashtbl.t = <abstr>
            # let () = Hashtbl.add ht "key" A;;
            # Hashtbl.find ht "key";;
            - : t = A
            #


            As a side note, I would not open the Hashtbl module. Your code already uses full names (Hashtbl.x), so there's no benefit in opening it. And it's best to open as few modules as possible.






            share|improve this answer



























              up vote
              0
              down vote













              You need to define the type stackVal before defining your hash table. Otherwise the type technically resides in an inner scope. Another way of saying it is that you are expecting the table to contain values of a type that didn't exist when the table was created.



              Here are two small examples showing the effect of the two orders:



              $ ocaml
              OCaml version 4.06.1

              # let ht = Hashtbl.create 1;;
              val ht : ('_weak1, '_weak2) Hashtbl.t = <abstr>
              # type t = A | B;;
              type t = A | B
              # let () = Hashtbl.add ht "key" A;;
              Error: This expression has type t but an expression
              was expected of type 'a
              The type constructor t would escape its scope
              #

              $ ocaml
              OCaml version 4.06.1

              # type t = A | B;;
              type t = A | B
              # let ht = Hashtbl.create 1;;
              val ht : ('_weak1, '_weak2) Hashtbl.t = <abstr>
              # let () = Hashtbl.add ht "key" A;;
              # Hashtbl.find ht "key";;
              - : t = A
              #


              As a side note, I would not open the Hashtbl module. Your code already uses full names (Hashtbl.x), so there's no benefit in opening it. And it's best to open as few modules as possible.






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                You need to define the type stackVal before defining your hash table. Otherwise the type technically resides in an inner scope. Another way of saying it is that you are expecting the table to contain values of a type that didn't exist when the table was created.



                Here are two small examples showing the effect of the two orders:



                $ ocaml
                OCaml version 4.06.1

                # let ht = Hashtbl.create 1;;
                val ht : ('_weak1, '_weak2) Hashtbl.t = <abstr>
                # type t = A | B;;
                type t = A | B
                # let () = Hashtbl.add ht "key" A;;
                Error: This expression has type t but an expression
                was expected of type 'a
                The type constructor t would escape its scope
                #

                $ ocaml
                OCaml version 4.06.1

                # type t = A | B;;
                type t = A | B
                # let ht = Hashtbl.create 1;;
                val ht : ('_weak1, '_weak2) Hashtbl.t = <abstr>
                # let () = Hashtbl.add ht "key" A;;
                # Hashtbl.find ht "key";;
                - : t = A
                #


                As a side note, I would not open the Hashtbl module. Your code already uses full names (Hashtbl.x), so there's no benefit in opening it. And it's best to open as few modules as possible.






                share|improve this answer














                You need to define the type stackVal before defining your hash table. Otherwise the type technically resides in an inner scope. Another way of saying it is that you are expecting the table to contain values of a type that didn't exist when the table was created.



                Here are two small examples showing the effect of the two orders:



                $ ocaml
                OCaml version 4.06.1

                # let ht = Hashtbl.create 1;;
                val ht : ('_weak1, '_weak2) Hashtbl.t = <abstr>
                # type t = A | B;;
                type t = A | B
                # let () = Hashtbl.add ht "key" A;;
                Error: This expression has type t but an expression
                was expected of type 'a
                The type constructor t would escape its scope
                #

                $ ocaml
                OCaml version 4.06.1

                # type t = A | B;;
                type t = A | B
                # let ht = Hashtbl.create 1;;
                val ht : ('_weak1, '_weak2) Hashtbl.t = <abstr>
                # let () = Hashtbl.add ht "key" A;;
                # Hashtbl.find ht "key";;
                - : t = A
                #


                As a side note, I would not open the Hashtbl module. Your code already uses full names (Hashtbl.x), so there's no benefit in opening it. And it's best to open as few modules as possible.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 19 at 5:35

























                answered Nov 19 at 4:11









                Jeffrey Scofield

                46.8k24877




                46.8k24877






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366678%2focaml-pattern-matching-scoping-issues-when-appending-to-hash-tables%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Ottavio Pratesi

                    Tricia Helfer

                    15 giugno