Está en la página 1de 50

Lenguajes de Programacin

Lenguajes lgicos: PROLOG


M.C Said Zamora

Lenguajes lgicos
Utilizan constructores lgicos para establecer relaciones.
tomos.

PROLOG
Hechos

Reglas

Base de conocimiento.

Bsquedas.

Base de conocimiento
mujer(ana).
mujer(juany).
mujer(yolanda).
tocaguitarra(juany).

fiesta.

Bsquedas
?- mujer(ana).
?- mujer(genoveva).
?- party.

Creacin de reglas.
feliz(yolanda).
escuchamusica(ana).
escuchamusica(yolanda):- feliz(yolanda).

Bsquedas
?- mujer(X).

Base de conocimiento
gusta(vicente,ana).
gusta(marsellus,ana).
gusta(calabaza,conejodechocolate).
gusta(conejodechocolate,calabaza).
celos(X,Y):- gusta(X,Z), gusta(Y,Z).

Archivos .pl
?- [kb2].
?- ['c:/Documents and Settings//Prolog/kb2.pl'].

?- listing.

tomos
String de caracteres.
Secuencia arbitraria de caracteres entre comillas simples
&^%&#@$ &*

String de caracteres especiales


@= ====> ; :-

Nmeros
Enteros

Flotantes

Variables
X , Y , Variable , _variables , X_526 , Lista , Lista24

Actividad

vINCENT
Footmassage
variable23
Variable2000
big_kahuna_burger
big kahuna burger
big kahuna burger
Jules
_Jules
_Jules

Unificacin
Dos trminos se unifican si son el mismo tomo
?- =(mia,mia).
?- =(mia,vincent).

Trminos complejos
Se construyen a partir de constantes, nmeros y variables.
Son llamados estructuras.
Es una funcin seguido de sus argumentos.

Trminos complejos
canta(juany)
busca(X,padre(padre(padre(ana))))

Aridad
Nmero de argumentos que posee un termino complejo.

mujer(ana)

Predicado
gusta(vincent,mia)
gusta(vincent,marsellus,mia)
gusta/2
gusta/3

Unificacin de trminos complejos


Pertenecen al mismo funtor.
Tienen la misma aridad.
Sus argumentos pueden ser unificados.
Las variables deben ser compatibles.

?- 2 = 2.
yes
?- mia = vincent.
No
?- 'mia' = mia.
yes

?- '2' = 2.
no
?- mia = X.

X = mia
yes

?- k(s(g),Y) = k(X,t(k)).
X = s(g)
Y = t(k)

?- father(X) = X.
X = father(father(father(father(father(father
(father(father(father(father(father(father
(father(father(father(father(father(father
(father(father(father(father(father(father
(father(father(father(father(father(father
X = father(father(father(father(...))))))))
yes

Recursividad
digiere(X,Y) :- hacomido(X,Y).
digiere(X,Y) :
hacomido(X,Z),

digiere(Z,Y).
hacomido(mosquito,sangre(cacerolo)).
hacomido(rana,mosquito).
hacomido(hacomido,rana).

Significado Declarativo y Procedimental


Lo que dice, lo que significa como declaraciones lgicas.
Si X digiere a Y, Prolog puede utilizar esta regla para preguntar si X se
ha comido a Y.

?- digiere(caiman,mosquito).
?- hacomido(caiman,mosquito).
Al unificar caimn con X y mosquito con Y
?- hacomido(caiman,Z),
digiere(Z,mosquito).

?- hacomido(caiman,Z).
?- digiere(Z,mosquito).
?- hacomido(caiman,rana).
?- digiere(rana,mosquito).

?- hacomido(rana,mosquito).

rbol genealgico
hijo(anne,bridget).
hijo(bridget,caroline).
hijo(caroline,donna).
hijo(donna,emily).
descendiente(X,Y) :- hijo(X,Y).
descendiente(X,Y) :- hijo(X,Z),

descendiente(Z,Y).

Listas
[mia, vincent, jules, yolanda]
[mia, ladron(honey_bunny), X, 2, mia]
[]
[mia, [vincent, jules], [butch, novia(butch)]]

[[], muerto(z), [2, [b, c]], [], Z, [2, [b, c]]]

Listas
?- [Head|Tail] = [mia, vincent, jules, yolanda].
Head = mia
Tail = [vincent,jules,yolanda]
yes
?- [X|Y] = [[], dead(z), [2, [b, c]], [], Z].

X = []
Y = [dead(z),[2,[b,c]],[],_7800]
Z = _7800
yes

Aritmtica
6+2=8
6 2 = 12
62=4
68=2
62=3
72=3

8 is 6+2.
12 is 6*2.
4 is 6-2.
-2 is 6-8.
3 is 6/2.
3 is 7/2.

Aritmtica y variables
?- 8 is 6+2.

yes
?- 12 is 6*2.
yes

?- X is 6*2.
X = 12
?- R is mod(7,2).
R=1

Predicado aritmtico
sumatresyduplica(X,Y) :- Y is (X+3)*2.
?- sumatresyduplica(1,X).
X=8

Lenguajes de programacin
Mercury
M.C Said Zamora

Lista: [1, 2, 3]
Tupl: {1, 2, "tres"}

Operadores
Especificadores
And xfy
Prioridad

Objetos
Declaraciones o clausulas
:- func
:- import_module
:- use_module
:- include_module
:- end_module

Mdulos

:- module queue.
:- interface.
%
:- type queue(T).
%
:- pred empty_queue(queue(T)).
:- mode empty_queue(out) is det.
:- mode empty_queue(in) is semidet.
:- pred put(queue(T), T, queue(T)).
:- mode put(in, in, out) is det.
:- pred get(queue(T), T, queue(T)).
:- mode get(in, out, out) is semidet.

Sub mdulos
:- module x.
:- interface.
:- include_module y.
:- end_module x.

Compilacin
mmc filename.m
mmc c filename.m
Module.o
Module.c

Interfaz de compilacin
mmc --make-short-int filename1.m filename2.m ...
mmc --make-priv-int filename1.m filename2.m ...
mmc --make-int filename1.m filename2.m ...
Mmc filename1.m filename2.m ..

Modos
Mapeo del estado inicial de los argumentos del predicado o los
argumentos y resultado de una funcin en base a este estado inicial.
rbol de valores instantneos
:- inst listskel == bound( [] ; [free | listskel] ).

:- inst maybeskel ---> no ; yes(ground).


:- inst maybeskel == bound(no ; yes(ground)).
:- mode in == ground >> ground.
:- mode out == free >> ground.

Lenguajes de Programacin
Godel
M.C Said Zamora

Mdulos: Declara todos los smbolos en ese mdulo.


Importar.

Smbolos
BASE: enumera los tipos del modulo.
CONSTRUCTOR: Se declara con su aridad para construir nuevos tipos desde una base.
Dia, Lista(Dia), Lista(Lista(Dia))
CONSTANT
FUNCTION:
PROPOSITION: Predicado sin argumentos.

PREDICATE: Su nombre se sigue del tipo de cada argumento.

BASE Guy.
CONSTANT Abraham, Isaac, Ishmael : Guy.
PREDICATE Father : Guy * Guy.
Father(Abraham, Isaac).
Father(Abraham, Ishmael).

BASE Guy.
CONSTANT Abraham, Isaac, Ishmael : Guy.
FUNCTION Father : Guy -> Guy.
Father(Isaac) => Abraham.
Father(Ishmael) => Abraham.

MODULE Nat.
BASE Nat.
CONSTANT Zero.
FUNCTION Succ: Nat -> Nat.
PREDICATE Plus: Nat * Nat * Nat;
IsZero: Nat.
IsZero(Zero).
Plus(Zero, x, x).
Plus(Succ(x), y, Succ(z)) <- Plus(x, y, z).

MODULE Nat.
BASE Nat.
CONSTANT Zero.
FUNCTION Succ: Nat -> Nat;
+ : yFx(510): Nat * Nat -> Nat.

PREDICATE IsZero: Nat.


IsZero(Zero).
Zero + x => x.
Succ(x) + y => Succ(x + y).

También podría gustarte