arg Arg Term Value functor Term Functor Arity

  • Slides: 76
Download presentation

Обработка на термове arg(? Arg, +Term, ? Value) functor(? Term, ? Functor, ? Arity)

Обработка на термове arg(? Arg, +Term, ? Value) functor(? Term, ? Functor, ? Arity)

5 ? - T=f(1, 2, g(X)), arg(2, T, V). T = f(1, 2, g(X))

5 ? - T=f(1, 2, g(X)), arg(2, T, V). T = f(1, 2, g(X)) V=2; No 6 ? - T=f(1, 2, g(X)), arg(3, T, V). T = f(1, 2, g(X)) V = g(X) Yes 7 ? - T=f(1, 2, g(X)), functor(T, F, N). T = f(1, 2, g(X)) F=f N=3 Yes 8 ? - functor(T, f, 3). T = f(_G 339, _G 340, _G 341) Yes

? - term_variables(a(X, b(Y, X), Z), L). L = [X, Y, Z] Yes 10

? - term_variables(a(X, b(Y, X), Z), L). L = [X, Y, Z] Yes 10 ? - term_variables(a(X, b(Y, a), X), L). L = [X, Y] Yes 11 ? - term_variables(sin(X), L). L = [X] Yes 12 ? - term_variables(sin(90), L). L = [] Yes 13 ? - term_variables(sin(log(X)), L). L = [X] Yes

f(1). f(2). f(3).

f(1). f(2). f(3).

18 ? - T=g(X, Y, 1), f(Z), setarg(1, T, Z). T = g(1, Y,

18 ? - T=g(X, Y, 1), f(Z), setarg(1, T, Z). T = g(1, Y, 1) Z=1; T = g(2, Y, 1) Z=2; T = g(3, Y, 1) Z=3; No

14 ? - T=f(X, Y, 1), setarg(1, T, 5). T = f(5, Y, 1)

14 ? - T=f(X, Y, 1), setarg(1, T, 5). T = f(5, Y, 1) Yes 15 ? - T=f(X, X, 1), setarg(1, T, 5). T = f(5, X, 1) Yes 16 ? - T=f(X, Y, 1), setarg(1, T, 5), setarg(2, T, 2). T = f(5, 2, 1) Yes

Изпълнение на цел Goal Times на брой пъти !!! (има проблем) succeeds_n_times(Goal, Times) :

Изпълнение на цел Goal Times на брой пъти !!! (има проблем) succeeds_n_times(Goal, Times) : Counter = counter(0), ( Goal, arg(1, Counter, N 0), N is N 0 + 1, nb_setarg(1, Counter, N), fail ; arg(1, Counter, Times) ).

7 ? - nonvar(X), X=5. No 8 ? - X=5, nonvar(X). X=5 Yes 3

7 ? - nonvar(X), X=5. No 8 ? - X=5, nonvar(X). X=5 Yes 3 ? - X=5, var(X). No 4 ? - var(14323). No 5 ? - var(fdhhgdsj). No 6 ? - nonvar(X). No

Пример 1 | ? - numbervars(term(X, Y, Z), 28, M). M = 31, X

Пример 1 | ? - numbervars(term(X, Y, Z), 28, M). M = 31, X = C 1, Y = D 1, Z = E 1 ?

Пример 1 a | ? - X=5, numbervars(term(X, Y, Z), 28, M). M =

Пример 1 a | ? - X=5, numbervars(term(X, Y, Z), 28, M). M = X = Y = Z = yes 30, 5, C 1, D 1 ?

Пример 1 b |? - X=5, numbervars(term(X, Y, Z), 0, M). M = X

Пример 1 b |? - X=5, numbervars(term(X, Y, Z), 0, M). M = X = Y = Z = yes 2, 5, A, B ?

Пример 1 c |? - X=5, Y=7, Z=2, numbervars(term(X, Y, Z), 0, M). M

Пример 1 c |? - X=5, Y=7, Z=2, numbervars(term(X, Y, Z), 0, M). M = X = Y = Z = yes 0, 5, 7, 2?

Семантична мрежа animal daylight active_at isa bird moving_method isa albatross isa black_&_white Albert isa

Семантична мрежа animal daylight active_at isa bird moving_method isa albatross isa black_&_white Albert isa Ross Kim mo vin brown g_m _at ive co kiwi colour act ur o l fly night eth od walk

isa(bird, animal). isa(albatross, bird). isa(kiwi, bird). isa(ross, albatross). isa(albert, albatross). isa(kim, kiwi). moving_method(bird, fly).

isa(bird, animal). isa(albatross, bird). isa(kiwi, bird). isa(ross, albatross). isa(albert, albatross). isa(kim, kiwi). moving_method(bird, fly). moving_method(kiwi, walk). active_at(bird, daylight). active_at(kiwi, night). colour(albatross, black_&_white). colour(kiwi, brown). moving_method(X, Method): isa(X, Super. X), moving_method(Super. X, Method).

fact(Fact): call(Fact), !. fact(Fact): Fact=. . [Rel, Arg 1, Arg 2], isa(Arg 1, Super.

fact(Fact): call(Fact), !. fact(Fact): Fact=. . [Rel, Arg 1, Arg 2], isa(Arg 1, Super. Arg), Super. Fact=. . [Rel, Super. Arg, Arg 2], fact(Super. Fact).

| ? - fact(moving_method(kim, Method)). Method = walk ? | ? - fact(moving_method(albert, Method)).

| ? - fact(moving_method(kim, Method)). Method = walk ? | ? - fact(moving_method(albert, Method)). Method = fly ?

Оператори : : : - op(900, fx, if). op(890, xfy, then). op(880, xfy, or).

Оператори : : : - op(900, fx, if). op(890, xfy, then). op(880, xfy, or). op(870, xfy, and). op(860, fx, not).

Правила if gives_milk(X) then mammal(X). if has_feathers(X) and lays_eggs(X) then bird(X). if has_gills(X) and

Правила if gives_milk(X) then mammal(X). if has_feathers(X) and lays_eggs(X) then bird(X). if has_gills(X) and lays_eggs(X) then fish(X). if bird(X) and not penguin(X) then flies(X). if fish(X) then lives(X, sea). if mammal(X) or fish(X) or bird(X) then homeothermic(X). if mammal(X) and can_speak(X) then human(X). if bird(X) and can_speak(X) then parrot(X).

: -op(800, xfy, loves). : -op(900, xfy, hates). john loves mary. peter loves ann.

: -op(800, xfy, loves). : -op(900, xfy, hates). john loves mary. peter loves ann. mary loves paul. john loves susan. ann loves john. mary loves peter. ann hates mary. peter hates paul. mary hates susan. X hates Y : - Y loves X, + X loves Y.

Ø listing(term). Ø : - dynamic <fact 1>/n 1 , <fact 2>/n 2, Ø

Ø listing(term). Ø : - dynamic <fact 1>/n 1 , <fact 2>/n 2, Ø assert(<fact> ). Ø assertz(<fact> ). Ø asserta(<fact> ). Ø retract(<fact>). Ø retractall(<fact>)

: -dynamic st/1. st(0). my_statistics: write('This program is executed '), st(N), write(' times. '),

: -dynamic st/1. st(0). my_statistics: write('This program is executed '), st(N), write(' times. '), nl. program(X): write('Value '), write(X), nl, st(N), M is N+1, retract(st(N)), assert(st(M)).

База от факти : - dynamic fact/1. fact(has_feathers(tweety)). fact(lays_eggs(tweety)). fact(gives_milk(milka)). fact(mammal(john)). fact(can_speak(john)).

База от факти : - dynamic fact/1. fact(has_feathers(tweety)). fact(lays_eggs(tweety)). fact(gives_milk(milka)). fact(mammal(john)). fact(can_speak(john)).

Прав извод (forward chaining) Правила if gives_milk(X) then mammal(X). if has_feathers(X) and lays_eggs(X)then bird(X).

Прав извод (forward chaining) Правила if gives_milk(X) then mammal(X). if has_feathers(X) and lays_eggs(X)then bird(X). if has_gills(X) and lays_eggs(X) then fish(X). if bird(X) and not penguin(X) then flies(X). . Факти fact(has_feathers(tweety)). fact(lays_eggs(tweety)). fact(gives_milk(milka)). . fact(gives_milk(X))

Прав извод (forward chaining) Правила if gives_milk(X) then mammal(X). if has_feathers(X) and fact(mammal(milka)) lays_eggs(X)then

Прав извод (forward chaining) Правила if gives_milk(X) then mammal(X). if has_feathers(X) and fact(mammal(milka)) lays_eggs(X)then bird(X). if has_gills(X) and lays_eggs(X) then fish(X). if bird(X) and not penguin(X) then flies(X). . Факти fact(has_feathers(tweety)). fact(lays_eggs(tweety)). fact(gives_milk(milka)). . fact(gives_milk(milka))

Прав извод (forward chaining) derive : derive_fact(P), write('Derived: '), write(P), nl, derive.

Прав извод (forward chaining) derive : derive_fact(P), write('Derived: '), write(P), nl, derive.

Прав извод (forward chaining) derive_fact(P) : if Condition then P, ok(Condition), + fact(P), assertz(fact(P)).

Прав извод (forward chaining) derive_fact(P) : if Condition then P, ok(Condition), + fact(P), assertz(fact(P)).

Прав извод (forward chaining) ok(P) : fact(P). ok(P and Q) : ok(P), ok(Q). ok(P

Прав извод (forward chaining) ok(P) : fact(P). ok(P and Q) : ok(P), ok(Q). ok(P or Q) : ok(P); ok(Q).

Прав извод | ? - derive. Derived: mammal(milka) Derived: bird(tweety) Derived: homeothermic(john) Derived: homeothermic(milka)

Прав извод | ? - derive. Derived: mammal(milka) Derived: bird(tweety) Derived: homeothermic(john) Derived: homeothermic(milka) Derived: homeothermic(tweety) Derived: human(john) yes

База от факти - черна дъска fact(has_feathers(tweety)). fact(lays_eggs(tweety)). fact(gives_milk(milka)). fact(mammal(john)). fact(can_speak(john)). fact(mammal(milka)). fact(bird(tweety)). fact(homeothermic(john)).

База от факти - черна дъска fact(has_feathers(tweety)). fact(lays_eggs(tweety)). fact(gives_milk(milka)). fact(mammal(john)). fact(can_speak(john)). fact(mammal(milka)). fact(bird(tweety)). fact(homeothermic(john)). fact(homeothermic(milka)). fact(homeothermic(tweety)). fact(human(john)).

? - X=5, write(X). 5 ? - X=5, write(x=X). x=5 ? - write('Hello World!').

? - X=5, write(X). 5 ? - X=5, write(x=X). x=5 ? - write('Hello World!'). Hello World! ? - X=5, Y=10, write(x=X), nl, write(y=Y). x=5 y=10 Yes

? - read(Z). |: 34. 67. Z = 34. 67 Yes ? - read(Z).

? - read(Z). |: 34. 67. Z = 34. 67 Yes ? - read(Z). |: Hello. Yes ? - read(Z). |: 'Hello!'. Z = 'Hello!' Yes ? - read(Z). |: hello. Z = hello Yes

? - read(Z). |: 1+2. Z = 1+2 Yes ? - X=5, Y=10, write(X+Y).

? - read(Z). |: 1+2. Z = 1+2 Yes ? - X=5, Y=10, write(X+Y). 5+10 X=5 Y = 10

summa: read(X), read(Y), Z is X+Y, write(summa=Z).

summa: read(X), read(Y), Z is X+Y, write(summa=Z).

Ø get_char(Stream, Char) Ø format(Text), format(Text, List). Ø nl(Stream)

Ø get_char(Stream, Char) Ø format(Text), format(Text, List). Ø nl(Stream)

3 ? - format('This is a ~w ~w', [bad, boy]). This is a bad

3 ? - format('This is a ~w ~w', [bad, boy]). This is a bad boy Yes 4 ? - format('This is a ~w ~w', [good, girl]). This is a good girl Yes 5 ? - format('This is a ~w ~w', [good, student]). This is a good student Yes

Ø Ø Ø open(Filename, read/write/append , Stream) current_input(Stream) current_output(Stream) set_input(Stream) set_output(Stream) close(Stream) Ø end_of_file

Ø Ø Ø open(Filename, read/write/append , Stream) current_input(Stream) current_output(Stream) set_input(Stream) set_output(Stream) close(Stream) Ø end_of_file

? -open(‘d: /temp/proba. txt’, read, S), set_input(S). ? -open(‘d: /temp/proba. txt’, write, S), set_output(S).

? -open(‘d: /temp/proba. txt’, read, S), set_input(S). ? -open(‘d: /temp/proba. txt’, write, S), set_output(S). ? -open(‘d: /temp/proba. txt’, append, S), set_output(S). ? -current_output(S), close(S). ? -current_input(S), close(S).

read_text(Filename): open(Filename, read, S), set_input(S), display_file(S), close(S). display_file(S): read(S, X), ( X = end_of_file

read_text(Filename): open(Filename, read, S), set_input(S), display_file(S), close(S). display_file(S): read(S, X), ( X = end_of_file -> nl ; write(X), nl, display_file(S)).

read_text(Filename, Text): open(Filename, read, S), set_input(S), read_string(S, Text), close(S).

read_text(Filename, Text): open(Filename, read, S), set_input(S), read_string(S, Text), close(S).

read_string(S, Text): get_char(S, Char), ( Char = end_of_file -> Text = '' ; name(Char,

read_string(S, Text): get_char(S, Char), ( Char = end_of_file -> Text = '' ; name(Char, [CCode]), read_string(S, Rest. Text), name(Rest. Text, TCode), name(Text, [CCode|TCode])).

write_text(Filename, Text): open(Filename, write, S), set_output(S), write(S, Text), close(S).

write_text(Filename, Text): open(Filename, write, S), set_output(S), write(S, Text), close(S).

write_list([ ]). write_list([ X | L]): write(X), nl, write_list(L).

write_list([ ]). write_list([ X | L]): write(X), nl, write_list(L).

write_text_lines(Filename, L): open(Filename, write, S), set_output(S), write_list(L), close(S).

write_text_lines(Filename, L): open(Filename, write, S), set_output(S), write_list(L), close(S).

file_words(Input_file, Output_file): read_text(Input_file, Text), tag_words(Text, Words. List), compress(Words. List, Out. List), write_text_lines(Output_file, Out. List).

file_words(Input_file, Output_file): read_text(Input_file, Text), tag_words(Text, Words. List), compress(Words. List, Out. List), write_text_lines(Output_file, Out. List).

count([ ], _, 0). count([X|L], X, N): count(L, X, K), N is K+1. count([Y|L],

count([ ], _, 0). count([X|L], X, N): count(L, X, K), N is K+1. count([Y|L], X, N): Y = X, count(L, X, N).

count_words([], []). count_words([X|Words], [ X/N |CW]): count([X|Words], X, N), del_all(X, Words, W 1), count_words(W

count_words([], []). count_words([X|Words], [ X/N |CW]): count([X|Words], X, N), del_all(X, Words, W 1), count_words(W 1, CW).

file_words_st(Input_file, Output_file): read_text(Input_file, Text), tag_words(Text, Words. List), count_words(Words. List, Res. List), sort(Res. List, Out.

file_words_st(Input_file, Output_file): read_text(Input_file, Text), tag_words(Text, Words. List), count_words(Words. List, Res. List), sort(Res. List, Out. List), write_text_lines(Output_file, Out. List). % сортирани по азбучен ред

count_words 1([], []). count_words 1([X|Words], [ N/X |CW]): count([X|Words], X, N), del_all(X, Words, W

count_words 1([], []). count_words 1([X|Words], [ N/X |CW]): count([X|Words], X, N), del_all(X, Words, W 1), count_words 1(W 1, CW).

file_words_st(Input_file, Output_file): read_text(Input_file, Text), tag_words(Text, Words. List), count_words 1(Words. List, Res. List), sort(Res. List,

file_words_st(Input_file, Output_file): read_text(Input_file, Text), tag_words(Text, Words. List), count_words 1(Words. List, Res. List), sort(Res. List, Out. List), write_text_lines(Output_file, Out. List). % сортирани по брой срещания

split(L, T, P, Res): append(Prev, L 2, L), append(T, Next, L 2), append(P, Next,

split(L, T, P, Res): append(Prev, L 2, L), append(T, Next, L 2), append(P, Next, Res 1), append(Prev, Res 1, Res). replace(Text, Old, New, Res): split(Text, Old, New, Temp. Res), !, replace(Temp. Res, Old, New, Res). replace(Text, Old, New, Text).

read_string(S, Text): get_char(S, Char), ( Char = end_of_file -> Text = '' ; name(Char,

read_string(S, Text): get_char(S, Char), ( Char = end_of_file -> Text = '' ; name(Char, [CCode]), read_string(S, Rest. Text), name(Rest. Text, TCode), name(Text, [CCode|TCode])).

exchange_words(File, Old. Word, New. Word): open(File, read, S), set_input(S), read_string(S, Text), close(S), name(Text, Code),

exchange_words(File, Old. Word, New. Word): open(File, read, S), set_input(S), read_string(S, Text), close(S), name(Text, Code), name(Old. Word, COW), name(New. Word, CNW), replace(Code, COW, CNW, Res), name(New. Text, Res), open(File, write, S 1), set_output(S 1), write(New. Text), close(S 1).