Some recent topics are noting the use of SSU as generators. Since I use generators often wanted to give generating with SSU a try.
When I converted some normal code that generates ASTs to SSU (no DCGs) only one result was generated for the SSU version.
As this was my first time trying SSU I don’t know if
- Generating ASTs is something that should be done with SSU?
- I did the conversion to SSU correctly?
Normal code (Click triangle to expand)
s(0,[]).
s(I,true) :-
I >= 1.
s(I,false) :-
I >= 1.
s(I0,if(T1,T2,T3)) :-
I0 > 1,
succ(I,I0),
s(I,T1),
s(I,T2),
s(I,T3).
Example run:
?- s(2,Ast).
Ast = true ;
Ast = false ;
Ast = if(true, true, true) ;
Ast = if(true, true, false) ;
Ast = if(true, false, true) ;
Ast = if(true, false, false) ;
Ast = if(false, true, true) ;
Ast = if(false, true, false) ;
Ast = if(false, false, true) ;
Ast = if(false, false, false) ;
false.
SSU Code (Click triangle to expand)
s_ssu(0,Ast) =>
Ast = [].
s_ssu(I,Ast) =>
I >= 1,
Ast = true.
s_ssu(I,Ast) =>
I >= 1,
Ast = false.
s_ssu(I0,Ast) =>
I0 > 1,
succ(I,I0),
s_ssu(I,T1),
s_ssu(I,T2),
s_ssu(I,T3),
Ast = if(T1,T2,T3).
Example run:
?- s_ssu(2,Ast).
Ast = true.
The SSU code is clearly acting like a cut exist for each clause which is not what is desired but I know from generating ASTs that cuts are to be avoided if the clauses are to seen as an OR condition with backtracking.