%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%!TEX TS-program = latex
% !BIB program = biber	
%!TEX encoding = IsoLatin
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\documentclass[11pt]{article}
\usepackage[height=10in,a4paper,hmargin={2.5cm,0.8in}]{geometry}

\usepackage{fancyhdr}
\pagestyle{fancy}

\lhead{\tiny Programa de Posgrado en Lingüística}
\chead{\tiny\today}
\rhead{\tiny Universidad de Costa Rica}
\lfoot{\tiny Prof. Jorge Antonio Leoni de León}
\cfoot{\thepage}
\rfoot{\tiny SP--7767 Procesamiento del Lenguaje Natural}

\usepackage[spanish]{babel}

\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}	% Changement de polices par défault vers une avec 
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\usepackage[
    backend=biber,
    style=authoryear-icomp,
    %sortlocale=de_DE,
    natbib=true,
    url=false, 
    doi=true,
    eprint=false
]{biblatex}
\addbibresource{prologdocs.bib}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\usepackage{pifont}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\usepackage{rotating}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\usepackage[usenames,dvipsnames]{color}
\definecolor{darkgray}{rgb}{0.66, 0.66, 0.66}
\definecolor{trueblue}{rgb}{0.0, 0.45, 0.81}
\definecolor{ferrarired}{rgb}{1.0, 0.11, 0.0}
\definecolor{darkorchid}{rgb}{0.6, 0.2, 0.8}
\definecolor{darkelectricblue}{rgb}{0.33, 0.41, 0.47}
\definecolor{orange(colorwheel)}{rgb}{1.0, 0.5, 0.0}
\definecolor{applegreen}{rgb}{0.55, 0.71, 0.0}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\usepackage{listings}
\usepackage{array}
\usepackage{stix}
%\usepackage{alltt}

\usepackage{url}

\usepackage{qtree}
\usepackage{tree-dvips}
\usepackage{gb4e}

\author{
	Jorge Antonio Leoni de León\\
	Programa de Posgrado en Lingüística\\
	Universidad de Costa Rica\\
	San Pedro Montes de Oca, San José\\
	Costa Rica
}
\title{
	Las reglas {\tt miembro/2} y {\tt concatenar/3} en Prolog\\
	SP--7767 Procesamiento del Lenguaje Natural: Ejemplos	
}
\date{
		Fecha de creación: 23 de marzo de 2019\\
		Última modificación: \today\\
		Email: \url{antonio.leoni@ucr.ac.cr}\\
		URL: \url{http://www.mulkin.info}\\
}

\begin{document}

\maketitle

\listoffigures 
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\newpage

% La figura \ref{ejemplo:complemento:01} (página \pageref{ejemplo:complemento:01}) detalla el procesamiento de la gramática en (\ref{conc001}). Los subíndices en la figura \ref{ejemplo:complemento:01} se refieren a los números de línea en el código (\ref{conc001}).

Este documento ilustra los procesos de las reglas de membresía (\ref{miembro001}) y concatenación (\ref{conc001}) en Prolog. En el caso de la regla {\tt miembro/2}, es posible preguntar si un elemento {\tt X} forma parte de una lista {\tt Y}. Por ejemplo, en (\ref{miembro:ej:01}), la pregunta planteada es si el átomo ``{\tt tyrion}'' (primer argumento del predicado {\tt miembro/2}) forma parte de la lista que es el segundo elemento del predicado {\tt miembro/2}, hecho confirmado con la respuesta ``{\tt true}''.

\begin{exe} 
\ex {\bf Membresía en una lista}\label{miembro001}
\lstset{language=Prolog,numbers=left,basicstyle=\footnotesize\ttfamily}
\begin{lstlisting}[frame=single]
miembro(X , [X|_]).
miembro(X, [_|COLA]) :- miembro(X,COLA).
\end{lstlisting}
\end{exe}

\begin{exe} 
\ex{\bf Ejemplo de respuesta}\label{miembro:ej:01}
\lstset{language=Prolog,numbers=left,basicstyle=\footnotesize\ttfamily}
\begin{lstlisting}[frame=single]
?- member(tyrion,[tywin, tytos, joffrey,myrcella,tommen,tyrion,lancel]).
true 
\end{lstlisting}
\end{exe}

La regla {\tt concatenar/3} en (\ref{conc001}) fusiona dos listas, (\ref{conc:ejemplo:002}) y (\ref{conc:ejemplo:003}), en una tercera lista (\ref{conc:ejemplo:001}):

\begin{exe}
\ex {\bf Concatenación}\label{conc001}
\lstset{language=Prolog,numbers=left,basicstyle=\footnotesize\ttfamily}
\begin{lstlisting}[frame=single]
concatenar([],X,X).
concatenar([X|Y],Z,[X|W]) :- 
	concatenar(Y,Z,W).
\end{lstlisting}
\end{exe}

\begin{exe}
\ex\begin{xlist}
\ex {\bf Lista 1}: {\tt [targaryen,stark,lannister]}\label{conc:ejemplo:002}
\ex {\bf Lista 2}: {\tt [martell,tully,baratheon]}\label{conc:ejemplo:003}
\ex{\bf Respuesta de {\tt concatenar/3}}\label{conc:ejemplo:001}
\lstset{language=Prolog,numbers=left,basicstyle=\footnotesize\ttfamily}
\begin{lstlisting}[frame=single]
?- concatenar([targaryen,stark,lannister],[martell,tully,baratheon], R).
R = [targaryen, stark, lannister, martell, tully, baratheon].
\end{lstlisting}

\end{xlist}
\end{exe}



Los resultados del proceso de ejecución en el modo de funcionamiento {\em trace} están presentados en las figuras~\ref{miembro002} (página \pageref{miembro002}) y \ref{conc002} (página \pageref{conc002}); el detalle del procesamiento está ilustrado en las figuras~\ref{miembro003} (página \pageref{miembro003}) y \ref{conc003} (página \pageref{conc003}). La definición de estas reglas ha sido ampliamente comentada en la literatura de Prolog, una buena referencia son los clásicos \textcite[54--65]{ClocksinMellish03} y \textcite[58--60]{Sterling:1994:APA:175753}.
% \citep{ClocksinMellish03} \textcite{ClocksinMellish03} and \parencite{ClocksinMellish03} \cite{ClocksinMellish03}


%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\begin{sidewaysfigure}
\begin{lstlisting}[frame=single]
[trace]  ?- miembro(pablo,[antonio, diego, pablo, juri]).
   Call: (8) miembro(pablo, [antonio, diego, pablo, juri]) ? creep
   Call: (9) miembro(pablo, [diego, pablo, juri]) ? creep
   Call: (10) miembro(pablo, [pablo, juri]) ? creep
   Exit: (10) miembro(pablo, [pablo, juri]) ? creep
   Exit: (9) miembro(pablo, [diego, pablo, juri]) ? creep
   Exit: (8) miembro(pablo, [antonio, diego, pablo, juri]) ? creep
true .
\end{lstlisting}
\caption{Ejecución de {\tt miembro/2} en el modo {\tt trace}}\label{miembro002}
\end{sidewaysfigure}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

\begin{sidewaysfigure}
\begin{center}
\begin{tabular}{ >{\color{darkgray}\hspace{\fill}}l@{\color{darkgray}\hspace{.5cm}} >{\tt\footnotesize}l >{\tt\footnotesize}l >{\color{darkgray}\tt\footnotesize}l  @{\vspace{.2cm}\hspace{.5cm}} >{\color{darkgray}\tt\footnotesize}l @{\hspace{.5cm}} >{\color{darkelectricblue}\tt\footnotesize\hspace{\fill}}l }
\hline
0. & 
	&
		Input $\rightarrow$ &
			& 
				\node{000}miembro(pablo,[antonio, diego, pablo, juri]). &
			  		\\
\hline
1. & 
	{\color{applegreen}\ding{192}} &
		\node{001}miembro([X, [X|\_])\node{001a}. &
			{\color{ferrarired}\ding{192}}$\underrightarrow{\;2\;}${\color{applegreen}\ding{193}}&
				&
			  		\\
 & 
	&
		&
			{\color{ferrarired}\ding{194}}$\underrightarrow{\;2\;}${\color{applegreen}\ding{195}} &
				&
			  		\\
 & 
	&
		&
			{\color{orange(colorwheel)}\ding{196}} &
				&
			  		\\
2. & 
	{\color{applegreen}\ding{193}}{\color{applegreen}\ding{195}}  & 
		\node{002}miembro(X, [\_|COLA]) :-  & 
			{\color{trueblue}\ding{193}} &
				\node{002a}miembro(\node{002b}pablo, [\node{002c}antonio| diego, pablo, juri]) :-  & 
					\node{002cc}[antonio| diego, pablo, juri\node{002d}]
						\\
3. & 
	& 
		\hspace{2em}miembro(X,COLA).\node{003}. & 
			&
				\hspace{2em}miembro(pablo, [diego, pablo, juri])\node{003a}.{\color{trueblue}\ding{193}}$\underrightarrow{\;1\;}${\color{applegreen}\ding{194}} & 
					COLA=\node{003c}[diego, pablo, juri\node{003d}]
				\\
4. & 
	 &
		&
			{\color{trueblue}\ding{195}} & 
				\node{004}miembro(\node{004b}pablo, [diego| pablo, juri]\node{004c}) :- &  
					[diego|pablo, juri\node{004d}]
					\\
5. & 
	&
		&
			& 
				\hspace{2em}miembro(pablo, [pablo, juri])\node{005a}. {\color{trueblue}\ding{195}}$\underrightarrow{\;1\;}${\color{applegreen}\ding{196}}& 
					COLA=[pablo, juri\node{005d}]
						\\
6. &
	&
		&
			{\color{orange(colorwheel)}\ding{196}} & 
				\node{006}miembro(pablo, [pablo|juri])\node{006b}. & 
					\node{006c}[pablo| juri\node{006d}]
			  			\\
\hline
7. & 
	&
		Output $\rightarrow$ & 
			& 
				true\node{007}. &
			  		\\
\hline
\multicolumn{6}{l}{%
\tiny{\bf Clave de los colores:} 
	\hspace{\fill} Invocación de reglas (CALL): {\color{applegreen}\rule{1cm}{2pt}}%
	\hspace{\fill} Instanciación de variables y aplicación de reglas: {\color{trueblue}\rule{1cm}{2pt}}%
	\hspace{\fill }{\color{orange(colorwheel)}\rule{1cm}{2pt}}%
	\hspace{\fill } Regla fallida e invocación de la siguiente regla: {\color{ferrarired}\rule{1cm}{2pt}}%
	\hspace{\fill } Recuperación de la información (EXIT): {\color{darkorchid}\rule{1cm}{2pt}}%
	\hspace{\fill}%
}\\
\multicolumn{6}{m{24cm}}{%
\tiny{\bf Nota:}%
	``$\underrightarrow{\;1\;}$'' indica el número de línea de la siguiente regla invocada. %
	Por ejemplo, en ``$\underrightarrow{\;1\;}$'' la regla invocada es la de la línea 1. %
	Los números dentro de círculos señalan la secuencia de procesamiento: paso \ding{192}, paso \ding{193}, etc. %
	Así,  ``\ding{193}$\underrightarrow{\;1\;}$ \ding{194} '' indica que del paso \ding{193} al paso \ding{194} fue invocada la regla de la línea ``1''.%
}\\
\end{tabular}
\end{center}
{\color{applegreen}\makedash{6pt}\anodecurve[l]{000}[tr]{001}{.3in}}
{\color{ferrarired}\makedash{6pt}\anodecurve[bl]{001a}[tl]{002}{.5in}}
{\color{trueblue}\makedash{2pt}\anodecurve[r]{003}[l]{002a}{.3in}}
{\color{applegreen}\anodecurve[t]{003a}[tr]{001a}{.3in}}
{\color{ferrarired}\anodecurve[b]{001a}[t]{002}{.4in}}
{\color{trueblue}\anodecurve[b]{003}[l]{004}{.3in}}
{\color{applegreen}\makedash{3pt}\anodecurve[t]{005a}[r]{001a}{.75in}}
{\color{orange(colorwheel)}\makedash{3pt}\anodecurve[br]{001a}[l]{006}{.5in}}

{\color{darkorchid}\anodecurve[br]{006b}[tl]{006c}{.25in}}
{\color{darkorchid}\anodecurve[r]{006d}[r]{005d}{.25in}}
{\color{darkorchid}\anodecurve[r]{005d}[r]{004d}{.25in}}
{\color{darkorchid}\anodecurve[r]{004d}[r]{003d}{.25in}}
{\color{darkorchid}\anodecurve[r]{003d}[r]{002d}{.25in}}

{\makedash{3pt}\anodecurve[b]{002cc}[r]{007}{1.5in}}

\caption{\bf Procesamiento de {\tt miembro/2} en Prolog}\label{miembro003}
\end{sidewaysfigure}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\begin{sidewaysfigure}
\begin{lstlisting}[frame=single]
[trace]  ?- concatenar([antonio,diego],[pablo,juri],L).
   Call: (8) concatenar([antonio, diego], [pablo, juri], _6766) ? creep
   Call: (9) concatenar([diego], [pablo, juri], _7004) ? creep
   Call: (10) concatenar([], [pablo, juri], _7010) ? creep
   Exit: (10) concatenar([], [pablo, juri], [pablo, juri]) ? creep
   Exit: (9) concatenar([diego], [pablo, juri], [diego, pablo, juri]) ? creep
   Exit: (8) concatenar([antonio, diego], [pablo, juri], [antonio, diego, pablo, juri]) ? creep
L = [antonio, diego, pablo, juri].
\end{lstlisting}
\caption{Ejecución de {\tt concatenar/3} en el modo {\tt trace}}\label{conc002}
\end{sidewaysfigure}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

\begin{sidewaysfigure}
\begin{center}
\begin{tabular}{ >{\color{darkgray}\hspace{\fill}}l@{\color{darkgray}\hspace{.5cm}} >{\tt\footnotesize}l >{\tt\footnotesize}l >{\color{darkgray}\tt\footnotesize}l  @{\vspace{.2cm}\hspace{.5cm}} >{\color{darkgray}\tt\footnotesize}l @{\hspace{.5cm}} >{\color{darkelectricblue}\tt\footnotesize\hspace{\fill}}l }
\hline
0. & 
	&
		Input $\rightarrow$ &
			& 
				\node{000}concatenar([antonio, diego], [pablo, juri], L). &
			  		\\
\hline
1. & 
	{\color{applegreen}\ding{192}} &
		\node{001}concatenar([\hspace{1em}], X, X)\node{001a}. &
			{\color{ferrarired}\ding{192}}$\underrightarrow{\;2\;}${\color{applegreen}\ding{193}}&
				&
			  		\\
 & 
	&
		&
			{\color{ferrarired}\ding{194}}$\underrightarrow{\;2\;}${\color{applegreen}\ding{195}} &
				&
			  		\\
 & 
	&
		&
			{\color{orange(colorwheel)}\ding{196}} &
				&
			  		\\
2. & 
	{\color{applegreen}\ding{193}}{\color{applegreen}\ding{195}}  & 
		\node{002}concatenar([X|Y], Z, [X|W]) :-  & 
			{\color{trueblue}\ding{193}} &
				\node{002a}concatenar([\node{002b}antonio| diego], [pablo, juri], [\node{002c}antonio|W]) :-  & 
					\node{002cc}[antonio| diego, pablo, juri\node{002d}]
						\\
3. & 
	& 
		\hspace{2em}concatenar(Y , Z, W)\node{003}. & 
			&
				\hspace{2em}concatenar([diego],[pablo, juri], W)\node{003a}.{\color{trueblue}\ding{193}}$\underrightarrow{\;1\;}${\color{applegreen}\ding{194}} & 
					W=\node{003c}[diego, pablo, juri\node{003d}]
				\\
4. & 
	 &
		&
			{\color{trueblue}\ding{195}} & 
				\node{004}concatenar([\node{004b}diego|$\emptyset$], [pablo, juri], [\node{004c}diego|W]) :- &  
					[diego| pablo, juri\node{004d}]
					\\
5. & 
	&
		&
			& 
				\hspace{2em}concatenar([\hspace{1em}],[pablo, juri], W)\node{005a}. {\color{trueblue}\ding{195}}$\underrightarrow{\;1\;}${\color{applegreen}\ding{196}}& 
					W=[pablo, juri\node{005d}]
						\\
6. &
	&
		&
			{\color{orange(colorwheel)}\ding{196}} & 
				\node{006}concatenar([\hspace{1em}], [pablo,juri], [pablo,juri])\node{006b}. & 
					X=\node{006c}[pablo, juri\node{006d}]
			  			\\
\hline
7. & 
	&
		Output $\rightarrow$ & 
			& 
				L = [antonio, diego, pablo, juri]\node{007}. &
			  		\\
\hline
\multicolumn{6}{l}{%
\tiny{\bf Clave de los colores:} 
	\hspace{\fill} Invocación de reglas (CALL): {\color{applegreen}\rule{1cm}{2pt}}%
	\hspace{\fill} Instanciación de variables y aplicación de reglas: {\color{trueblue}\rule{1cm}{2pt}}%
	\hspace{\fill }{\color{orange(colorwheel)}\rule{1cm}{2pt}}%
	\hspace{\fill } Regla fallida e invocación de la siguiente regla: {\color{ferrarired}\rule{1cm}{2pt}}%
	\hspace{\fill } Recuperación de la información (EXIT): {\color{darkorchid}\rule{1cm}{2pt}}%
	\hspace{\fill}%
}\\
\multicolumn{6}{m{24cm}}{%
\tiny{\bf Nota:}%
	``$\underrightarrow{\;1\;}$'' indica el número de línea de la siguiente regla invocada. %
	Por ejemplo, en ``$\underrightarrow{\;1\;}$'' la regla invocada es la de la línea 1. %
	Los números dentro de círculos señalan la secuencia de procesamiento: paso \ding{192}, paso \ding{193}, etc. %
	Así,  ``\ding{193}$\underrightarrow{\;1\;}$ \ding{194} '' indica que del paso \ding{193} al paso \ding{194} fue invocada la regla de la línea ``1''.%
}\\
\end{tabular}
\end{center}
{\color{applegreen}\makedash{6pt}\anodecurve[l]{000}[tr]{001}{.3in}}
{\color{ferrarired}\makedash{6pt}\anodecurve[bl]{001a}[tl]{002}{.75in}}
{\color{trueblue}\makedash{2pt}\anodecurve[r]{003}[l]{002a}{.3in}}
{\color{applegreen}\anodecurve[t]{003a}[tr]{001a}{.3in}}
{\color{ferrarired}\anodecurve[b]{001a}[t]{002}{.4in}}
{\color{trueblue}\anodecurve[b]{003}[l]{004}{.3in}}
{\color{applegreen}\makedash{3pt}\anodecurve[t]{005a}[r]{001a}{.75in}}
{\color{orange(colorwheel)}\makedash{3pt}\anodecurve[br]{001a}[l]{006}{.5in}}

{\color{darkorchid}\anodecurve[br]{006b}[tl]{006c}{.25in}}
{\color{darkorchid}\anodecurve[r]{006d}[r]{005d}{.25in}}
{\color{darkorchid}\anodecurve[r]{005d}[r]{004d}{.25in}}
{\color{darkorchid}\anodecurve[r]{004d}[r]{003d}{.25in}}
{\color{darkorchid}\anodecurve[r]{003d}[r]{002d}{.25in}}

{\makedash{3pt}\anodecurve[b]{002cc}[r]{007}{1.5in}}

\caption{\bf Procesamiento de {\tt concatenar/3} en Prolog}\label{conc003}
\end{sidewaysfigure}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\newpage
\printbibliography
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\end{document}
