Formal Linux Kernel Memory Model - linux-kernel

images and citations comes from:
Frightening Small Children and Disconcerting Grown-ups: Concurrency in the Linux Kernel
Let's consider a simple program:
cumul-fence is defined as:
cumul-fence := A-cumul(strong-fence ∪ po-rel) ∪ wmb
A-cumul(r) := rfe';r
In the linked publication in 3.2.3 it is written that (b, e) ∈ prop. From that we can conclude that (c, d) ∈ cumul-fence.
So, let's see:
po-rel = {(c,d)}
strong-fence = {(a,b),(e,f)}
wmb = {}
rfe = {(d,e)}
rfe' = {(d,d), (d,e), (e,e)} <- reflexive closure of rfe.
A-cumul({(a,b),(e,f),(c,d)}) = {(d,d), (d,e), (e,e)};{(a,b),(e,f),(c,d)} = {(d,f), (e,f)}
cumul-fence = {(d,f), (e,f)}
so, as we can see (c,d) is not in cumul-fence. Can someone explain me where my reasoning is incorrect?

rfe', the reflexive closure of rfe, is
{(d,e), (a, a), (b, b), (c, c), (d, d), (e, e), (f, f), (k, k), (r, r)}
since the set of nodes is {a, b, c, d, e, f, k, r}.
From there, cumul-fence is {(d, f), (a, b), (c, d), (e, f)}.

Related

An efficient way to iteratively substitute map values into another map with spark

Lets say I have some data structure, it could be List[Map[String, List[String]]] or potentially Dataset[Map[String, List[String]]]
and it contains maps like the following
[a, (b)]
[b, (a, c, d)]
[c, (b, d)]
[d, (b, c)]
[e, (f, g)]
[f, (e)]
[g, (e, h)]
I would like to find a way to obtain the following
[a, (a, b, c, d)]
[b, (a, b, c, d)]
[c, (a, b, c, d)]
[d, (a, b, c, d)]
[e, (e, f, g, h)]
[f, (e, f, g, h)]
[g, (e, f, g, h)]
Is there a good way to code this up using spark?
And what would be the best data types/structures to use for this process?
The only way I can think to solve this is to continuously loop over the maps and perform substitutions, but there must be a better way.
It seems like you need to use transitive closure algorithm.
There is an example of implementation with iterative join.

ZDD with Quantification in Prolog

What would be the ZDD approach in Prolog that also provides quantifiers.
Notation for existential quantifier would be as follows:
X^F
where X is the bound variable and F is the formula. It corresponds
to the following formula:
F[X/0] v F[X/1]
How would one go about and write a Prolog routine that takes a ZDD
for F and a variable X and produces the ZDD for X^F ?
Daniel Pehoushek posted some interesting C-code, which I translated to Prolog. Its not directly working with ZDD, but rather with sets of sets of variables, which is also explained here. But I guess the algorithm can be translated from sets of sets of variables to ZDD.
It only needs two new operations, the rest works with the operations from library(lists). One operation is split/4 which gives the left and right branch for a tree root. And the other operation is join/4 which does the inverse. The main routine is bob/3:
bob(_, [], []) :- !.
bob([], A, A).
bob([V|W], A, T) :-
split(A, V, L, R),
intersection(L, R, H),
bob(W, H, P),
union(L, R, J),
bob(W, J, Q),
join(V, P, Q, T).
What does bob do? It transforms a formula P into a formula Q. The original formula P has propositional variables x1,..,xn. The resulting formulas has propositional variables x1',..,xn' which act as switches, where xi'=0 means ∀xi and xi'=1 means ∃xi. So the desired quantification result can be read off in the corresponding truth table row of Q.
Lets make an example:
P = (- A v B) & (-B v C)
Q = (A' v B') & (B' v C') & (A' v C')
A'B'C' Q
000 0
001 0
010 0
011 1 eg: (All A)(Exists B)(Exists C) P
100 0
101 1
110 1
111 1
Or as a Prolog call, computing Q from P for the above example:
?- bob([c,b,a], [[],[c],[b,c],[a,b,c]], A).
A = [[b, a], [c, a], [c, b], [c, b, a]]
Open source:
Some curious transformation by Daniel Pehoushek
https://gist.github.com/jburse/928f060331ed7d5307a0d3fcd6d4aae9#file-bob-pl

Prolog: Debugging Recursive Source Removal Algorithm

I am currently working on implementing a source-removal topological sorting algorithm for a directed graph. Basically the algorithm goes like this:
Find a node in a graph with no incoming edges
Remove that node and all edges coming out from it and write its value down
Repeat 1 and 2 until you eliminate all nodes
So, for example, the graph
would have a topological sort of a,e,b,f,c,g,d,h. (Note: topological sorts aren't unique and thus there can be a different topological sort as well)
I am currently working on a Prolog implementation of this with the graph being represented in list form as follows:
[ [a,[b,e,f]], [b,[f,g]], [c,[g,d]], [d,[h]], [e,[f]], [f,[]],
[g,[h]], [h,[]] ]
Where the [a, [b,e,f] ] term for example represents the edges going from a to b, e, and f respectively, and the [b, [f,g] ] term represents the edges going from b to f and g. In other words, the first item in the array "tuple" is the "from" node and the following array contains the destinations of edges coming from the "from" node.
I am also operating under assumption that there is one unique name for each vertex and thus when I find it, I can delete it without worrying about any potential duplicates.
I wrote the following code
% depends_on shows that D is adjacent to A, i.e. I travel from A to D on the graph
% returns true if A ----> D
depends_on(G,A,D) :- member([A,Ns],G), member(D,Ns).
% doesnt_depend_on shows that node D doesnt have paths leading to it
doesnt_depend_on(G, D) :- \+ depends_on(G, _, D).
% removes node from a graph with the given value
remove_graph_node([ [D,_] | T], D, T). % base case -- FOUND IT return the tail only since we already popped it
remove_graph_node([ [H,Ns] | T], D, R) :- \+ H=D,remove_graph_node( T, D, TailReturn), append([[H,Ns]], TailReturn, R).
%----------------------------------------------------
source_removal([], []]). % Second parameter is empty list due to risk of a cycle
source_removal(G,Toposort):-member([D,_], G),
doesnt_depend_on(G,D),
remove_graph_node(G,D,SubG),
source_removal(SubG, SubTopoSort),
append([D], SubTopoSort, AppendResult),
Toposort is AppendResult.
And I tested the depends_on, doesnt_depend_on, and remove_graph_node by hand using the graph [ [a,[b,e,f]], [b,[f,g]], [c,[g,d]], [d,[h]], [e,[f]], [f,[]], [g,[h]], [h,[]] ] and manually changing the parameter variables (especially when it comes to node names like a, b, c and etc). I can vouch after extensive testing that they work.
However, my issue is debugging the source_removal command. In it, I repeatedly remove a node with no directed edge pointing towards it along with its outgoing edges and then try to add the node's name to the Toposort list I am building.
At the end of the function's running, I expect to get an array of output like [a,e,b,f,c,g,d,h] for its Toposort parameter. Instead, I got
?- source_removal([ [a,[b,e,f]], [b,[f,g]], [c,[g,d]], [d,[h]], [e,[f]], [f,[]], [g,[h]], [h,[]] ], Result).
false.
I got false as an output instead of the list I am trying to build.
I have spent hours trying to debug the source_removal function but failed to come up with anything. I would greatly appreciate it if anyone would be willing to take a look at this with a different pair of eyes and help me figure out what the issue in the source_removal function is. I would greatly appreciate it.
Thanks for the time spent reading this post and in advance.
The first clause for source_removal/2 contained a typo (one superfluous closing square bracket).
The last line for the second clause in your code says Toposort is AppendResult. Note that is is used in Prolog to denote the evaluation of an arithmetic expression, e.g., X is 3+4 yields X = 7 (instead of just unifying variable X with the term 3+4). When I change that line to use = (assignment, more precisely unification) instead of is (arithmetic evaluation) like so
source_removal([], []). % Second parameter is empty list due to risk of a cycle
source_removal(G,Toposort):-member([D,_], G),
doesnt_depend_on(G,D),
remove_graph_node(G,D,SubG),
source_removal(SubG, SubTopoSort),
append([D], SubTopoSort, AppendResult),
Toposort = AppendResult.
I get the following result:
?- source_removal([ [a,[b,e,f]], [b,[f,g]], [c,[g,d]], [d,[h]], [e,[f]], [f,[]], [g,[h]], [h,[]] ], Result).
Result = [a, b, c, d, e, f, g, h] ;
Result = [a, b, c, d, e, g, f, h] ;
Result = [a, b, c, d, e, g, h, f] ;
Result = [a, b, c, d, g, e, f, h] ;
Result = [a, b, c, d, g, e, h, f] ;
Result = [a, b, c, d, g, h, e, f] ;
Result = [a, b, c, e, d, f, g, h] ;
Result = [a, b, c, e, d, g, f, h] ;
Result = [a, b, c, e, d, g, h, f] ;
Result = [a, b, c, e, f, d, g, h] ;
Result = [a, b, c, e, f, g, d, h] ;
...
Result = [c, d, a, e, b, g, h, f] ;
false.
(Shortened, it shows 140 solutions in total.)
Edit: I didn't check all the solutions, but among the ones it finds is the one you gave in your example ([a,e,b,f,c,g,d,h]), and they look plausible in the sense that each either starts with a or with c.

Prolog ensure a rule's return parameters are unique and in canonical order

I have some data declared in a Prolog file that looks like the following:
gen1(grass).
gen1(poison).
gen1(psychic).
gen1(bug).
gen1(rock).
...
gen1((poison, flying)).
gen1((ghost, poison)).
gen1((water, ice)).
...
weak1(grass, poison).
weak1(grass, bug).
weak1(poison, rock).
strong1(grass, rock).
strong1(poison, grass).
strong1(bug, grass).
strong1(poison, bug).
strong1(psychic, poison).
strong1(bug, poison).
strong1(bug, psychic).
strong1(rock, bug).
Note that the data does not define strong1 or weak1 for compound gen1(...). Those are determined by rules which do not contribute to the minimal working example. I mention them because it might be useful to know they exist.
I am trying to find relations between these terms that form a cycle. Here's one sample function:
triangle1(A, B, C) :-
setof(A-B-C, (
gen1(A), gen1(B), gen1(C), A \= B, A \= C, B \= C,
strong1(A, B), strong1(B, C), strong1(C, A)
), Tris),
member(A-B-C, Tris).
This setup does remove duplicates where A, B, and C are in the same order. However, it doesn't remove duplicates in different orders. For example:
?- triangle1(A, B, C),
member(A, [bug, grass, rock]),
member(B, [bug, rock, grass]),
member(C, [bug, rock, grass]).
A = bug,
B = grass,
C = rock ;
A = grass,
B = rock,
C = bug ;
A = rock,
B = bug,
C = grass ;
false.
That query should only return one set of [A, B, C].
I've thought about using sort/2, but there are cases where simply sorting changes the meaning of the answer:
?- triangle1(A, B, C),
sort([A, B, C], [D, E, F]),
\+member([D, E, F], [[A, B, C], [B, C, A], [C, A, B]]).
A = D, D = bug,
B = F, F = psychic,
C = E, E = poison .
I also tried < and >, but those don't work on atoms, apparently.
Any thoughts?
(I looked at the similar questions, but have no idea how what I'm doing here compares to what other people are doing)
EDIT: As per comment about minimal working example.
You can try sorting inside the setof/3 call. So you should avoid the generation of triples in wrong order.
I mean: calling setof/3, instead of
A \= B, A \= C, B \= C,
try with
A #< B, A #< C, B \= C,
This way you impose that A is lower than B and lower than C, you avoid duplicates and maintain correct solutions.
The full triangle1/3
triangle1(A, B, C) :-
setof(A-B-C, (
gen1(A), gen1(B), gen1(C), A #< B, A #< C, B \= C,
strong1(A, B), strong1(B, C), strong1(C, A)
), Tris),
member(A-B-C, Tris).

Returning the median of a 5-tuple after using sort

I am new to FP and OCaml. The reason I am trying to do it in such a brute way is because I haven't learned lists in OCaml. I am trying to write a function which returns the median of a 5-tuple after sorting the 5-tuple by a function called sort5 which I wrote. This is the code
let median5 (a, b, c, d, e) =
let sort5 (a, b, c, d, e) =
let sort2 (a, b) = if a > b then (b, a) else (a, b) in
let sort3 (a, b, c) =
let (a, b) = sort2 (a, b) in
let (b, c) = sort2 (b, c) in
let (a, b) = sort2 (a, b) in
(a, b, c) in
let sort4 (a, b, c, d) =
let (a, b) = sort2 (a, b) in
let (b, c) = sort2 (b, c) in
let (c, d) = sort2 (c, d) in
let (a, b, c) = sort3 (a, b, c) in
(a, b, c, d) in
let (a, b) = sort2 (a, b) in
let (b, c) = sort2 (b, c) in
let (c, d) = sort2 (c, d) in
let (d, e) = sort2 (d, e) in
let (a, b, c, d) = sort4 (a, b, c, d) in
(a, b, c, d, e);;
I tried using if, get_med (a, b, c, d, e) = c and bunch of other silly ways that I thought would work but got nothing. I always get a syntax error, if I manage to get rid of that then I am stuck with unused variable sort5 or get_med. I am already sorry about the bruteness. Thank you.
Add the following to the end of your code:
in
let (_, _, m, _, _) = sort5 (a, b, c, d, e) in
m
Your code will be a lot more readable if you define each function on its own.
let sort2 (a, b) =
if a > b then (b, a) else (a, b)
let sort3 (a, b, c) =
let (a, b) = sort2 (a, b) in
let (b, c) = sort2 (b, c) in
let (a, b) = sort2 (a, b) in
(a, b, c)
let sort4 (a, b, c, d) =
let (a, b) = sort2 (a, b) in
let (b, c) = sort2 (b, c) in
let (c, d) = sort2 (c, d) in
let (a, b, c) = sort3 (a, b, c) in
(a, b, c, d)
let sort5 (a, b, c, d, e) =
let (a, b) = sort2 (a, b) in
let (b, c) = sort2 (b, c) in
let (c, d) = sort2 (c, d) in
let (d, e) = sort2 (d, e) in
let (a, b, c, d) = sort4 (a, b, c, d) in
(a, b, c, d, e)
let median5 (a, b, c, d, e) =
let (_, _, m, _, _) = sort5 (a, b, c, d, e) in
m
As you say, this code isn't at all practical. I hope you'll learn to work with lists soon :-)

Resources