\documentclass{article}
\usepackage{graphicx} 
%% ODER: format ==         = "\mathrel{==}"
%% ODER: format /=         = "\neq "
%
%
\makeatletter
\@ifundefined{lhs2tex.lhs2tex.sty.read}%
  {\@namedef{lhs2tex.lhs2tex.sty.read}{}%
   \newcommand\SkipToFmtEnd{}%
   \newcommand\EndFmtInput{}%
   \long\def\SkipToFmtEnd#1\EndFmtInput{}%
  }\SkipToFmtEnd

\newcommand\ReadOnlyOnce[1]{\@ifundefined{#1}{\@namedef{#1}{}}\SkipToFmtEnd}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\DeclareFontFamily{OT1}{cmtex}{}
\DeclareFontShape{OT1}{cmtex}{m}{n}
  {<5><6><7><8>cmtex8
   <9>cmtex9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmtex10}{}
\DeclareFontShape{OT1}{cmtex}{m}{it}
  {<-> ssub * cmtt/m/it}{}
\newcommand{\texfamily}{\fontfamily{cmtex}\selectfont}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
  {<5><6><7><8>cmtt8
   <9>cmbtt9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmbtt10}{}
\DeclareFontShape{OT1}{cmtex}{bx}{n}
  {<-> ssub * cmtt/bx/n}{}
\newcommand{\tex}[1]{\text{\texfamily#1}}	% NEU

\newcommand{\Sp}{\hskip.33334em\relax}


\newcommand{\Conid}[1]{\mathit{#1}}
\newcommand{\Varid}[1]{\mathit{#1}}
\newcommand{\anonymous}{\kern0.06em \vbox{\hrule\@width.5em}}
\newcommand{\plus}{\mathbin{+\!\!\!+}}
\newcommand{\bind}{\mathbin{>\!\!\!>\mkern-6.7mu=}}
\newcommand{\rbind}{\mathbin{=\mkern-6.7mu<\!\!\!<}}% suggested by Neil Mitchell
\newcommand{\sequ}{\mathbin{>\!\!\!>}}
\renewcommand{\leq}{\leqslant}
\renewcommand{\geq}{\geqslant}
\usepackage{polytable}

%mathindent has to be defined
\@ifundefined{mathindent}%
  {\newdimen\mathindent\mathindent\leftmargini}%
  {}%

\def\resethooks{%
  \global\let\SaveRestoreHook\empty
  \global\let\ColumnHook\empty}
\newcommand*{\savecolumns}[1][default]%
  {\g@addto@macro\SaveRestoreHook{\savecolumns[#1]}}
\newcommand*{\restorecolumns}[1][default]%
  {\g@addto@macro\SaveRestoreHook{\restorecolumns[#1]}}
\newcommand*{\aligncolumn}[2]%
  {\g@addto@macro\ColumnHook{\column{#1}{#2}}}

\resethooks

\newcommand{\onelinecommentchars}{\quad-{}- }
\newcommand{\commentbeginchars}{\enskip\{-}
\newcommand{\commentendchars}{-\}\enskip}

\newcommand{\visiblecomments}{%
  \let\onelinecomment=\onelinecommentchars
  \let\commentbegin=\commentbeginchars
  \let\commentend=\commentendchars}

\newcommand{\invisiblecomments}{%
  \let\onelinecomment=\empty
  \let\commentbegin=\empty
  \let\commentend=\empty}

\visiblecomments

\newlength{\blanklineskip}
\setlength{\blanklineskip}{0.66084ex}

\newcommand{\hsindent}[1]{\quad}% default is fixed indentation
\let\hspre\empty
\let\hspost\empty
\newcommand{\NB}{\textbf{NB}}
\newcommand{\Todo}[1]{$\langle$\textbf{To do:}~#1$\rangle$}

\EndFmtInput
\makeatother
%

\title{Narrowing: The Scope of this Project}
\author{Steven Libby}

\begin{document}
\maketitle

This project implements a narrowing strategy, specifically the Needed Narrowing strategy \cite{Antoy} for Term Rewriting.
This implementation is correct and relatively efficient.  It can be used to form a simple functional logic DSL inside of Haskell.
The scope of this project is limited to implementing this strategy.  There are many features in PAKCS that
are extensions to the basic idea of Narrowing, and they are not covered.  As such, the resulting DSL 
is not convenient, but it is functional.  Further development of this system into a real language, or integration with Funlog,
are possible future projects, although integration with Funlog would require modification of the Funlog parser.

\section{Term}

Terms are similar to the terms that we covered in the first order logic example, but there are a few additions that I've had to make.
The first is that I've made a distinction between a constructor and a function.  This is because the theory of narrowing,
or at least the part I'm familiar with, is built around constructor based systems.
In this case, a term will be in a normal form when it has only constructors and variables,
or there are no more rewrite rules to apply.  The first case is actually a subset of the second case,
but the first case gives us a clear stopping point when narrowing.
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{module}\;\Conid{Term}\;\mathbf{where}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{import}\;\Conid{Subst}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{type}\;\Conid{Name}\mathrel{=}\Conid{String}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

To get anywhere with narrowing we need to start with a term.
A term $t$ is inductively defined as either
\begin{itemize}
  \item a constructor with $0$ or more subterms
  \item a function with $0$ or more subterms
  \item a variable
\end{itemize}
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{16}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{data}\;\Conid{Term}\;\Varid{v}{}\<[16]%
\>[16]{}\mathrel{=}\Conid{Con}\;\Conid{Name}\;[\mskip1.5mu \Conid{Term}\;\Varid{v}\mskip1.5mu]{}\<[E]%
\\
\>[16]{}\mid \Conid{Fun}\;\Conid{Name}\;[\mskip1.5mu \Conid{Term}\;\Varid{v}\mskip1.5mu]{}\<[E]%
\\
\>[16]{}\mid \Conid{Var}\;\Varid{v}\;\mathbf{deriving}\;(\Conid{Show},\Conid{Eq},\Conid{Ord}){}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{type}\;\Conid{Pos}\mathrel{=}[\mskip1.5mu \Conid{Int}\mskip1.5mu]{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

There are a few important points about terms.  Let $t$ be a term.
The arity of $t$ is the number of direct subterms it has.  In our case this is the length of the subterm list.
A position $p$ in $t$ is a sequence of integers that determine a subterm of $t$.
The subterm of $t$ determined by $p$ (written $t|_p$) is a path down the tree where we take the subtree at position $p$.

So if $t$ is the tree of $f x : map f xs$

\includegraphics[scale=.5]{figure1}
\\

and $p$ is the path $[2,1]$ 
Then $t|_p$ is $f$.
Finally if $t$ is a term, $p$ a position and $s$ a term then $t[s]_p$ is the result of replacing $t|_p$ with $s$.
so $t[id]_p$ would result in.
\\

\includegraphics[scale=.5]{figure3}
\\

A rewrite rule is a pair of terms where the left term is rooted with a function.
We write $l \rightarrow r$ to refer to the rule.
The idea is that if we have $l \rightarrow r$ and $t|p = l$ for some $p$ then we could rewrite that to $t[r]_p$

The general problem of term rewriting is difficult.  In fact in many systems equality of terms is undecidable.
To get around this problem, we limit ourselves to left linear, non overlapping, constructor based systems.
In practice this amounts to making sure that variables in the left hand side are distinct, and the left hand
side of a rule can't unify with any subterm of the left hand side of another rule.
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{17}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{data}\;\Conid{Rule}\;\Varid{v}{}\<[17]%
\>[17]{}\mathrel{=}\Conid{Term}\;\Varid{v}\mathbin{:=>}\Conid{Term}\;\Varid{v}\mid \Conid{E}\;\mathbf{deriving}\;(\Conid{Show},\Conid{Eq},\Conid{Ord}){}\<[E]%
\\
\>[3]{}\mathbf{type}\;\Conid{Program}{}\<[17]%
\>[17]{}\mathrel{=}[\mskip1.5mu \Conid{Rule}\;\Conid{Name}\mskip1.5mu]{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

In order to create the structures needed to narrow a term we need three different equivalence relations.
The first is the usual definition of equality given in Haskell.
The second is equality up to variables.
That means two terms are equal if renaming variables in the first term produces the second term.
The third is equality of the function and constructor symbols.  This is just used for grouping differnt rules together.
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{14}{@{}>{\hspre}l<{\hspost}@{}}%
\column{28}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}(\mathbin{\char126 =})\mathbin{::}(\Conid{Eq}\;\Varid{v})\Rightarrow \Conid{Term}\;\Varid{v}\to \Conid{Term}\;\Varid{v}\to \Conid{Bool}{}\<[E]%
\\
\>[3]{}(\Conid{Con}\;\Varid{c}\;\Varid{ts})\mathbin{\char126 =}(\Conid{Con}\;\Varid{d}\;\Varid{us})\mathrel{=}\Varid{c}\equiv \Varid{d}\mathrel{\wedge}\Varid{and}\;(\Varid{zipWith}\;(\mathbin{\char126 =})\;\Varid{ts}\;\Varid{us}){}\<[E]%
\\
\>[3]{}(\Conid{Fun}\;\Varid{f}\;\Varid{ts})\mathbin{\char126 =}(\Conid{Fun}\;\Varid{g}\;\Varid{us})\mathrel{=}\Varid{f}\equiv \Varid{g}\mathrel{\wedge}\Varid{and}\;(\Varid{zipWith}\;(\mathbin{\char126 =})\;\Varid{ts}\;\Varid{us}){}\<[E]%
\\
\>[3]{}(\Conid{Var}\;\anonymous ){}\<[14]%
\>[14]{}\mathbin{\char126 =}(\Conid{Var}\;\anonymous ){}\<[28]%
\>[28]{}\mathrel{=}\Conid{True}{}\<[E]%
\\
\>[3]{}\anonymous {}\<[14]%
\>[14]{}\mathbin{\char126 =}\anonymous {}\<[28]%
\>[28]{}\mathrel{=}\Conid{False}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{14}{@{}>{\hspre}l<{\hspost}@{}}%
\column{27}{@{}>{\hspre}l<{\hspost}@{}}%
\column{28}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}(\mathbin{\char126 \char126 }){}\<[27]%
\>[27]{}\mathbin{::}(\Conid{Eq}\;\Varid{v})\Rightarrow \Conid{Term}\;\Varid{v}\to \Conid{Term}\;\Varid{v}\to \Conid{Bool}{}\<[E]%
\\
\>[3]{}(\Conid{Con}\;\Varid{c}\;\anonymous ){}\<[14]%
\>[14]{}\mathbin{\char126 \char126 }(\Conid{Con}\;\Varid{d}\;\anonymous ){}\<[28]%
\>[28]{}\mathrel{=}\Varid{c}\equiv \Varid{d}{}\<[E]%
\\
\>[3]{}(\Conid{Fun}\;\Varid{f}\;\anonymous ){}\<[14]%
\>[14]{}\mathbin{\char126 \char126 }(\Conid{Fun}\;\Varid{g}\;\anonymous ){}\<[28]%
\>[28]{}\mathrel{=}\Varid{f}\equiv \Varid{g}{}\<[E]%
\\
\>[3]{}(\Conid{Var}\;\Varid{v}){}\<[14]%
\>[14]{}\mathbin{\char126 \char126 }(\Conid{Var}\;\Varid{w}){}\<[28]%
\>[28]{}\mathrel{=}\Varid{v}\equiv \Varid{w}{}\<[E]%
\\
\>[3]{}\anonymous {}\<[14]%
\>[14]{}\mathbin{\char126 \char126 }\anonymous {}\<[28]%
\>[28]{}\mathrel{=}\Conid{False}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

The following function are all simple helper functions.
Note that $arity$ and $!>$ correspond to the notion of arity and position defined above.
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{20}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{isCon}\;(\Conid{Con}\;\anonymous \;\anonymous ){}\<[20]%
\>[20]{}\mathrel{=}\Conid{True}{}\<[E]%
\\
\>[3]{}\Varid{isCon}\;\anonymous {}\<[20]%
\>[20]{}\mathrel{=}\Conid{False}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{20}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{isFun}\;(\Conid{Fun}\;\anonymous \;\anonymous ){}\<[20]%
\>[20]{}\mathrel{=}\Conid{True}{}\<[E]%
\\
\>[3]{}\Varid{isFun}\;\anonymous {}\<[20]%
\>[20]{}\mathrel{=}\Conid{False}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{18}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{isVar}\;(\Conid{Var}\;\anonymous ){}\<[18]%
\>[18]{}\mathrel{=}\Conid{True}{}\<[E]%
\\
\>[3]{}\Varid{isVar}\;\anonymous {}\<[18]%
\>[18]{}\mathrel{=}\Conid{False}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{14}{@{}>{\hspre}l<{\hspost}@{}}%
\column{23}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{x}{}\<[14]%
\>[14]{}\mathbin{!>}[\mskip1.5mu \mskip1.5mu]{}\<[23]%
\>[23]{}\mathrel{=}\Varid{x}{}\<[E]%
\\
\>[3]{}(\Conid{Con}\;\anonymous \;\Varid{cs})\mathbin{!>}(\Varid{h}\mathbin{:}\Varid{t})\mathrel{=}(\Varid{cs}\mathbin{!!}\Varid{h})\mathbin{!>}\Varid{t}{}\<[E]%
\\
\>[3]{}(\Conid{Fun}\;\anonymous \;\Varid{fs})\mathbin{!>}(\Varid{h}\mathbin{:}\Varid{t})\mathrel{=}(\Varid{fs}\mathbin{!!}\Varid{h})\mathbin{!>}\Varid{t}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{10}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{left}\;{}\<[10]%
\>[10]{}(\Varid{l}\mathbin{:=>}\anonymous )\mathrel{=}\Varid{l}{}\<[E]%
\\
\>[3]{}\Varid{right}\;{}\<[10]%
\>[10]{}(\anonymous \mathbin{:=>}\Varid{r})\mathrel{=}\Varid{r}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{21}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{arity}{}\<[21]%
\>[21]{}\mathbin{::}\Conid{Term}\;\Varid{a}\to \Conid{Int}{}\<[E]%
\\
\>[3]{}\Varid{arity}\;(\Conid{Var}\;\anonymous ){}\<[21]%
\>[21]{}\mathrel{=}\mathrm{0}{}\<[E]%
\\
\>[3]{}\Varid{arity}\;(\Conid{Fun}\;\anonymous \;\Varid{ts}){}\<[21]%
\>[21]{}\mathrel{=}\Varid{length}\;\Varid{ts}{}\<[E]%
\\
\>[3]{}\Varid{arity}\;(\Conid{Con}\;\anonymous \;\Varid{ts}){}\<[21]%
\>[21]{}\mathrel{=}\Varid{length}\;\Varid{ts}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

$variables$ gets a list of variables occurring in the term.  I know, shocking.
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{25}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{variables}{}\<[25]%
\>[25]{}\mathbin{::}\Conid{Term}\;\Varid{v}\to [\mskip1.5mu \Conid{Term}\;\Varid{v}\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{variables}\;(\Conid{Var}\;\Varid{v}){}\<[25]%
\>[25]{}\mathrel{=}[\mskip1.5mu \Conid{Var}\;\Varid{v}\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{variables}\;(\Conid{Con}\;\Varid{c}\;\Varid{ts}){}\<[25]%
\>[25]{}\mathrel{=}[\mskip1.5mu \Varid{v}\mid \Varid{t}\leftarrow \Varid{ts},\Varid{v}\leftarrow \Varid{variables}\;\Varid{t}\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{variables}\;(\Conid{Fun}\;\Varid{f}\;\Varid{ts}){}\<[25]%
\>[25]{}\mathrel{=}[\mskip1.5mu \Varid{v}\mid \Varid{t}\leftarrow \Varid{ts},\Varid{v}\leftarrow \Varid{variables}\;\Varid{t}\mskip1.5mu]{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

Finally we come to variable substitution.
We handle this using a monad, because the syntax is similar to how this is normally written.
If $t$ is a term and $\sigma$ is a substitution then $t >>= (\sigma !) \equiv t \sigma$ is the application of $\sigma$ to $t$.
A common pattern is $(t !> p) >>= (\sigma!)$ and this is read as "take the subterm $t|_p$ and apply $\sigma$ to it.
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{4}{@{}>{\hspre}l<{\hspost}@{}}%
\column{16}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{instance}\;\Conid{Monad}\;\Conid{Term}\;\mathbf{where}{}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\Varid{return}\;\Varid{v}\mathrel{=}\Conid{Var}\;\Varid{v}{}\<[E]%
\\[\blanklineskip]%
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}(\Conid{Var}\;\Varid{v}){}\<[16]%
\>[16]{}\bind \Varid{s}\mathrel{=}\Varid{s}\;\Varid{v}{}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}(\Conid{Fun}\;\Varid{f}\;\Varid{ts}){}\<[16]%
\>[16]{}\bind \Varid{s}\mathrel{=}\Conid{Fun}\;\Varid{f}\;(\Varid{map}\;(\bind \Varid{s})\;\Varid{ts}){}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}(\Conid{Con}\;\Varid{c}\;\Varid{ts}){}\<[16]%
\>[16]{}\bind \Varid{s}\mathrel{=}\Conid{Con}\;\Varid{c}\;(\Varid{map}\;(\bind \Varid{s})\;\Varid{ts}){}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
\subsection{Substitution}

The substitution is very similar to the one used in class for first order logic.
The only difference is that I've changed the type from a function to a $Map$.
This has two advantages.  The first is that lookups in a $Map$ are faster than a function,
although given the inefficiencies in the $pdt$ construction the value here is debatable.
The second, and much more important, advantage is that a Map can be printed out.
This was indispensable for debugging.
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{module}\;\Conid{Subst}\;(\Conid{Subst},\Varid{emptySubst},(\mathbin{|=>}),(\mathbin{|->}),(\mathbin{|/->}),(\mathbin{!}),\Varid{chain})\;\mathbf{where}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{import}\;\Varid{qualified}\;\Conid{\Conid{Data}.\Conid{Map}.Lazy}\;\Varid{as}\;\Conid{M}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{type}\;\Conid{Subst}\;\Varid{v}\;\Varid{m}\mathrel{=}\Conid{\Conid{M}.Map}\;\Varid{v}\;(\Varid{m}\;\Varid{v}){}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

We need a simple function for getting values out of our substitution.
This is normally written as $\sigma(v)$, but we use the notation $\sigma ! v$.
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{4}{@{}>{\hspre}l<{\hspost}@{}}%
\column{20}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}(\mathbin{!})\mathbin{::}(\Conid{Ord}\;\Varid{v},\Conid{Monad}\;\Varid{m})\Rightarrow \Conid{Subst}\;\Varid{v}\;\Varid{m}\to \Varid{v}\to \Varid{m}\;\Varid{v}{}\<[E]%
\\
\>[3]{}\Varid{s}\mathbin{!}\Varid{v}{}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mid \Varid{\Conid{M}.member}\;\Varid{v}\;\Varid{s}{}\<[20]%
\>[20]{}\mathrel{=}\Varid{s}\Conid{M}.\mathbin{!}\Varid{v}{}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mid \Varid{otherwise}{}\<[20]%
\>[20]{}\mathrel{=}\Varid{return}\;\Varid{v}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

The following functions are equivalent to the definitions in the first order logic example.
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{emptySubst}\mathbin{::}\Conid{Monad}\;\Varid{m}\Rightarrow \Conid{Subst}\;\Varid{v}\;\Varid{m}{}\<[E]%
\\
\>[3]{}\Varid{emptySubst}\mathrel{=}\Varid{\Conid{M}.empty}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{12}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}(\mathbin{|->}){}\<[12]%
\>[12]{}\mathbin{::}(\Conid{Ord}\;\Varid{v},\Conid{Monad}\;\Varid{m})\Rightarrow \Varid{v}\to \Varid{m}\;\Varid{v}\to \Conid{Subst}\;\Varid{v}\;\Varid{m}{}\<[E]%
\\
\>[3]{}\Varid{v}\mathbin{|->}\Varid{t}{}\<[12]%
\>[12]{}\mathrel{=}\Varid{\Conid{M}.singleton}\;\Varid{v}\;\Varid{t}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{14}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}(\mathbin{|=>}){}\<[14]%
\>[14]{}\mathbin{::}(\Conid{Ord}\;\Varid{v},\Conid{Monad}\;\Varid{m})\Rightarrow \Conid{Subst}\;\Varid{v}\;\Varid{m}\to \Conid{Subst}\;\Varid{v}\;\Varid{m}\to \Conid{Subst}\;\Varid{v}\;\Varid{m}{}\<[E]%
\\
\>[3]{}\Varid{s1}\mathbin{|=>}\Varid{s2}{}\<[14]%
\>[14]{}\mathrel{=}\Varid{\Conid{M}.union}\;\Varid{s1}\;\Varid{s2}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{13}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{chain}{}\<[13]%
\>[13]{}\mathbin{::}(\Conid{Ord}\;\Varid{v},\Conid{Monad}\;\Varid{m})\Rightarrow [\mskip1.5mu \Conid{Subst}\;\Varid{v}\;\Varid{m}\mskip1.5mu]\to \Conid{Subst}\;\Varid{v}\;\Varid{m}{}\<[E]%
\\
\>[3]{}\Varid{chain}\;\Varid{ss}{}\<[13]%
\>[13]{}\mathrel{=}\Varid{foldr}\;(\mathbin{|=>})\;\Varid{emptySubst}\;\Varid{ss}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{13}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}(\mathbin{|/->}){}\<[13]%
\>[13]{}\mathbin{::}(\Conid{Ord}\;\Varid{v},\Conid{Monad}\;\Varid{m})\Rightarrow \Varid{v}\to \Conid{Subst}\;\Varid{v}\;\Varid{m}\to \Conid{Subst}\;\Varid{v}\;\Varid{m}{}\<[E]%
\\
\>[3]{}\Varid{v}\mathbin{|/->}\Varid{s}{}\<[13]%
\>[13]{}\mathrel{=}\Varid{\Conid{M}.delete}\;\Varid{v}\;\Varid{s}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
\section{unification}
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{module}\;\Conid{Unify}\;\mathbf{where}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{import}\;\Conid{Term}{}\<[E]%
\\
\>[3]{}\mathbf{import}\;\Conid{Subst}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

This is nearly identical to the unification algorithm in the first order logic example.
I actually did some of my own work I swear.
Two terms $t$ and $t'$ unify if there exists a substitution $\sigma$ such that $t \sigma = t'$.
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{4}{@{}>{\hspre}l<{\hspost}@{}}%
\column{5}{@{}>{\hspre}l<{\hspost}@{}}%
\column{30}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{occurs}\mathbin{::}(\Conid{Eq}\;\Varid{v}){}\<[30]%
\>[30]{}\Rightarrow \Varid{v}\to \Conid{Term}\;\Varid{v}\to \Conid{Maybe}\;(){}\<[E]%
\\
\>[3]{}\Varid{occurs}\;\Varid{v}\;\Varid{t}{}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mid \Varid{any}\;\Varid{hasV}\;(\Varid{variables}\;\Varid{t}){}\<[30]%
\>[30]{}\mathrel{=}\Conid{Nothing}{}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mid \Varid{otherwise}{}\<[30]%
\>[30]{}\mathrel{=}\Conid{Just}\;(){}\<[E]%
\\
\>[4]{}\hsindent{1}{}\<[5]%
\>[5]{}\mathbf{where}\;\Varid{hasV}\;(\Conid{Var}\;\Varid{u}){}\<[30]%
\>[30]{}\mathrel{=}\Varid{v}\equiv \Varid{u}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{10}{@{}>{\hspre}l<{\hspost}@{}}%
\column{22}{@{}>{\hspre}l<{\hspost}@{}}%
\column{34}{@{}>{\hspre}l<{\hspost}@{}}%
\column{44}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{unify}{}\<[10]%
\>[10]{}\mathbin{::}(\Conid{Show}\;\Varid{v},\Conid{Ord}\;\Varid{v}){}\<[44]%
\>[44]{}\Rightarrow \Conid{Term}\;\Varid{v}\to \Conid{Term}\;\Varid{v}\to \Conid{Maybe}\;(\Conid{Subst}\;\Varid{v}\;\Conid{Term}){}\<[E]%
\\
\>[3]{}\Varid{unify}\;{}\<[10]%
\>[10]{}(\Conid{Var}\;\Varid{v})\;{}\<[22]%
\>[22]{}(\Conid{Var}\;\Varid{u}){}\<[34]%
\>[34]{}\mid \Varid{u}\equiv \Varid{v}{}\<[44]%
\>[44]{}\mathrel{=}\Varid{return}\;\Varid{emptySubst}{}\<[E]%
\\
\>[3]{}\Varid{unify}\;{}\<[10]%
\>[10]{}(\Conid{Var}\;\Varid{v})\;{}\<[22]%
\>[22]{}\Varid{y}{}\<[44]%
\>[44]{}\mathrel{=}\Varid{occurs}\;\Varid{v}\;\Varid{y}\sequ \Varid{return}\;(\Varid{v}\mathbin{|->}\Varid{y}){}\<[E]%
\\
\>[3]{}\Varid{unify}\;{}\<[10]%
\>[10]{}\Varid{x}\;{}\<[22]%
\>[22]{}(\Conid{Var}\;\Varid{v}){}\<[44]%
\>[44]{}\mathrel{=}\Varid{occurs}\;\Varid{v}\;\Varid{x}\sequ \Varid{return}\;(\Varid{v}\mathbin{|->}\Varid{x}){}\<[E]%
\\
\>[3]{}\Varid{unify}\;{}\<[10]%
\>[10]{}(\Conid{Fun}\;\Varid{f}\;\Varid{ts})\;{}\<[22]%
\>[22]{}(\Conid{Fun}\;\Varid{g}\;\Varid{ss}){}\<[34]%
\>[34]{}\mid \Varid{f}\equiv \Varid{g}{}\<[44]%
\>[44]{}\mathrel{=}\Varid{unifyLists}\;\Varid{ts}\;\Varid{ss}{}\<[E]%
\\
\>[3]{}\Varid{unify}\;{}\<[10]%
\>[10]{}(\Conid{Con}\;\Varid{c}\;\Varid{ts})\;{}\<[22]%
\>[22]{}(\Conid{Con}\;\Varid{d}\;\Varid{ss}){}\<[34]%
\>[34]{}\mid \Varid{c}\equiv \Varid{d}{}\<[44]%
\>[44]{}\mathrel{=}\Varid{unifyLists}\;\Varid{ts}\;\Varid{ss}{}\<[E]%
\\
\>[3]{}\Varid{unify}\;{}\<[10]%
\>[10]{}\Varid{x}\;{}\<[22]%
\>[22]{}\Varid{y}{}\<[44]%
\>[44]{}\mathrel{=}\Conid{Nothing}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{22}{@{}>{\hspre}l<{\hspost}@{}}%
\column{33}{@{}>{\hspre}l<{\hspost}@{}}%
\column{39}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{unifyLists}\mathbin{::}(\Conid{Show}\;\Varid{v},\Conid{Ord}\;\Varid{v})\Rightarrow [\mskip1.5mu \Conid{Term}\;\Varid{v}\mskip1.5mu]\to [\mskip1.5mu \Conid{Term}\;\Varid{v}\mskip1.5mu]\to \Conid{Maybe}\;(\Conid{Subst}\;\Varid{v}\;\Conid{Term}){}\<[E]%
\\
\>[3]{}\Varid{unifyLists}\;[\mskip1.5mu \mskip1.5mu]\;{}\<[22]%
\>[22]{}[\mskip1.5mu \mskip1.5mu]{}\<[33]%
\>[33]{}\mathrel{=}\Conid{Just}\;\Varid{emptySubst}{}\<[E]%
\\
\>[3]{}\Varid{unifyLists}\;[\mskip1.5mu \mskip1.5mu]\;{}\<[22]%
\>[22]{}(\Varid{x}\mathbin{:}\Varid{xs}){}\<[33]%
\>[33]{}\mathrel{=}\Conid{Nothing}{}\<[E]%
\\
\>[3]{}\Varid{unifyLists}\;(\Varid{x}\mathbin{:}\Varid{xs})\;{}\<[22]%
\>[22]{}[\mskip1.5mu \mskip1.5mu]{}\<[33]%
\>[33]{}\mathrel{=}\Conid{Nothing}{}\<[E]%
\\
\>[3]{}\Varid{unifyLists}\;(\Varid{x}\mathbin{:}\Varid{xs})\;{}\<[22]%
\>[22]{}(\Varid{y}\mathbin{:}\Varid{ys}){}\<[33]%
\>[33]{}\mathrel{=}\mathbf{do}\;{}\<[39]%
\>[39]{}\Varid{s1}\leftarrow \Varid{unify}\;\Varid{x}\;\Varid{y}{}\<[E]%
\\
\>[39]{}\Varid{s2}\leftarrow \Varid{unifyLists}\;(\Varid{map}\;(\bind (\Varid{s1}\mathbin{!}))\;\Varid{xs})\;(\Varid{map}\;(\bind (\Varid{s1}\mathbin{!}))\;\Varid{ys}){}\<[E]%
\\
\>[39]{}\Varid{return}\;(\Varid{s2}\mathbin{|=>}\Varid{s1}){}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
\section{Definitional Trees}
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{module}\;\Conid{PDT}\;\mathbf{where}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{import}\;\Conid{Term}{}\<[E]%
\\
\>[3]{}\mathbf{import}\;\Conid{Unify}{}\<[E]%
\\
\>[3]{}\mathbf{import}\;\Conid{Subst}{}\<[E]%
\\
\>[3]{}\mathbf{import}\;\Conid{\Conid{Data}.List}{}\<[E]%
\\
\>[3]{}\mathbf{import}\;\Conid{\Conid{Data}.Char}\;(\Varid{isAlpha}){}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

This is the first new idea introduced in this paper.
At the high level, a Partial Definitional Tree (or PDT) is a tree for pattern matching rewrite rules.
There are three types of patterns in PDTs: a $Branch$ which represents matching part of a pattern,
a $Rule$ which represents matching enough of a pattern to apply a rewrite rule, and an
$Error$ which represents a failed pattern match.
A Definitional Tree is a PDT where the root pattern has only variables.
By convention a rule must be operation rooted, that is it must start with a function on the left.

For example consider the function $\leq$ defined by \\
$0 \leq _ \rightarrow true$ \\
$S(X1) \leq 0 \rightarrow false$ \\
$S(X1) \leq S(X2) \rightarrow X1 \leq X2$ \\

This would produce the following Definitional Tree

\includegraphics[scale=.5]{figure4}
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{15}{@{}>{\hspre}l<{\hspost}@{}}%
\column{24}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{data}\;\Conid{PDT}\;\Varid{v}{}\<[15]%
\>[15]{}\mathrel{=}\Conid{Branch}\;(\Conid{Term}\;\Varid{v})\;\Conid{Pos}\;[\mskip1.5mu \Conid{PDT}\;\Varid{v}\mskip1.5mu]{}\<[E]%
\\
\>[15]{}\mid \Conid{Rule}\;{}\<[24]%
\>[24]{}(\Conid{Term}\;\Varid{v})\;\Conid{Pos}\;(\Conid{Term}\;\Varid{v})\;(\Conid{Term}\;\Varid{v}){}\<[E]%
\\
\>[15]{}\mid \Conid{Error}\;{}\<[24]%
\>[24]{}(\Conid{Term}\;\Varid{v})\;\Conid{Pos}\;\mathbf{deriving}\;(\Conid{Show}){}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

In order to turn a set of rules into Definitional Trees, we group the rules by their outer function symbol,
and then turn each group of rules into a Definitional Tree.
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{4}{@{}>{\hspre}l<{\hspost}@{}}%
\column{10}{@{}>{\hspre}l<{\hspost}@{}}%
\column{35}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{dt}{}\<[35]%
\>[35]{}\mathbin{::}[\mskip1.5mu \Conid{Rule}\;\Conid{Name}\mskip1.5mu]\to [\mskip1.5mu \Conid{PDT}\;\Conid{Name}\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{dt}\;\Varid{rs}{}\<[35]%
\>[35]{}\mathrel{=}\Varid{map}\;\Varid{topdt}\;(\Varid{splitRules}\;\Varid{rs}){}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mathbf{where}\;\Varid{newPat}\;(\Conid{Fun}\;\Varid{f}\;\Varid{ts}\mathbin{:=>}\anonymous ){}\<[35]%
\>[35]{}\mathrel{=}\Conid{Fun}\;\Varid{f}\;(\Varid{take}\;(\Varid{length}\;\Varid{ts})\;(\Varid{newVars}\;\text{\tt \char34 x\char34}\;[\mskip1.5mu \mskip1.5mu])){}\<[E]%
\\
\>[4]{}\hsindent{6}{}\<[10]%
\>[10]{}\Varid{topdt}\;\Varid{r}{}\<[35]%
\>[35]{}\mathrel{=}\Varid{pdt}\;(\Varid{newPat}\;(\Varid{head}\;\Varid{r}))\;[\mskip1.5mu \mathrm{0}\mskip1.5mu]\;\Varid{r}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

To turn a group of rules with the same function symbol into a Definitional Tree,
we need to slowly fill in the variables with constructors until we reach the pattern for a rule.
The basic algorithm is simple.
Start with a pattern with only variables (i.e. $X1 \leq X2$).
We split the rules into three cases.
Case 1: The pattern we are looking at matches a rule up to variable renaming.  Then we just return the rule.
Case 2: All of the rules have a variable at the position in the pattern we are looking at.
Then we just move on to the next variable in our pattern and start over.
Case 3: Some rules have a constructor in the position of our variable.
In this case we split all of rules by the constructor in that position, and make a subtree out of each constructor.

If now rules match our patter then this is an error.  This actually can't happen,
because this implementation forces the system to be orthogonal (or we will discard rules until it is)
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{4}{@{}>{\hspre}l<{\hspost}@{}}%
\column{11}{@{}>{\hspre}l<{\hspost}@{}}%
\column{18}{@{}>{\hspre}l<{\hspost}@{}}%
\column{22}{@{}>{\hspre}l<{\hspost}@{}}%
\column{24}{@{}>{\hspre}l<{\hspost}@{}}%
\column{28}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{pdt}{}\<[18]%
\>[18]{}\mathbin{::}\Conid{Term}\;\Conid{Name}\to \Conid{Pos}\to [\mskip1.5mu \Conid{Rule}\;\Conid{Name}\mskip1.5mu]\to \Conid{PDT}\;\Conid{Name}{}\<[E]%
\\
\>[3]{}\Varid{pdt}\;\Varid{pat}\;\Varid{p}\;[\mskip1.5mu \mskip1.5mu]{}\<[18]%
\>[18]{}\mathrel{=}\Conid{Error}\;\Varid{pat}\;\Varid{p}{}\<[E]%
\\
\>[3]{}\Varid{pdt}\;\Varid{pat}\;\Varid{p}\;\Varid{rs}{}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mid \neg \;(\Varid{null}\;\Varid{rules}){}\<[24]%
\>[24]{}\mathrel{=}\Varid{renameRule}\;(\Varid{head}\;\Varid{rules}){}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mid \Varid{null}\;\Varid{cons}{}\<[24]%
\>[24]{}\mathrel{=}\Varid{pdt}\;\Varid{pat}\;[\mskip1.5mu \Varid{head}\;\Varid{p}\mathbin{+}\mathrm{1}\mskip1.5mu]\;(\Varid{vRules}\plus \Varid{tooShort}){}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mid \Varid{otherwise}{}\<[24]%
\>[24]{}\mathrel{=}\Conid{Branch}\;\Varid{pat}\;\Varid{p}\mathbin{\$}\Varid{branches}\plus \Varid{varBranch}{}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mathbf{where}\;{}\<[11]%
\>[11]{}\Varid{branches}{}\<[22]%
\>[22]{}\mathrel{=}\Varid{map}\;\Varid{branch}\;\Varid{cons}{}\<[E]%
\\
\>[11]{}\Varid{branch}\;\Varid{cs}{}\<[22]%
\>[22]{}\mathrel{=}\Varid{pdt}\;(\Varid{sub}\;\Varid{cs})\;(\Varid{p}\plus [\mskip1.5mu \mathrm{0}\mskip1.5mu])\;\Varid{cs}{}\<[E]%
\\
\>[11]{}\Varid{rules}{}\<[22]%
\>[22]{}\mathrel{=}[\mskip1.5mu \Conid{Rule}\;\Varid{pat}\;(\Varid{init}\;\Varid{p})\;\Varid{l}\;\Varid{r}\mid (\Varid{l}\mathbin{:=>}\Varid{r})\leftarrow \Varid{rs},\Varid{pat}\mathbin{\char126 =}\Varid{l}\mskip1.5mu]{}\<[E]%
\\
\>[11]{}\Varid{vRules}{}\<[22]%
\>[22]{}\mathrel{=}\Varid{filter}\;\Varid{unifies}\;\Varid{rs}{}\<[E]%
\\
\>[11]{}\Varid{tooShort}{}\<[22]%
\>[22]{}\mathrel{=}\Varid{filter}\;\Varid{ends}\;\Varid{rs}{}\<[E]%
\\
\>[11]{}\Varid{cons}{}\<[22]%
\>[22]{}\mathrel{=}\Varid{groupBy}\;(\Varid{leftEq}\;\Varid{p})\;(\Varid{filter}\;\Varid{differs}\;\Varid{rs}){}\<[E]%
\\
\>[11]{}\Varid{con}\;\Varid{l}{}\<[28]%
\>[28]{}\mathrel{=}\Conid{Con}\;(\Varid{name}\;\Varid{l})\;(\Varid{take}\;(\Varid{arity}\;\Varid{l})\;(\Varid{newVars}\;\Varid{v}\;\Varid{p})){}\<[E]%
\\
\>[11]{}\Varid{sub}\;((\Varid{l}\mathbin{:=>\char95 })\mathbin{:\char95 }){}\<[28]%
\>[28]{}\mathrel{=}\Varid{pat}\bind ((\Varid{v}\mathbin{|->}\Varid{con}\;(\Varid{l}\mathbin{!>}\Varid{p}))\mathbin{!}){}\<[E]%
\\
\>[11]{}(\Conid{Var}\;\Varid{v}){}\<[28]%
\>[28]{}\mathrel{=}\Varid{pat}\mathbin{!>}\Varid{p}{}\<[E]%
\\
\>[11]{}\Varid{exists}\;\Varid{l}{}\<[28]%
\>[28]{}\mathrel{=}\Varid{arity}\;(\Varid{l}\mathbin{!>}(\Varid{init}\;\Varid{p}))\mathbin{>}\Varid{last}\;\Varid{p}{}\<[E]%
\\
\>[11]{}\Varid{ends}\;(\Varid{l}\mathbin{:=>}\Varid{r}){}\<[28]%
\>[28]{}\mathrel{=}\neg \;(\Varid{exists}\;\Varid{l}){}\<[E]%
\\
\>[11]{}\Varid{unifies}\;(\Varid{l}\mathbin{:=>}\Varid{r}){}\<[28]%
\>[28]{}\mathrel{=}\Varid{exists}\;\Varid{l}\mathrel{\wedge}(\Varid{l}\mathbin{!>}\Varid{p})\mathbin{\char126 =}(\Varid{pat}\mathbin{!>}\Varid{p}){}\<[E]%
\\
\>[11]{}\Varid{differs}\;(\Varid{l}\mathbin{:=>}\Varid{r}){}\<[28]%
\>[28]{}\mathrel{=}\Varid{exists}\;\Varid{l}\mathrel{\wedge}\neg \;((\Varid{l}\mathbin{!>}\Varid{p})\mathbin{\char126 =}(\Varid{pat}\mathbin{!>}\Varid{p})){}\<[E]%
\\
\>[11]{}\Varid{varBranch}{}\<[28]%
\>[28]{}\mathrel{=}\mathbf{if}\;\neg \;(\Varid{null}\;\Varid{vRules})\;\mathbf{then}\;[\mskip1.5mu \Varid{pdt}\;\Varid{pat}\;[\mskip1.5mu \Varid{head}\;\Varid{p}\mathbin{+}\mathrm{1}\mskip1.5mu]\;\Varid{vRules}\mskip1.5mu]\;\mathbf{else}\;[\mskip1.5mu \mskip1.5mu]{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

The following functions are all helper functions for $dt$ and $pdt$.
$leftEq$ checks if the left hand side of two rules have the same function/constructor symbol at position $p$.
$splitRules$ splits the rules based on their outer function symbol.
$renameRule$ renames a rule based on the variable names in the PDT.
$newVars$ makes an infinite list of new variables at position $p$.
$name$ just returns the outer name of a term.
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{31}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{leftEq}\mathbin{::}(\Conid{Eq}\;\Varid{v}){}\<[31]%
\>[31]{}\Rightarrow \Conid{Pos}\to \Conid{Rule}\;\Varid{v}\to \Conid{Rule}\;\Varid{v}\to \Conid{Bool}{}\<[E]%
\\
\>[3]{}\Varid{leftEq}\;\Varid{p}\;(\Varid{lx}\mathbin{:=>\char95 })\;(\Varid{ly}\mathbin{:=>\char95 }){}\<[31]%
\>[31]{}\mathrel{=}(\Varid{lx}\mathbin{!>}\Varid{p})\mathbin{\char126 \char126 }(\Varid{ly}\mathbin{!>}\Varid{p}){}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{splitRules}\mathbin{::}(\Conid{Ord}\;\Varid{v})\Rightarrow [\mskip1.5mu \Conid{Rule}\;\Varid{v}\mskip1.5mu]\to [\mskip1.5mu [\mskip1.5mu \Conid{Rule}\;\Varid{v}\mskip1.5mu]\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{splitRules}\mathrel{=}\Varid{map}\;\Varid{sort}\mathbin{\circ}\Varid{groupBy}\;(\Varid{leftEq}\;[\mskip1.5mu \mskip1.5mu]){}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{4}{@{}>{\hspre}l<{\hspost}@{}}%
\column{15}{@{}>{\hspre}l<{\hspost}@{}}%
\column{35}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{renameRule}{}\<[15]%
\>[15]{}\mathbin{::}(\Conid{Show}\;\Varid{v},\Conid{Ord}\;\Varid{v}){}\<[35]%
\>[35]{}\Rightarrow \Conid{PDT}\;\Varid{v}\to \Conid{PDT}\;\Varid{v}{}\<[E]%
\\
\>[3]{}\Varid{renameRule}\;{}\<[15]%
\>[15]{}(\Conid{Rule}\;\Varid{pat}\;\Varid{p}\;\Varid{l}\;\Varid{r}){}\<[35]%
\>[35]{}\mathrel{=}\Conid{Rule}\;\Varid{pat}\;\Varid{p}\;(\Varid{l}\bind (\Varid{s}\mathbin{!}))\;(\Varid{r}\bind (\Varid{s}\mathbin{!})){}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mathbf{where}\;{}\<[15]%
\>[15]{}(\Conid{Just}\;\Varid{s}){}\<[35]%
\>[35]{}\mathrel{=}\Varid{unify}\;\Varid{l}\;\Varid{pat}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{4}{@{}>{\hspre}l<{\hspost}@{}}%
\column{16}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{newVars}{}\<[16]%
\>[16]{}\mathbin{::}\Conid{Name}\to \Conid{Pos}\to [\mskip1.5mu \Conid{Term}\;\Conid{Name}\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{newVars}\;\Varid{n}\;\Varid{p}{}\<[16]%
\>[16]{}\mathrel{=}[\mskip1.5mu \Conid{Var}\;((\Varid{takeWhile}\;\Varid{isAlpha}\;\Varid{n})\plus (\Varid{dots}\;(\Varid{p}\plus [\mskip1.5mu \Varid{i}\mskip1.5mu])))\mid \Varid{i}\leftarrow [\mskip1.5mu \mathrm{0}\mathinner{\ldotp\ldotp}\mskip1.5mu]\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mathbf{where}\;\Varid{dots}{}\<[16]%
\>[16]{}\mathrel{=}\Varid{concat}\mathbin{\circ}\Varid{intersperse}\;\text{\tt \char34 .\char34}\mathbin{\circ}\Varid{map}\;\Varid{show}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{18}{@{}>{\hspre}l<{\hspost}@{}}%
\column{19}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{name}{}\<[18]%
\>[18]{}\mathbin{::}\Conid{Term}\;\Conid{Name}\to \Conid{Name}{}\<[E]%
\\
\>[3]{}\Varid{name}\;(\Conid{Con}\;\Varid{c}\;\anonymous ){}\<[19]%
\>[19]{}\mathrel{=}\Varid{c}{}\<[E]%
\\
\>[3]{}\Varid{name}\;(\Conid{Fun}\;\Varid{f}\;\anonymous ){}\<[19]%
\>[19]{}\mathrel{=}\Varid{f}{}\<[E]%
\\
\>[3]{}\Varid{name}\;(\Conid{Var}\;\Varid{v}){}\<[19]%
\>[19]{}\mathrel{=}\Varid{v}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
\section{Narrowing}
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{module}\;\Conid{Narrow}\;\mathbf{where}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{import}\;\Conid{Term}{}\<[E]%
\\
\>[3]{}\mathbf{import}\;\Conid{Subst}{}\<[E]%
\\
\>[3]{}\mathbf{import}\;\Conid{Unify}{}\<[E]%
\\
\>[3]{}\mathbf{import}\;\Conid{PDT}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

Finally we get to Narrowing.  The idea here is that we are evaluating a term while unifying it.
This is done by unifying our value with a PDT, and when we encounter a rewrite rule we replace that subterm.
This is called a narrowing step.
First we need to find the rule we want to use.
This can be done by traversing the PDT and unifying with each branch until we hit a rule or an error.
Since one term can be rewritten to multiple different terms we return a list of new terms together with substitutions.
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{type}\;\Conid{Step}\;\Varid{v}\mathrel{=}[\mskip1.5mu (\Conid{Pos},\Conid{Rule}\;\Varid{v},\Conid{Subst}\;\Varid{v}\;\Conid{Term})\mskip1.5mu]{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{4}{@{}>{\hspre}l<{\hspost}@{}}%
\column{5}{@{}>{\hspre}l<{\hspost}@{}}%
\column{12}{@{}>{\hspre}l<{\hspost}@{}}%
\column{24}{@{}>{\hspre}l<{\hspost}@{}}%
\column{31}{@{}>{\hspre}l<{\hspost}@{}}%
\column{35}{@{}>{\hspre}l<{\hspost}@{}}%
\column{55}{@{}>{\hspre}l<{\hspost}@{}}%
\column{56}{@{}>{\hspre}c<{\hspost}@{}}%
\column{56E}{@{}l@{}}%
\column{59}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{narrow}\mathbin{::}(\Conid{Show}\;\Varid{v},\Conid{Ord}\;\Varid{v}){}\<[35]%
\>[35]{}\Rightarrow \Conid{Term}\;\Varid{v}\to \Conid{PDT}\;\Varid{v}\to [\mskip1.5mu \Conid{PDT}\;\Varid{v}\mskip1.5mu]\to \Conid{Step}\;\Varid{v}{}\<[E]%
\\
\>[3]{}\Varid{narrow}\;\Varid{t}\;(\Conid{Rule}\;\Varid{pat}\;\Varid{p}\;\Varid{l}\;\Varid{r})\;{}\<[31]%
\>[31]{}\anonymous {}\<[35]%
\>[35]{}\mathrel{=}[\mskip1.5mu (\Varid{p},\Varid{l}\mathbin{:=>}\Varid{r},\Varid{u}){}\<[55]%
\>[55]{}\mid (\Conid{Just}\;\Varid{u})\leftarrow [\mskip1.5mu \Varid{unify}\;\Varid{pat}\;\Varid{t}\mskip1.5mu]\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{narrow}\;\Varid{t}\;(\Conid{Error}\;\Varid{pat}\;\Varid{p})\;{}\<[31]%
\>[31]{}\anonymous {}\<[35]%
\>[35]{}\mathrel{=}[\mskip1.5mu (\Varid{p},\Conid{E},\Varid{u}){}\<[55]%
\>[55]{}\mid (\Conid{Just}\;\Varid{u})\leftarrow [\mskip1.5mu \Varid{unify}\;\Varid{pat}\;\Varid{t}\mskip1.5mu]\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{narrow}\;\Varid{t}\;(\Conid{Branch}\;\Varid{pat}\;\Varid{p}\;\Varid{ts})\;{}\<[31]%
\>[31]{}\Varid{dt}{}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mid \neg \;(\Varid{null}\;\Varid{us}){}\<[35]%
\>[35]{}\mathrel{=}[\mskip1.5mu (\Varid{p'},\Varid{r},\Varid{s}){}\<[56]%
\>[56]{}\mid {}\<[56E]%
\>[59]{}(\Varid{ti},\Varid{u})\leftarrow \Varid{us},{}\<[E]%
\\
\>[59]{}(\Varid{p'},\Varid{r},\Varid{s})\leftarrow \Varid{narrow}\;\Varid{t}\;\Varid{ti}\;\Varid{dt}\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mid \Varid{otherwise}{}\<[35]%
\>[35]{}\mathrel{=}[\mskip1.5mu (\Varid{p'},\Varid{r},\Varid{s}\mathbin{|=>}\Varid{tau}){}\<[56]%
\>[56]{}\mid {}\<[56E]%
\>[59]{}(\Varid{p'},\Varid{r},\Varid{s})\leftarrow \Varid{narrow}\;\Varid{t}\;\Varid{t'}\;\Varid{dt}\mskip1.5mu]{}\<[E]%
\\
\>[4]{}\hsindent{1}{}\<[5]%
\>[5]{}\mathbf{where}\;{}\<[12]%
\>[12]{}\Varid{us}{}\<[24]%
\>[24]{}\mathrel{=}[\mskip1.5mu (\Varid{ti},\Varid{u})\mid \Varid{ti}\leftarrow \Varid{ts},(\Conid{Just}\;\Varid{u})\leftarrow [\mskip1.5mu \Varid{unify}\;(\Varid{pattern}\;\Varid{ti})\;\Varid{t}\mskip1.5mu]\mskip1.5mu]{}\<[E]%
\\
\>[12]{}(\Conid{Just}\;\Varid{tau}){}\<[24]%
\>[24]{}\mathrel{=}\Varid{unify}\;\Varid{pat}\;\Varid{t}{}\<[E]%
\\
\>[12]{}\Varid{t'}{}\<[24]%
\>[24]{}\mathrel{=}\Varid{getTree}\;((\Varid{t}\mathbin{!>}\Varid{p})\bind (\Varid{tau}\mathbin{!}))\;\Varid{dt}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{4}{@{}>{\hspre}l<{\hspost}@{}}%
\column{17}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{getTree}\mathbin{::}\Conid{Term}\;\Varid{v}\to [\mskip1.5mu \Conid{PDT}\;\Varid{v}\mskip1.5mu]\to \Conid{PDT}\;\Varid{v}{}\<[E]%
\\
\>[3]{}\Varid{getTree}\;\Varid{t}\;[\mskip1.5mu \mskip1.5mu]\mathrel{=}\Conid{Error}\;\Varid{t}\;[\mskip1.5mu \mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{getTree}\;\Varid{t}\mathord{@}(\Conid{Fun}\;\Varid{f}\;\anonymous )\;(\Varid{dt}\mathord{@}(\Conid{Branch}\;(\Conid{Fun}\;\Varid{g}\;\anonymous )\;\anonymous \;\anonymous )\mathbin{:}\Varid{dts}){}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mid \Varid{f}\equiv \Varid{g}{}\<[17]%
\>[17]{}\mathrel{=}\Varid{dt}{}\<[E]%
\\
\>[3]{}\hsindent{1}{}\<[4]%
\>[4]{}\mid \Varid{otherwise}{}\<[17]%
\>[17]{}\mathrel{=}\Varid{getTree}\;\Varid{t}\;\Varid{dts}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{29}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{pattern}\;(\Conid{Rule}\;\Varid{pat}\;\anonymous \;\anonymous \;\anonymous ){}\<[29]%
\>[29]{}\mathrel{=}\Varid{pat}{}\<[E]%
\\
\>[3]{}\Varid{pattern}\;(\Conid{Error}\;\Varid{pat}\;\anonymous ){}\<[29]%
\>[29]{}\mathrel{=}\Varid{pat}{}\<[E]%
\\
\>[3]{}\Varid{pattern}\;(\Conid{Branch}\;\Varid{pat}\;\anonymous \;\anonymous ){}\<[29]%
\>[29]{}\mathrel{=}\Varid{pat}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

Now that we can complete a narrowing step, we need to narrow a term to a normal form.
We can do this by narrowing each term to head normal form.
\\
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{26}{@{}>{\hspre}l<{\hspost}@{}}%
\column{31}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{nf}\mathbin{::}(\Conid{Show}\;\Varid{v},\Conid{Ord}\;\Varid{v}){}\<[26]%
\>[26]{}\Rightarrow [\mskip1.5mu \Conid{PDT}\;\Varid{v}\mskip1.5mu]\to \Conid{Term}\;\Varid{v}\to [\mskip1.5mu (\Conid{Term}\;\Varid{v},\Conid{Subst}\;\Varid{v}\;\Conid{Term})\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{nf}\;\Varid{dts}\;(\Conid{Con}\;\Varid{c}\;\Varid{ts}){}\<[26]%
\>[26]{}\mathrel{=}[\mskip1.5mu (\Conid{Con}\;\Varid{c}\;(\Varid{map}\;\Varid{fst}\;\Varid{tsu}),\Varid{chain}\;(\Varid{map}\;\Varid{snd}\;\Varid{tsu})){}\<[E]%
\\
\>[26]{}\hsindent{5}{}\<[31]%
\>[31]{}\mid \Varid{tsu}\leftarrow \Varid{sequence}\;(\Varid{map}\;(\Varid{nf}\;\Varid{dts})\;\Varid{ts})\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{nf}\;\Varid{dts}\;\Varid{f}\mathord{@}(\Conid{Fun}\;\anonymous \;\anonymous ){}\<[26]%
\>[26]{}\mathrel{=}[\mskip1.5mu (\Varid{f''},\Varid{u}\mathbin{|=>}\Varid{u'})\mid (\Varid{f'},\Varid{u})\leftarrow \Varid{hnf}\;\Varid{dts}\;\Varid{f},(\Varid{f''},\Varid{u'})\leftarrow \Varid{nf}\;\Varid{dts}\;\Varid{f'}\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{nf}\;\Varid{dts}\;\Varid{v}\mathord{@}(\Conid{Var}\;\anonymous ){}\<[26]%
\>[26]{}\mathrel{=}[\mskip1.5mu (\Varid{v},\Varid{emptySubst})\mskip1.5mu]{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{12}{@{}>{\hspre}l<{\hspost}@{}}%
\column{27}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{hnf}\mathbin{::}(\Conid{Show}\;\Varid{v},\Conid{Ord}\;\Varid{v}){}\<[27]%
\>[27]{}\Rightarrow [\mskip1.5mu \Conid{PDT}\;\Varid{v}\mskip1.5mu]\to \Conid{Term}\;\Varid{v}\to [\mskip1.5mu (\Conid{Term}\;\Varid{v},\Conid{Subst}\;\Varid{v}\;\Conid{Term})\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{hnf}\;\anonymous \;{}\<[12]%
\>[12]{}\Varid{c}\mathord{@}(\Conid{Con}\;\anonymous \;\anonymous ){}\<[27]%
\>[27]{}\mathrel{=}[\mskip1.5mu (\Varid{c},\Varid{emptySubst})\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{hnf}\;\Varid{dts}\;{}\<[12]%
\>[12]{}\Varid{f}\mathord{@}(\Conid{Fun}\;\anonymous \;\anonymous ){}\<[27]%
\>[27]{}\mathrel{=}[\mskip1.5mu (\Varid{f'},\Varid{u'}\mathbin{|=>}\Varid{u})\mid (\Varid{p},\Varid{l}\mathbin{:=>}\Varid{r},\Varid{u})\leftarrow \Varid{narrow}\;\Varid{f}\;(\Varid{getTree}\;\Varid{f}\;\Varid{dts})\;\Varid{dts},(\Varid{f'},\Varid{u'})\leftarrow \Varid{hnf}\;\Varid{dts}\;(\Varid{r}\bind (\Varid{u}\mathbin{!}))\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{hnf}\;\anonymous \;{}\<[12]%
\>[12]{}\Varid{v}\mathord{@}(\Conid{Var}\;\anonymous ){}\<[27]%
\>[27]{}\mathrel{=}[\mskip1.5mu (\Varid{v},\Varid{emptySubst})\mskip1.5mu]{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks

\section{examples that will be fixed later}
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\mathbf{import}\;\Conid{Term}{}\<[E]%
\\
\>[3]{}\mathbf{import}\;\Conid{Subst}{}\<[E]%
\\
\>[3]{}\mathbf{import}\;\Conid{Unify}{}\<[E]%
\\
\>[3]{}\mathbf{import}\;\Conid{PDT}{}\<[E]%
\\
\>[3]{}\mathbf{import}\;\Conid{Narrow}{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

\section{Example}

The following is a simple example of a rewrite system where equations can be solved using the Narrowing strategy.
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{z}\mathrel{=}\Conid{Con}\;\text{\tt \char34 0\char34}\;[\mskip1.5mu \mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{s}\;\Varid{x}\mathrel{=}\Conid{Con}\;\text{\tt \char34 S\char34}\;[\mskip1.5mu \Varid{x}\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{x}\mathrel{=}\Conid{Var}\;\text{\tt \char34 X\char34}{}\<[E]%
\\
\>[3]{}\Varid{y}\mathrel{=}\Conid{Var}\;\text{\tt \char34 Y\char34}{}\<[E]%
\\
\>[3]{}\Varid{t}\mathrel{=}\Conid{Con}\;\text{\tt \char34 true\char34}\;[\mskip1.5mu \mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{f}\mathrel{=}\Conid{Con}\;\text{\tt \char34 false\char34}\;[\mskip1.5mu \mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{x}\mathbin{<-=}\Varid{y}\mathrel{=}\Conid{Fun}\;\text{\tt \char34 <=\char34}\;[\mskip1.5mu \Varid{x},\Varid{y}\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{half}\;\Varid{x}\mathrel{=}\Conid{Fun}\;\text{\tt \char34 half\char34}\;[\mskip1.5mu \Varid{x}\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{x}\mathbin{/\char92 }\Varid{y}\mathrel{=}\Conid{Fun}\;\text{\tt \char34 /\char92 \char92 \char34}\;[\mskip1.5mu \Varid{x},\Varid{y}\mskip1.5mu]{}\<[E]%
\\
\>[3]{}\Varid{x}\mathbin{=:=}\Varid{y}\mathrel{=}\Conid{Fun}\;\text{\tt \char34 =:=\char34}\;[\mskip1.5mu \Varid{x},\Varid{y}\mskip1.5mu]{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $
\begingroup\par\noindent\advance\leftskip\mathindent\(
\begin{pboxed}\SaveRestoreHook
\column{B}{@{}>{\hspre}l<{\hspost}@{}}%
\column{3}{@{}>{\hspre}l<{\hspost}@{}}%
\column{14}{@{}>{\hspre}l<{\hspost}@{}}%
\column{33}{@{}>{\hspre}l<{\hspost}@{}}%
\column{E}{@{}>{\hspre}l<{\hspost}@{}}%
\>[3]{}\Varid{rules}\mathrel{=}[\mskip1.5mu {}\<[14]%
\>[14]{}(\Varid{z}\mathbin{<-=}\Varid{x}){}\<[33]%
\>[33]{}\mathbin{:=>}\Varid{t},{}\<[E]%
\\
\>[14]{}(\Varid{s}\;\Varid{x}\mathbin{<-=}\Varid{z}){}\<[33]%
\>[33]{}\mathbin{:=>}\Varid{f},{}\<[E]%
\\
\>[14]{}(\Varid{s}\;\Varid{x}\mathbin{<-=}\Varid{s}\;\Varid{y}){}\<[33]%
\>[33]{}\mathbin{:=>}(\Varid{x}\mathbin{<-=}\Varid{y}),{}\<[E]%
\\
\>[14]{}(\Varid{half}\;\Varid{z}){}\<[33]%
\>[33]{}\mathbin{:=>}\Varid{z},{}\<[E]%
\\
\>[14]{}(\Varid{half}\;(\Varid{s}\;\Varid{z})){}\<[33]%
\>[33]{}\mathbin{:=>}\Varid{z},{}\<[E]%
\\
\>[14]{}(\Varid{half}\;(\Varid{s}\;(\Varid{s}\;\Varid{x}))){}\<[33]%
\>[33]{}\mathbin{:=>}(\Varid{half}\;\Varid{x}),{}\<[E]%
\\
\>[14]{}\Varid{t}\mathbin{/\char92 }\Varid{x}{}\<[33]%
\>[33]{}\mathbin{:=>}\Varid{x},{}\<[E]%
\\
\>[14]{}\Varid{f}\mathbin{/\char92 }\Varid{x}{}\<[33]%
\>[33]{}\mathbin{:=>}\Varid{f},{}\<[E]%
\\
\>[14]{}(\Varid{t}\mathbin{=:=}\Varid{t}){}\<[33]%
\>[33]{}\mathbin{:=>}\Varid{t},{}\<[E]%
\\
\>[14]{}(\Varid{f}\mathbin{=:=}\Varid{f}){}\<[33]%
\>[33]{}\mathbin{:=>}\Varid{t},{}\<[E]%
\\
\>[14]{}(\Varid{z}\mathbin{=:=}\Varid{z}){}\<[33]%
\>[33]{}\mathbin{:=>}\Varid{t},{}\<[E]%
\\
\>[14]{}((\Varid{s}\;\Varid{x})\mathbin{=:=}(\Varid{s}\;\Varid{y})){}\<[33]%
\>[33]{}\mathbin{:=>}(\Varid{x}\mathbin{=:=}\Varid{y})\mskip1.5mu]{}\<[E]%
\ColumnHook
\end{pboxed}
\)\par\noindent\endgroup\resethooks
$\ $

\begin{thebibliography}{1}

\bibitem{Antoy} Antoy, Sergio {\em  A Needed Narrowing Strategy} 1994, POPL 94.

\end{thebibliography}

\end{document}
