0

My task consists in imitating features of an imperative programming language in Haskell. Now I'm struggling with assigning a value to a string. The point is that I have to do it with the types Env and Sigma. Env assigns a String (variable name) to a Location Loc. Sigma assigns the Location to a specific value. Here's the code:

type Loc = Int                 --Loc for Location
type Env =  String -> Loc 
type Sigma = Loc -> Int

assign :: String -> Int -> Env -> Sigma -> Sigma
assign name value env sig = \input -> ??

Now I don't know how to do it in the function assign of my program. The function should store the String name and the Location loc in the function env and also store the same location loc and the Value val in the function sig which must be returned by the whole function 'assign'.

Testing it with e.g. env = \"test"->42, sig= \42->6 (sig and env just provisionally for test case),assign "x" 45 env sig and then sig (env "x") should return 45. I don't know how you manage it to do without assigning the Locations to env and sig. Can you help me and tell me what I should do?

5
  • Can you show the implementation of sig? Commented Jan 28, 2017 at 14:16
  • @WillemVanOnsem What do you mean with implementation? sig is just the parameter of type Sigma for the function Commented Jan 28, 2017 at 14:19
  • Then what do you mean by “then sig (env "x") should return 45”? Commented Jan 28, 2017 at 14:32
  • @leftaroundabout Oh right. This is just for the test case. Corrected it in my question Commented Jan 28, 2017 at 14:42
  • 1
    Your test case is wrong. In addition, either your assign type signature or the env function in your test case is wrong. If the type signature is correct, your test case might look like let { env x = case x of { "test" -> 42; "x" -> 43 }; sig = \42 -> 6 } in let { sig2 = assign "x" 45 env sig } in sig2 (env "x") Commented Jan 28, 2017 at 14:56

1 Answer 1

2

Hint:

you need to start by comparing the input location to the location of name, i.e. env name.

When these are equal, you know the new value to return.

When these locations differ, you can access the old Sigma to retrieve the old value for the input location.

Sign up to request clarification or add additional context in comments.

3 Comments

if you mean this: assign name value env sig = \inp -> (if (inp == (env s)) then val else (sig inp)) . I tried this also but I wonder if this is correct. I mean the String name must be already assigned to the specific Location in env to use this function properly.
@JohnDoe Looks good to me (apart from s being name). Yes, this requires name to be already assigned a location by env, but there's no way around that. Usually you have a "declare" function which provides you with a new env, allocating a new location for the new variable. But this function is meant to assign to an already defined variable.
Yeah s was just the parameter I used for the String. I thought about it again and put into the context of my task this should be the correct solution. So thanks a lot for your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.