-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to use flat ast fact representation.
- Loading branch information
Showing
26 changed files
with
868 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
% tagging of input ast elements | ||
ast(rule(H,B)) :- rule(H,B). | ||
ast(literal_tuple(I,P,E)) :- literal_tuple(I,P,E). | ||
ast(term_tuple(I,P,E)) :- term_tuple(I,P,E). | ||
ast(string(Id,Val)) :- string(Id,Val). | ||
ast(number(Id,Val)) :- number(Id,Val). | ||
ast(variable(Id,Name)) :- variable(Id,Name). | ||
ast(constant(Id,Name)) :- constant(Id, Name). | ||
ast(function(Id,Name,Args)) :- function(Id,Name,Args). | ||
ast(term_tuple(Id,Pos,Element)) :- term_tuple(Id,Pos,Element). | ||
ast(binary_operation(Id,Operator,Left,Right)) :- binary_operation(Id,Operator,Left,Right). | ||
ast(literal(Id,Sign,Function)) :- literal(Id,Sign,Function). | ||
ast(literal_tuple(Id,Pos,Element)) :- literal_tuple(Id,Pos,Element). | ||
ast(rule(Id,Head,Body)) :- rule(Id,Head,Body). | ||
ast(statement_tuple(Id,Pos,Element)) :- statement_tuple(Id,Pos,Element). | ||
ast(constant_tuple(Id,Pos,Element)) :- constant_tuple(Id,Pos,Element). | ||
ast(program(Name,Params,Statements)) :- program(Name,Params,Statements). | ||
|
||
|
||
final(A) :- ast(A), not delete(A), not replace(A,_). | ||
final(R) :- replace(_,R). | ||
final(N) :- new(N). | ||
final(N) :- add(N). | ||
|
||
#show final/1. |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
% reified fact representation of program: | ||
% #program base. | ||
% equal((1+1),2). | ||
|
||
program("base",constant_tuple(0),statement_tuple(1)). | ||
statement_tuple(1,0,rule(2)). | ||
rule(2,literal(3),literal_tuple(10)). | ||
% facts representing head literal equal((1+1),2). | ||
literal(3,"pos",function(4)). | ||
function(4,equal,term_tuple(5)). | ||
% facts representing the binary operation (1+1). | ||
term_tuple(5,0,binary_operation(6)). | ||
binary_operation(6,"+",number(7),number(8)). | ||
number(7,1). | ||
number(8,1). | ||
term_tuple(5,1,number(9)). | ||
number(9,2). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
% reified fact representation of program: | ||
% #program base. | ||
% good(human). | ||
|
||
program("base",constant_tuple(0),statement_tuple(1)). | ||
statement_tuple(1,0,rule(2)). | ||
rule(2,literal(3),literal_tuple(7)). | ||
% facts representing head literal yummy("carrot"). | ||
literal(3,"pos",function(4)). | ||
function(4,good,term_tuple(5)). | ||
term_tuple(5,0,constant(6)). | ||
constant(6,human). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
% reified fact representation of program: | ||
% #program base. | ||
% rel(2,1) :- rel(1,2). | ||
|
||
program("base",constant_tuple(0),statement_tuple(1)). | ||
statement_tuple(1,0,rule(2)). | ||
rule(2,literal(3),literal_tuple(8)). | ||
% facts representing head literal rel(2,1). | ||
literal(3,"pos",function(4)). | ||
function(4,rel,term_tuple(5)). | ||
term_tuple(5,0,number(6)). | ||
number(6,2). | ||
term_tuple(5,1,number(7)). | ||
number(7,1). | ||
% facts representing body literal rel(1,2). | ||
literal_tuple(8,0,literal(9)). | ||
literal(9,"pos",function(10)). | ||
function(10,rel,term_tuple(11)). | ||
term_tuple(11,0,number(12)). | ||
number(12,1). | ||
term_tuple(11,1,number(13)). | ||
number(13,2). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
% reified fact representation of program: | ||
% #program base. | ||
% next(move(a)). | ||
|
||
program("base",constant_tuple(0),statement_tuple(1)). | ||
statement_tuple(1,0,rule(2)). | ||
rule(2,literal(3),literal_tuple(9)). | ||
% facts representing head literal rel(2,1). | ||
literal(3,"pos",function(4)). | ||
function(4,next,term_tuple(5)). | ||
term_tuple(5,0,function(6)). | ||
function(6,move,term_tuple(7)). | ||
term_tuple(7,0,constant(8)). | ||
constant(8,a). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
% reified fact representation of program: | ||
% #program base. | ||
% a. | ||
|
||
program("base",constant_tuple(0),statement_tuple(1)). | ||
statement_tuple(1,0,rule(2)). | ||
rule(2,literal(3),literal_tuple(6)). | ||
% facts representing head literal a. | ||
literal(3,"pos",function(4)). | ||
function(4,a,term_tuple(5)). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
% reified fact representation of program: | ||
% #program base. | ||
% a :- b; not c. | ||
|
||
program("base",constant_tuple(0),statement_tuple(1)). | ||
statement_tuple(1,0,rule(2)). | ||
rule(2,literal(3),literal_tuple(6)). | ||
% facts representing head literal a. | ||
literal(3,"pos",function(4)). | ||
function(4,a,term_tuple(5)). | ||
% facts representing body literal b. | ||
literal_tuple(6,0,literal(7)). | ||
literal(7,"pos",function(8)). | ||
function(8,b,term_tuple(9)). | ||
% facts representing body literal not c. | ||
literal_tuple(6,1,literal(10)). | ||
literal(10,"not",function(11)). | ||
function(11,c,term_tuple(12)). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
% reified fact representation of program: | ||
% #program base. | ||
% yummy("carrot"). | ||
|
||
program("base",constant_tuple(0),statement_tuple(1)). | ||
statement_tuple(1,0,rule(2)). | ||
rule(2,literal(3),literal_tuple(7)). | ||
% facts representing head literal yummy("carrot"). | ||
literal(3,"pos",function(4)). | ||
function(4,yummy,term_tuple(5)). | ||
term_tuple(5,0,string(6)). | ||
string(6,"carrot"). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
% reified fact representation of program: | ||
% #program base. | ||
% rel(Y,X) :- rel(X,Y). | ||
|
||
program("base",constant_tuple(0),statement_tuple(1)). | ||
statement_tuple(1,0,rule(2)). | ||
rule(2,literal(3),literal_tuple(8)). | ||
% facts representing head literal rel(2,1). | ||
literal(3,"pos",function(4)). | ||
function(4,rel,term_tuple(5)). | ||
term_tuple(5,0,variable(6)). | ||
variable(6,"Y"). | ||
term_tuple(5,1,variable(7)). | ||
variable(7,"X"). | ||
% facts representing body literal rel(1,2). | ||
literal_tuple(8,0,literal(9)). | ||
literal(9,"pos",function(10)). | ||
function(10,rel,term_tuple(11)). | ||
term_tuple(11,0,variable(12)). | ||
variable(12,"X"). | ||
term_tuple(11,1,variable(13)). | ||
variable(13,"Y"). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
good(X) :- person(X). | ||
good(dog(X,"spotty")) :- cute(dog(X,"spotty")). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#program base. | ||
good(X) :- person(X); not bad(X). | ||
good(dog(X,"spotty")) :- cute(dog(X,"spotty")); not bad(dog(X,"spotty")). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
% ast transformation: | ||
|
||
% transforms every rule with single symbolic head | ||
% literal with name "good", adding a negated symbolic literal to the | ||
% body with name "bad", and same arguments as the head literal | ||
|
||
% examples: | ||
% good(X) :- person(X). | ||
% -> | ||
% good(X) :- person(X), not bad(X). | ||
% | ||
% good(dog(X,"spotty")) :- cute(dog(X,"spotty")). | ||
% -> | ||
% good(dog(X,"spotty")) :- cute(dog(X,"spotty")), not bad(dog(X,"spotty")). | ||
|
||
% auxiliary predicate to get maximal index within a tuple | ||
max_index(I,N) :- N = #max{ P : literal_tuple(I,P,_); -1 }, literal_tuple(I,_,_). | ||
|
||
|
||
% the transformation itself | ||
add_not_bad(LitTuple,N+1,Fargs) | ||
:- rule(_,literal(Lit),literal_tuple(LitTuple)), | ||
literal(Lit,"pos",function(Func)), | ||
function(Func,good,Fargs), | ||
max_index(LitTuple,N). | ||
|
||
|
||
% effect of add_not_bad | ||
add(literal_tuple(LitTuple,Pos,literal(new_id(LitTuple,0)))) | ||
:- add_not_bad(LitTuple,Pos,Fargs). | ||
|
||
add(literal(new_id(LitTuple,0),"not",function(new_id(LitTuple,1)))) | ||
:- add_not_bad(LitTuple,Pos,Fargs). | ||
|
||
add(function(new_id(LitTuple,1),bad,Fargs)) | ||
:- add_not_bad(LitTuple,Pos,Fargs). |
File renamed without changes.
2 changes: 2 additions & 0 deletions
2
...ro/asp/examples/transform/robot_output.lp → ...nopro/asp/tests/transform/robot_output.lp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
sad(robot(X),T) :- sad(robot(X),(T-1)); not cheer_up(robot(X),T). | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
rule(literal(0),literal_tuple(6)). | ||
|
||
literal(0,0,function(1)). | ||
function(1,"sad",term_tuple(2)). | ||
term_tuple(2,0,function(3)). | ||
function(3,"robot",term_tuple(4)). | ||
term_tuple(4,0,variable(5)). | ||
variable(5,"X"). | ||
|
||
literal_tuple(6,0,literal(7)). | ||
literal_tuple(6,1,literal(8)). | ||
|
||
|
||
function(1). | ||
|
||
literal(9,0,function(10)). | ||
function(11,"prev",term_tuple(12)). | ||
term_tuple(13,0,function(14)). | ||
function(15,"sad",term_tuple(16)). | ||
term_tuple(17,0,function(18)). | ||
function(19,"robot",term_tuple(20)). | ||
term_tuple(21,0,variable(22)). | ||
variable(23,"X"). | ||
|
||
literal(8,1,function(24)). | ||
function(24,"cheer_up",term_tuple(25)). | ||
turm_tuple(25,0,function(26)). | ||
function(26,"robot",term_tuple(27)). | ||
term_tuple(27,0,variable(28)). | ||
variable(28,"X"). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
rule(literal(0),literal_tuple(6)). | ||
|
||
literal(0,0,function(1)). | ||
function(1,"sad",term_tuple(2)). | ||
term_tuple(2,0,function(3)). | ||
function(3,"robot",term_tuple(4)). | ||
term_tuple(4,0,variable(5)). | ||
variable(5,"X"). | ||
|
||
literal_tuple(6,0,literal(7)). | ||
literal_tuple(6,0,literal(8)). | ||
|
||
literal(9,0,function(10)). | ||
function(11,"prev",term_tuple(12)). | ||
term_tuple(13,0,function(14)). | ||
function(15,"sad",term_tuple(16)). | ||
term_tuple(17,0,function(18)). | ||
function(19,"robot",term_tuple(20)). | ||
term_tuple(21,0,variable(22)). | ||
variable(23,"X"). | ||
|
||
literal(8,1,function(24)). | ||
function(24,"cheer_up",term_tuple(25)). | ||
turm_tuple(25,0,function(26)). | ||
function(26,"robot",term_tuple(27)). | ||
term_tuple(27,0,variable(28)). | ||
variable(28,"X"). |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
% auxiliary atoms | ||
max_index(T,M) :- M = #max{ P : term_tuple(T,P,_); -1 }, term_tuple(T,_,_). | ||
|
||
|
||
% when outermost function is not prev | ||
add_var(T,"T") :- literal(L,_,A), function(A,N,T), N!="prev". | ||
|
||
% definition of add_var | ||
new(term_tuple(T,M+1,variable(id(T)))) :- add_var(T,Var). | ||
new(variable(id(T),Var)) :- add_var(T,Var). | ||
|
||
|
||
% when outermost function is prev | ||
remove_prev(A,T,F) :- literal(L,_,A), function(A,"prev",T), | ||
term_tuple(T,0,function(F)), not term_tuple(T,1,_). | ||
|
||
replace(function(A,"prev",T),function(A,N,T')) :- remove_prev(A,T,F), function(F,N,T'). | ||
|
||
% add binary operator... | ||
new() :- remove_prev(A,T,F). | ||
|
Oops, something went wrong.