Skip to content

Commit 61851da

Browse files
committed
adjusted comment style and updated constant-folding example
1 parent ad02019 commit 61851da

File tree

9 files changed

+323
-285
lines changed

9 files changed

+323
-285
lines changed

examples/constant-folding/constant-folding.rls

+42-41
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,82 @@
1-
% this simple implementation does NOT consider the control flow graph
2-
% i.e. if (){
3-
% x=1;}
4-
% else{
5-
% x=2;}
6-
% y=x;
7-
% will lead to inconsistencies in the calculation
8-
% thus it is insufficient for real world examples
1+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
2+
false("false"^^xsd:boolean).
93

10-
% NOTE: AssignLocal MUST NOT contain phi-assignments, otherwise the chase is not successfull
11-
% run something like 'grep -v "phi-assign" data/AssignLocal.tsv.gz > data/AssignLocal.tsv.gz'
4+
%! this simple implementation does NOT consider the control flow graph
5+
%! e.g. if (){
6+
%! x=1;}
7+
%! else{
8+
%! x=2;}
9+
%! y=x;
10+
%! will lead to inconsistencies in the calculation
11+
%! thus it is insufficient for real world examples
1212

13-
% dive into doop-syntax:
14-
% the instrucion-id (?ins) is a unique identifier for each line of intermediate code (IR)
15-
% everything that happens on one IR-line, will be linked with the same instruction-id
16-
% it may look like this: "<Example: void main(java.lang.String[])>/assign/4"
17-
% where "<Example: void main(java.lang.String[])>/" is the function signature
18-
% "assign/" means it is an assignment (i.e. a=b;), and "4" is the line number of the statement
13+
%! dive into doop-syntax:
14+
%! the instrucion-id (?ins) is a unique identifier for each line of intermediate code (IR)
15+
%! everything that happens on one IR-line, will be linked with the same instruction-id
16+
%! it may look like this: "<Example: void main(java.lang.String[])>/assign/4"
17+
%! where "<Example: void main(java.lang.String[])>/" is the function signature
18+
%! "assign/" means it is an assignment (i.e. a=b;), and "4" is the line number of the statement
1919

2020

21-
%loaded files from doop:
21+
%% load facts generated by doop
2222

23-
% AssignNumConstant: contains all variables that hold a constant value (i.e. a = 1) -> this is the base for this analysis
23+
%%% Contains all variables that hold a constant value (i.e. a = 1) -> this is the base for this analysis
2424
@import AssignNumConstant :- tsv { resource = "data/AssignNumConstant.tsv.gz" } .
2525

2626

27-
% AssignLocal: holds assignments of type a = b.
28-
% which only holds static assignments (assignments that include the string "phi-assign" are dynamically and have been removed)
27+
%%% Holds assignments of type a = b.
28+
%%% Which only holds static assignments (assignments that include the string "phi-assign" are dynamically and have been removed)
2929
@import AssignLocal :- tsv { resource = "data/AssignLocal.tsv.gz" } .
3030

3131

32-
% OperatorAt: links an instruction-id of a unary or binary expression to its operator
33-
% most common operators: (~, +, -, *, /, >=, <=, ==, <<, !=, >, len, <, %, ^, cmp, &, cmpl, cmpg, >>>, >>, |)
32+
%%% Links an instruction-id of a unary or binary expression to its operator
33+
%%% Most common operators: (~, +, -, *, /, >=, <=, ==, <<, !=, >, len, <, %, ^, cmp, &, cmpl, cmpg, >>>, >>, |)
3434
@import OperatorAt :- tsv { resource = "data/OperatorAt.tsv.gz" } .
3535

3636

37-
% AssignUnop: holds the instruction-id and variable that is getting assigned by a unary expression
37+
%%% Holds the instruction-id and variable that is getting assigned by a unary expression
3838
@import AssignUnop :- tsv { resource = "data/AssignUnop.tsv.gz" } .
3939

4040

41-
% AssignBinop: holds the instruction-id and the variable that is getting assigned by a binary expression
41+
%%% Holds the instruction-id and the variable that is getting assigned by a binary expression
4242
@import AssignBinop :- tsv { resource = "data/AssignBinop.tsv.gz" } .
4343

4444

45-
% AssignOperFrom: links an instruction-id of a unary or binary expression, to the VARIABLE(S) that are used
46-
% (i.e. a = b + c, will have two entries in AssignOperFrom)
45+
%%% Links an instruction-id of a unary or binary expression, to the VARIABLE(S) that are used
46+
%%% (e.g. a = b + c, will have two entries in AssignOperFrom)
4747
@import AssignOperFrom :- tsv { resource = "data/AssignOperFrom.tsv.gz" } .
4848

4949

50-
% AssignOperFromConstant: links an instruction-id of a unary or binary expression to the CONSTANT(S) used
51-
% (i.e. a = b + 2, will have one entries in AssignOperFromConstant)
50+
%%% Links an instruction-id of a unary or binary expression to the CONSTANT(S) used
51+
%%% (e.g. a = b + 2, will have one entry in AssignOperFromConstant)
5252
@import AssignOperFromConstant :- tsv { resource = "data/AssignOperFromConstant.tsv.gz" }.
5353

5454

55-
56-
% Implementation:
55+
%% Implementation:
5756
% IntegerConstant contains all constants (given & computed)
5857
% it is initialized with all constants that are supplied by doop in AssignNumConstant (i.e. a = 2;)
5958
% ConstFolding will contain all variables and values where constant folding & propagation was successfull
59+
6060
% the doop relations AssignUnop & AssignBinop ONLY contain the instruction-id and the assigned variable
6161
% -> we need to manually check, if the expression consists of constants (a = 1 + 2;) variables (a = b + c;) or both (a = b + 2;)
6262
% -> 2 cases for unary expressions: (- const), (- var)
6363
% -> 3 cases each for binary expressions (+,*): (var + var), (const + const), (const + var)-> order of var & const is irrelevant
6464
% -> 4 cases each for binary expressions (-, /): (var - var), (const - const), (const - var), (var - const) -> order of var & const is relevant
6565

66-
67-
% Initialize IntegerConstant with all constants from AssignNumConstant
66+
%%% Initialize IntegerConstant with all constants from AssignNumConstant
6867
IntegerConstant(?var, ?val,?meth) :-
6968
AssignNumConstant(_, _, ?val, ?var, ?meth).
7069

71-
%Assignment without Operator (i.e. a = b)
70+
%%% Assignment without Operator (i.e. a = b)
71+
%%% Remove statements containing 'phi-assign' since they indicate CFG-issues
7272
IntegerConstant(?var_to, ?val, ?meth),
7373
ConstFolding(?ins,?var_to, ?val, ?meth) :-
7474
AssignLocal(?ins, _, ?var_from, ?var_to, ?meth),
75-
IntegerConstant(?var_from, ?val, ?meth).
76-
75+
false(CONTAINS(?ins,"phi-assign")),
76+
IntegerConstant(?var_from, ?val, ?meth).
7777

7878

79-
% Unary-Negation: "-" is translated to "~" in OperatorAt
79+
%% Unary-Negation: "-" is translated to "~" in OperatorAt
8080

8181
IntegerConstant(?var, 0 - ?val1, ?meth),
8282
ConstFolding(?ins,?var, 0 - ?val1, ?meth) :-
@@ -94,7 +94,8 @@ ConstFolding(?ins,?var, 0 - ?val1, ?meth) :-
9494
AssignOperFromConstant(?ins, _, ?val1).
9595

9696

97-
% Addition
97+
%% Addition
98+
9899
IntegerConstant(?var, ?val1 + ?val2, ?meth),
99100
ConstFolding(?ins,?var, ?val1 + ?val2, ?meth) :-
100101
AssignBinop(?ins,_, ?var, ?meth),
@@ -127,7 +128,7 @@ ConstFolding(?ins,?var, ?val1 + ?val2, ?meth) :-
127128

128129

129130

130-
% Subtraction
131+
%% Subtraction
131132

132133
IntegerConstant(?var, ?val1 - ?val2, ?meth),
133134
ConstFolding(?ins,?var, ?val1 - ?val2, ?meth) :-
@@ -170,8 +171,7 @@ ConstFolding(?ins,?var, ?val1 - ?val2, ?meth) :-
170171
IntegerConstant(?var2, ?val2,?meth).
171172

172173

173-
174-
% Multiplication
174+
%% Multiplication
175175

176176
IntegerConstant(?var, ?val1 * ?val2, ?meth),
177177
ConstFolding(?ins,?var, ?val1 * ?val2, ?meth) :-
@@ -202,7 +202,8 @@ ConstFolding(?ins,?var, ?val1 * ?val2, ?meth) :-
202202
IntegerConstant(?var1, ?val1, ?meth),
203203
IntegerConstant(?var2, ?val2,?meth).
204204

205-
% Division
205+
206+
%% Division
206207

207208
IntegerConstant(?var, ?val1 / ?val2, ?meth),
208209
ConstFolding(?ins,?var, ?val1 / ?val2, ?meth) :-
+19-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
%%% Sensor Readings Example from https://datalogmtl.github.io/
1+
%! Sensor Readings Example from https://datalogmtl.github.io/
22

3-
%% Facts
3+
% Original facts:
44
% HighTemperature(sensor1)@[0,27.5]
55
% HighTemperature(sensor2)@3.5
66
% HighTemperature(sensor2)@5.1
@@ -9,14 +9,16 @@
99
% HighTemperature(sensor2)@20
1010
% SameLocation(sensor1,sensor2)@(-inf,+inf)
1111

12-
%% Rules
12+
% Original Rules:
1313
% Overheat(X) :- ALWAYS[-10,0] HighTemperature(X)
1414
% Overheat(X) :- ALWAYS[-20,0] SOMETIME[-5.5,0] HighTemperature(X)
1515
% Alert :- Overheat(X), Overheat(Y), SameLocation(X,Y)
16-
17-
%% Entailment check
16+
% Entailment check
1817
% Alert@25
1918

19+
20+
%% Translated facts
21+
2022
HighTemperature("sensor1", 0.0, 27.5).
2123
HighTemperature("sensor2", 3.5, 3.5).
2224
HighTemperature("sensor2", 5.1, 5.1).
@@ -25,7 +27,10 @@ HighTemperature("sensor2", 14.7, 14.7).
2527
HighTemperature("sensor2", 20.0, 20.0).
2628
SameLocation("sensor1", "sensor2"). % Always holds
2729

28-
% aux rule to combine intervals
30+
31+
%% Translated rules
32+
33+
%%% aux rule to combine intervals
2934
HighTemperatureSometimes(?Sensor, ?StartA, ?EndB) :-
3035
HighTemperatureSometimes(?Sensor, ?StartA, ?EndA),
3136
HighTemperatureSometimes(?Sensor, ?StartB, ?EndB),
@@ -34,7 +39,7 @@ HighTemperatureSometimes(?Sensor, ?StartA, ?EndB) :-
3439
?StartA <= ?StartB,
3540
?EndA <= ?EndB.
3641

37-
% aux rule to combine intervals
42+
%%% aux rule to combine intervals
3843
Overheat(?Sensor, ?StartA, ?EndB) :-
3944
Overheat(?Sensor, ?StartA, ?EndA),
4045
Overheat(?Sensor, ?StartB, ?EndB),
@@ -43,26 +48,28 @@ Overheat(?Sensor, ?StartA, ?EndB) :-
4348
?StartA <= ?StartB,
4449
?EndA <= ?EndB.
4550

46-
% rule 1
51+
%%% corresponds to rule 1
4752
Overheat(?Sensor, ?Start + 10.0, ?End) :-
4853
HighTemperature(?Sensor, ?Start, ?End),
4954
?End - ?Start >= 10.0.
5055

51-
% rule 2 (split into two rules)
56+
57+
%% corresponds to rule 2 (split into two rules)
58+
5259
HighTemperatureSometimes(?Sensor, ?Start, ?End + 5.5) :-
5360
HighTemperature(?Sensor, ?Start, ?End).
5461
Overheat(?Sensor, ?Start + 20.0, ?End) :-
5562
HighTemperatureSometimes(?Sensor, ?Start, ?End),
5663
?End - ?Start >= 20.0.
5764

58-
% rule 3 (slightly altered since we do not have nullary predicates)
65+
%%% corresponds to rule 3 (slightly altered since we do not have nullary predicates)
5966
Alert(?SensorA, MAX(?StartA, ?StartB), MIN(?EndA, ?EndB)), Alert(?SensorB, MAX(?StartA, ?StartB), MIN(?EndA, ?EndB)) :-
6067
Overheat(?SensorA, ?StartA, ?EndA),
6168
Overheat(?SensorB, ?StartB, ?EndB),
6269
SameLocation(?SensorA, ?SensorB),
6370
?StartA <= ?EndB,
6471
?StartB <= ?EndA.
6572

66-
@output Overheat.
67-
@output Alert.
73+
@export Overheat :- tsv{resource="Overheat.tsv"}.
74+
@export Alert :- tsv{resource="Alert.tsv"}.
6875

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
%%% Inspired by:
2-
%%% https://github.com/wdimmy/MeTeoR/blob/main/experiments/AAAI2022/programs/w.txt
3-
%%% (diamondminus in head should really be a boxminus according to original example; seems to be a typo in the meteor repo)
1+
%! Inspired by:
2+
%! https://github.com/wdimmy/MeTeoR/blob/main/experiments/AAAI2022/programs/w.txt
3+
%! (diamondminus in head should really be a boxminus according to original example; seems to be a typo in the meteor repo)
44

5-
%% (a)
6-
%% Boxminus[0,1]ExcessiveHeat(X):-Diamondminus[0,1]TempAbove41(X),Boxminus[0,1]TempAbove24(X)
7-
%% HeatAffectedState(X):-ExcessiveHeat(Y),LocatedInState(Y,X)
8-
%%
9-
%% (b)
10-
%% Boxminus[0,1]HeavyWind(X):-Boxminus[0,1]HeavyWindForce(X)
11-
%% HeavyWindAffectedState(X):-HeavyWind(Y),LocatedInState(Y,X)
125

136
%% Weather Station in Ohio with some Temperature Data
147

@@ -20,49 +13,52 @@ HeavyWindForce("StationA", 113.4,115.2).
2013
HeavyWindForce("StationA", 115.3,117.0).
2114
HeavyWindForce("StationA", 117.1,118.0).
2215

23-
%% (a)
16+
%% Boxminus[0,1]ExcessiveHeat(X):-Diamondminus[0,1]TempAbove41(X),Boxminus[0,1]TempAbove24(X)
17+
%% HeatAffectedState(X):-ExcessiveHeat(Y),LocatedInState(Y,X)
2418

25-
% diamondminus part of first body
19+
%%% diamondminus part of first body
2620
TempAbove41DiamondMinus1(?Station, ?Start, ?End + 1.0)
2721
:- TempAbove41(?Station, ?Start, ?End).
28-
% boxminus part of first body
22+
23+
%%% boxminus part of first body
2924
TempConstantOver24AtLeastFor1(?Station, ?Start + 1.0, ?End)
3025
:- TempAbove24(?Station, ?Start, ?End),
3126
?Start + 1.0 < ?End.
3227

33-
% first rule using aux predicates
28+
%%% first rule using aux predicates
3429
ExcessiveHeat(?Station, MAX(?Start24, ?Start41) - 1.0, MIN(?End41, ?End24))
3530
:- TempAbove41DiamondMinus1(?Station, ?Start41, ?End41),
3631
TempConstantOver24AtLeastFor1(?Station, ?Start24, ?End24),
3732
?Start41 < ?End24,
3833
?Start24 < ?End41.
3934

40-
% second rule
35+
%%% second rule
4136
HeatAffectedState(?State, MAX(?StartHeat, ?StartIn), MIN(?EndHeat, ?EndIn))
4237
:- ExcessiveHeat(?Station, ?StartHeat, ?EndHeat),
4338
LocatedInState(?Station, ?State, ?StartIn, ?EndIn),
4439
?StartHeat < ?EndIn,
4540
?StartIn < ?EndHeat.
4641

47-
%% (b)
4842

49-
% third rule
43+
%% Boxminus[0,1]HeavyWind(X):-Boxminus[0,1]HeavyWindForce(X)
44+
%% HeavyWindAffectedState(X):-HeavyWind(Y),LocatedInState(Y,X)
45+
46+
%%% third rule
5047
HeavyWind(?Station, ?Start, ?End)
5148
:- HeavyWindForce(?Station, ?Start, ?End),
5249
?Start + 1.0 < ?End.
5350

54-
% fourth rule
51+
%%% fourth rule
5552
HeavyWindAffectedState(?State, MAX(?StartWind, ?StartIn), MIN(?EndWind, ?EndIn))
5653
:- HeavyWind(?Station, ?StartWind, ?EndWind),
5754
LocatedInState(?Station, ?State, ?StartIn, ?EndIn),
5855
?StartWind < ?EndIn,
5956
?StartIn < ?EndWind.
6057

61-
@output TempAbove41DiamondMinus1.
62-
@output TempConstantOver24AtLeastFor1.
63-
@output ExcessiveHeat.
64-
@output HeatAffectedState.
65-
66-
@output HeavyWind.
67-
@output HeavyWindAffectedState.
58+
@export TempAbove41DiamondMinus1 :- tsv{resource="TempAbove41DiamondMinus1.tsv"}.
59+
@export TempConstantOver24AtLeastFor1 :- tsv{resource="TempConstantOver24AtLeastFor1.tsv"}.
60+
@export ExcessiveHeat :- tsv{resource="ExcessiveHeat.tsv"}.
61+
@export HeatAffectedState :- tsv{resource="HeatAffectedState.tsv"}.
62+
@export HeavyWind :- tsv{resource="HeavyWind.tsv"}.
63+
@export HeavyWindAffectedState :- tsv{resource="HeavyWindAffectedState.tsv"}.
6864

examples/lime-trees/old-lime-trees.rls

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
%%% This example finds trees of (some species of lime/linden tree) in Dresden,
2-
%%% which are more than 200 years old.
3-
%%%
4-
%%% It shows how to load (typed) data from (compressed) CSV files, how to
5-
%%% perform a recursive reachability query, and how to use datatype built-in to
6-
%%% find old trees. It can be modified to use a different species or genus of
7-
%%% plant, and by changing the required age.
1+
%! This example finds trees of (some species of lime/linden tree) in Dresden,
2+
%! which are more than 200 years old.
3+
%!
4+
%! It shows how to load (typed) data from (compressed) CSV files, how to
5+
%! perform a recursive reachability query, and how to use datatype built-in to
6+
%! find old trees. It can be modified to use a different species or genus of
7+
%! plant, and by changing the required age.
88

9-
@import tree :- csv { resource = "dresden-trees-ages-heights.csv" } . % location URL, species, age, height in m
10-
@import taxon :- csv { resource = "wikidata-taxon-name-parent.csv.gz" } . % Wikidata ID, taxon name, Wikidata ID of parent taxon
9+
%%% tree contains: (location URL, species, age, height in m)
10+
@import tree :- csv { resource = "dresden-trees-ages-heights.csv",format=(string,string,int,int)} .
11+
12+
%%% taxon contains: (Wikidata ID, taxon name, Wikidata ID of parent taxon)
13+
@import taxon :- csv { resource = "wikidata-taxon-name-parent.csv.gz" } .
1114

1215
limeSpecies(?X, Tilia) :- taxon(?X, Tilia, ?P).
1316
limeSpecies(?X, ?Name) :- taxon(?X, ?Name, ?Y), limeSpecies(?Y, ?N).

0 commit comments

Comments
 (0)