Skip to content

Commit

Permalink
Refactor to use flat ast fact representation.
Browse files Browse the repository at this point in the history
  • Loading branch information
namcsi committed Jun 14, 2023
1 parent b7393ea commit e1d63e2
Show file tree
Hide file tree
Showing 26 changed files with 868 additions and 328 deletions.
19 changes: 15 additions & 4 deletions src/renopro/asp/encodings/transform.lp
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.
1 change: 0 additions & 1 deletion src/renopro/asp/examples/transform/good_input.lp

This file was deleted.

1 change: 0 additions & 1 deletion src/renopro/asp/examples/transform/good_output.lp

This file was deleted.

5 changes: 0 additions & 5 deletions src/renopro/asp/examples/transform/good_transform.lp

This file was deleted.

16 changes: 0 additions & 16 deletions src/renopro/asp/examples/transform/robot_reified.lp

This file was deleted.

17 changes: 17 additions & 0 deletions src/renopro/asp/tests/reify/binary_operation.lp
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).
12 changes: 12 additions & 0 deletions src/renopro/asp/tests/reify/constant.lp
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).
22 changes: 22 additions & 0 deletions src/renopro/asp/tests/reify/function.lp
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).
14 changes: 14 additions & 0 deletions src/renopro/asp/tests/reify/nested_function.lp
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).
10 changes: 10 additions & 0 deletions src/renopro/asp/tests/reify/prop_fact.lp
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)).
18 changes: 18 additions & 0 deletions src/renopro/asp/tests/reify/prop_normal_rule.lp
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)).
12 changes: 12 additions & 0 deletions src/renopro/asp/tests/reify/string.lp
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").
22 changes: 22 additions & 0 deletions src/renopro/asp/tests/reify/variable.lp
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").
2 changes: 2 additions & 0 deletions src/renopro/asp/tests/transform/not_bad/input.lp
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")).
3 changes: 3 additions & 0 deletions src/renopro/asp/tests/transform/not_bad/output.lp
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")).
36 changes: 36 additions & 0 deletions src/renopro/asp/tests/transform/not_bad/transform.lp
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).
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).


30 changes: 30 additions & 0 deletions src/renopro/asp/tests/transform/robot_reified.lp
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").
27 changes: 27 additions & 0 deletions src/renopro/asp/tests/transform/robot_reified_flat.lp
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").
21 changes: 21 additions & 0 deletions src/renopro/asp/tests/transform/robot_transform_flat.lp
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).

Loading

0 comments on commit e1d63e2

Please sign in to comment.