Active Unions in Static Scope - scope

Suppose i want to know what unions(referencing environment) are active in the point marked with (*), how do i acknowledge that ? Which unions are in fact active ?
procedure P(A,B ; real)
X: real
procedure Q(B,C : real)
y : real
...
procedure R(A,C : real)
Z:real
........ --(*)

It's basic nesting basically. But you don't specify the full blockstructure (with begin..end pairs) to fully fixate the structure.
Assuming from indentation that the begin end; block of P is at the end, and of Q and R is directly after resp. the y and z declarations, then in Q: Q is searched first, then P, then the scope above P (mainprogram/unit or another procedure), in R R, P,unit etc.

Related

value of callee and caller when using call by references

I encountered a confusion , when i pass a variable x to variable y by reference then both x and y should now point to same location, but the output that i am getting is not same.
Full detail discussion is here: http://gateoverflow.in/94182/programming-output
I have tried my best to explain the stuff to user but i am still unable to convience him fully, maybe i am lacking some concept.
rough code sample:
var b : int;
procedure M (var a, int)
begin
a= a*a;
print(a);
end;
procedure N
begin
b= b+1;
M(b);
end;
begin
b=12;
N;
print(b);
end;
I assume that as in question it is given that variables are static , so the value of a b should not change from 13 , but the value of a should be 13*13=169 , but my reasoning is counter to what call by reference is about.
pascal code from unauthorized book, please throw some insights.
I had to review scoping terminology. I had myself confused between static and dynamic scoping. Static scoping is used in all modern programming languages. I conclude that both a and b should have a value of 169 at the respective print statements.

Expressing rules in B-Method

I'm writing some specifications of a system in B-method. I have the following variables which are subsets of a general set:
First Notation:
a :={x,y,z,v}
b :={x,y,z}
I want to state a rule that whenever something exists in set "b", it also exists in set "a" which helps writing the above specifications as the following:
second Notation:
a :={v}
b :={x,y,z}
Explanation of second notation: I want the machine to infer that a :={x,y,z,v} from a :={v}, b :={x,y,z}, and the rule.
How can I express the rule so I avoid the first notation and instead write the second notation?
I tried the following but it didn't work
INITIALISATION
a :={v} &
b :={x,y,z}
ASSERTIONS
!x.(x:b => x:a)
First of all, the predicate !x.(x:b => x:a) can be more easily expressed just by b<:a.
It's not clear to me what exactly you want to express: Should b always be a subset of a or just in the initialisation?
If always, the INVARIANT would be the correct location for that. ASSERTIONS are similar but should be an implication by the other invariants.
But then you must explicitly ensure that in your initialisation.
Another point which is unclear to me is what you mean by "infer". Do you just not want to specify the details?
An initialisation where you assign a set with one element more than b could look like the following (assuming that a and b contain elements of S):
INITIALISATION
ANY v,s
WHERE
v:S
& s<:S
THEN
a := s\/{v}
|| b := s
END
(Disclaimer: I haven't actually tested it.)
If a should always be larger than b, you could add something like v/:s.
Your description does not make it clear what exactly you want to achieve.
Another approach would use the "becomes such substitution" (but in my opinion it is less readable):
INITIALISATION
a,b :( a:S & b:S &
b={x,y,z} &
#x.( x:S & a=b\/{x} ))
First, and foremost, the B machine does not infer anything by itself. It provides a language where the user can express properties and a mechanism that generates proof obligations that must be successfully processed by the prover (automatically or with human assistance) to guarantee that the properties hold.
In your example, if you want to express that every element of set bb is always an element of set aa, then as observed by danielp, just write
bb <: aa
Next, if you want to write that aa apossesses an element that is not in bb, then you can express it as
aa /= bb & not(aa = {})
or as
#(elem).(elem : S & elem : bb & not(elem : aa))
If you rather want to express that the specific value vv is in aa but not in bb, then the following applies
vv : bb & not(vv : aa)
These expressions may be used at several locations of the B machine, depending whether you want to assert properties on parameters, constants or variables.
For instance, say you have a machine with two variables va and vb, where both are sets of elements of a given set s1, and that you want they are initialized in such a way that every element of vb is also an element of va, and that there exists an element of va that is not in vb. Observe first that this means that vb is a strict subset of va.
INITIALISATION
ANY inia, inib WHERE
inia <: s1 & inib <: s2 & inib <<: inia
THEN
va := inia || vb := inib
END

Stop and copy garbage collector in OCAML

I have a homework assignment to finish writing a stopy-and-copy garbage collector in OCaml. There are 3 functions that need to be written. The first thing to be known is that a 64 slot array named ram will be what the garbage collector uses as memory. Each slot will contain an object of type 'cell'. That type may look like the following:
Object (id, size, references)
ObjData _
Free
FwdPointer (_)
I believe I am okay with the first function but the second function I need help with.
The function is:
let rec scan_tospace (free : int) (unscanned : int) =
Here is the objective of the function:
(* Scan To-space, copy all referenced objects to the To-space and
update references in objects. Recurse until the free pointer is
identical to the unscanned pointer.
[free] is the pointer to the next free address in the To-space,
[unscanned] is the address of the first unscanned object in the
To-space.
Return the address of the free pointer after all objects have been
scanned.
*)
What I want to do is pattern matching on the element at the unscanned pointer. If it is an object (x,y,z) then I want the result to go through each element in the integer list z and apply the function 'let copy_obj (free : int) (addr : int) =' to it as the argument addr. The problem is that the function copy_obj takes 2 arguments and I can't figure out how to also insert the second argument when calling List.iter like so:
List.iter obj_copy free z
I've also tried this as the result when it successfully matches with an object:
List.iter (fun k -> match k with
| int k -> copy_obj free k) z;
There I get this:
Error: This expression has type int * int
but an expression was expected of type unit
I didn't post any code on purpose, but if you'd liked see it I can post more. I didn't want to give any answers away. Also, I'm not look for someone to write any code for me, another reason I didn't post much of it. Any ideas in the right direction would be very helpful, thanks!
List.iter obj_copy free z should be List.iter (obj_copy free) z so the free is the first argument to obj_copy and the items in the list are the second. With this change you should get the same error as your later code.
The problem here is the use of List.iter. As you go over the referenced objects and copy them free has to change. Otherwise you copy each object over the previous one. You also need to remember where the copied objects now are so you can update the references in the outer object. Accordingly copy_obj returns (I assume) a tuple of the object and the new free.
You have to use List.fold_left or List.fold_right or manual recursion to go through the list while keeping track of free and the copied objects.

the reference was not found in the current environment

Disclaimer: this is for a homework assignment
I'm a coq noob, so I hope this is not a repeat question. I /have/ looked at this question, but my question seems to be unanswered still.
I have the following premises:
P \/ Q
~Q
I need to prove:
P
My coq code so far:
Section Q5.
Variables Q : Prop.
Goal P.
Hypothesis premise1 : P \/ Q.
Hypothesis premise2 : ~Q.
I get the following error when I try to execute the line Goal P.:
Error: The reference P was not found in the current environment.
These are the solutions that I was able to come up with:
Replace Variables Q : Prop. with Variables P Q : Prop.. The problem with this is that P will be assumed as a premise, which it is not
Add Variables P. before the goal declaration. This results in a syntax error.
Am I missing something? I don't seem to be able to figure this out.
The proper solution is 1, and the problem you are expecting is wrong.
When you write:
Variable P: Prop.
You are not assuming that P is inhabited (or, that "P holds"), but only that there exists a proposition named P, a "statement" whose validity is not considered here.
This is very different from writing:
Variable p: P.
Which assumes that there is a proof "p" that the type "P" is inhabited (if P has type Prop, p is a proof of the proposition P), and thus assumes that P is true.
Also, the reason why:
Variables P.
results in a syntax error is that you need to provide a type for each variable introduced (Coq can't figure it out magically when there is no information leading the type inference engine).
So it is perfectly fine to begin your script as:
Section Q5.
Variables P Q : Prop.
Hypothesis premise1 : P \/ Q.
Hypothesis premise2 : ~Q.
Goal P.

Handling nested variable scopes when implementing an interpreter

I'm currently writing an interpreter for a simple programming language and just wanted to ask on the best approach would be to tackle it.
The environment for a program is as follows:
type Env = [[(Var, Int)]]
So I've coded the lookup and update but I'm a bit stuck on how to deal with the the scope for each begin block. An example is shown below:
begin [a,b,c]
read i
n = 1
while i < 0 do
begin
n = 2 * n
i = i - 1
end;
write n
end
From my understanding the scope of the first begin would be [a,b,c,i,n]
and then the second begin would contain [i, n]
therefore the env would be
[ [ ("a",0), (b",0), ("c",0), ("i",3), ("n",2) ], [("n",8), ("i",0) ] ]`
Currently my lookup function returns the first occurrence of a variable, so I'm having problems with the 2nd scope (2nd begin).
I'm not quite sure how I can make the update and lookup function return the value associated with that particular scope.
Basically I have the code working for one begin statement, but I am having issues with 2 or more statements in the sample program.

Resources