How to identify wasteful representations of Prolog terms

What is a Prolog predicate that helps show a wasteful presentation of Prolog terms?


addition

In addition to Prolog SO's earlier answer, IIRC by mat , he used the Prolog predicate to parse the term Prolog and show how overly complex it was.

Especially for a term like

[op(add),[[number(0)],[op(add),[[number(1)],[number(1)]]]]]

it has shown that it matters to many [].

I searched my questions on the Prolog and looked through the answers twice, but still can not find them. I also remind you that this was not in the SWI Prolog, but in another Prolog, so instead of setting another Prolog, I was able to use the predicate with the online version of the Prolog.

If you read in the comments, you will see that the mat identified the post I was looking for.


What i was looking for

I have one last comment on the choice of representation . Please try the following using, for example, GNU Prolog or any other appropriate Prolog system:

| ? - write_canonical ([op (add), [Left, Right]]). 
'.' (op (add), '.' ('.' (_ 18, '.' (_ 19, [])), []))

This shows that this is a rather wasteful view and at the same time prevents the uniform processing of all the expressions you generate, combining several drawbacks.

, , Left+Right, , , , op_arguments(add, [Left,Right]), op_arguments(number, [1]) ..


, , , .

.

x + 0 + sin(y)

, AST

add(add(X,0),sin(Y))

AST, , . .: / , / , /AST-

, - , , , 3.30 , , .

, /2, , , ,

 add(add(X,0),sin(Y))

, /3, , , , " ",

 op(add,(op(add,X,0),op(sin,Y)))

, .

, , . , , , , .

, , , .


GNU Prolog tutorialspoint.com

:- initialization(main).
main :- write_canonical([op(add),[Left,Right]]).

sh-4.3$ gprolog --consult  file main.pg  
GNU Prolog 1.4.4 (64 bits)  
Compiled Aug 16 2014, 23:07:54 with gcc  
By Daniel Diaz  
Copyright (C) 1999-2013 Daniel Diaz  
compiling /home/cg/root/main.pg for byte code...  
/home/cg/root/main.pg:2warning: singleton variables [Left,Rightfor main/0  
/home/cg/root/main.pg compiled2 lines read - 524 bytes written, 9 ms  
'.'(op(add),'.'('.'(_39,'.'(_41,[])),[]))| ?-  

enter image description here


Prolog, :

?

, . , defaulty, spring , "default" "faulty". , " ", , . , - . ! .

+4
2

, :

fooobar.com/questions/1681458/...

write_canonical/1, .

, . . , , .

, SWI Prolog , SWI Prolog.

+4

, - , - ( );

single_element_list_subterms(Term, Index) :-
    Term =.. [Functor|Args],
    (   Args = []
    ->  Index = 0
    ;   maplist(single_element_list_subterms, Args, Indices),
        sum_list(Indices, SubIndex),
        (   Functor = '.', Args = [_, []]
        ->  Index is SubIndex + 1
        ;   Index = SubIndex
        )
    ).

:

| ?- single_element_list_subterms([op(add),[[number(0)],[op(add),[[number(1)],[number(1)]]]]], Count).

Count = 7

yes
| ?-

, 7 , . write_canonical:

| ?- write_canonical([op(add),[[number(0)],[op(add),[[number(1)],[number(1)]]]]]).
'.'(op(add),'.'('.'('.'(number(0),[]),'.'('.'(op(add),'.'('.'('.'(number(1),[]),'.'('.'(number(1),[]),[])),[])),[])),[]))

yes
| ?-
+1

Source: https://habr.com/ru/post/1681457/


All Articles