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
design-patterns ocaml hashtable matching
add a comment |
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
design-patterns ocaml hashtable matching
add a comment |
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
design-patterns ocaml hashtable matching
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
design-patterns ocaml hashtable matching
edited Nov 19 at 12:34
Brian Tompsett - 汤莱恩
4,153133699
4,153133699
asked Nov 18 at 23:57
Nick White
913
913
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 19 at 5:35
answered Nov 19 at 4:11
Jeffrey Scofield
46.8k24877
46.8k24877
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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