% Author:   Sallam
% http://sallamah.weebly.com
% Date: 5/5/2014

% ****--- read- write operations

mysum:-read(X), read(Y), Z is X+Y, write(Z).

% pretty print pp


/*
[1,2,3, [2,4], 7, 8]

         1
         2
         3
            2
            4
         7
         8
*/
pp(X):- pp(X,0).
pp([H|T], I):-!, J is I+3, pp(H,J), ppx(T, J).
pp(H,I):-space(I), write(H), nl.

ppx([],_).
ppx([H|T], I):- pp(H,I),ppx(T,I).

space(0):-!.
space(I):- write(' '), J is I-1, space(J).


/*spaces(0) :- !.
spaces(N) :- write(' '), N1 is N - 1, spaces(N1).

pp(X):- pp(X, 0).           % why we need this line

pp([H|T], I) :- !, J is I + 3, pp(H, J), ppx(T, J), nl.
pp(X, I) :- spaces(I), write(X), nl.

ppx([],_).                  % what this predicate should return?
ppx([H|T], I) :- pp(H, I), ppx(T, I). */


% ****---------------- write , write_canonical
% try ?- write(a+b*c*c), nl, write_canonical(a+b*c*c), nl.

% what happen with ?- write("Hello"). what about ?- write('Hello\n').

% ****--- characters

isO:- get_char(X), checkO(X).

checkO(X):- X='O'; X='o'.


checkO(_):- write('Opssss.....try again!'),isO.




/*isO :- write('Enter O:'), get_char(X), checkO(X).

checkO('o'):-!.
checkO('O'):-!.

checkO(_):- write('Opsssss, again ..\n'), isO. */

% check put_char(x).



% ***** files

read_file:- get_char(C), show(C).

show('end_of_file'):- !.
show(C):- put_char(C),read_file.

program:-
         open('d:\\a.txt',read, X ),
         current_input(S),
         set_input(X),
         read_file,
         set_input(S),
         close(X).
                 
                 


program(C):-
         open('d:\\b.txt',write, X ),
         current_output(S),
         set_output(X),
         put_char(C),
         set_output(S),
         close(X).
         
         
         
         

/*program :-
        open('c:\\a.txt',read, X),
        current_input(Stream),
        set_input(X),
        read_file,
        %consult(X),
        close(X),
        set_input(Stream).*/
        





  db:-consult('d:\\db').



%----------------------- Chapter 6

%?-consult(’\\john\\pl\\chat’).

% ?- consult(file1), consult(file2), consult(’fred.1’), consult(’bill.2’).
% ****List notation
% ?- [file1,file2,’fred.1’,’bill.2’].

%********************** Classifying Terms
 % ?- var(X).        succeedes if X uninstantiated

%  ?- var(23).

%  ?- X = Y, Y = 23, var(X).

%------- oppsite

% ?- nonvar(X).

% ?- atom (X).

% ?- number(X).

% ?- atomic (X).


%********************** Treating Clauses as Terms

%?- listing(show).
%?- clause(show(X), Y).

% ?- asserta(X).    add clause  e.g asserta(likes(ahmed, car)).
% ?- assertz(X).
% ?- retract(X).   remove clause added dynamically by assert

%************************ Constructing and Accessing Components of Structures

% ?- functor(f(a, b, g(Z)), F, N).

% ?- functor(a + b, F, N).

% ?- functor([a, b, c], F, N).

% ?- functor(apple, F, N).

% ?- functor([a, b, c], ’.’, 3).
% ?- functor([a, b, c], ., 2).

% ?- functor([a, b, c], a, Z).

% ---------- functor usage
copy(Old, New) :- functor(Old, F, N), functor(New, F, N).
% ?- copy(sentence(np(n(john)), v(eats)), X).

% ---------- arg
% ?- arg(2, related(john, mother(jane)), X).

% ?- arg(1, a+(b+c), X).

% ?- arg(2, [a,b,c], X).

% ?- arg(1, a+(b+c), b).

% ----------- univ ( =.. V)

%?- foo(a,b,c) =.. X.

%?- append([A|B], C, [A|D]) =.. L.

%?- [a,b,c,d] =.. L.

%?- (a+b) =.. L.

% -------------- atom level

%?- atom_chars(apple, X).
%?- number_chars(A,L).

% *************** Affecting Backtracking

% repeat. behave as rep above.

