Commit 0c33e030 authored by Björn Peemöller 's avatar Björn Peemöller
Browse files

Removed old documentation of Curry records

parent 56f18678
\documentclass[11pt,fleqn]{article}
\usepackage{latexsym}
\usepackage{makeidx}
\usepackage{amssymb}
\usepackage{url}
%%% ------------------------------------------------------------------
\usepackage[colorlinks,linkcolor=blue]{hyperref}
%\usepackage[]{hyperref}
\hypersetup{bookmarksopen=true}
\hypersetup{bookmarksopenlevel=0}
\hypersetup{pdftitle={Curry: An Integrated Functional Logic Language}}
\hypersetup{pdfauthor={Michael Hanus}}
%\hypersetup{pdfstartview=Title}
\hypersetup{pdfstartview=FitH}
\usepackage{thumbpdf}
%%% ------------------------------------------------------------------
\setlength{\textwidth}{16.5cm}
\setlength{\textheight}{23cm}
\renewcommand{\baselinestretch}{1.1}
\setlength{\topmargin}{-1cm}
\setlength{\oddsidemargin}{0cm}
\setlength{\evensidemargin}{0cm}
\setlength{\marginparwidth}{0.0cm}
\setlength{\marginparsep}{0.0cm}
\newlength{\figurewidth}
\setlength{\figurewidth}{\textwidth}
\addtolength{\figurewidth}{-0.4cm}
% environment for typing program texts:
\makeatletter
\newenvironment{prog}{\par\vspace{1.5ex}
\setlength{\parindent}{1.0cm}
\setlength{\parskip}{-0.1ex}
\obeylines\@vobeyspaces\tt}{\vspace{1.5ex}\noindent
}
\makeatother
\newcommand{\startprog}{\begin{prog}}
\newcommand{\stopprog}{\end{prog}\noindent}
\newcommand{\pr}[1]{\mbox{\tt #1}} % program text in normal text
\newcommand{\Ac}{{\cal{A}}}
\newcommand{\Cc}{{\cal{C}}}
\newcommand{\Dc}{{\cal{D}}}
\newcommand{\Fc}{{\cal{F}}}
\newcommand{\Tc}{{\cal{T}}}
\newcommand{\Xc}{{\cal{X}}}
\newcommand{\var}{{\cal V}ar}
\newcommand{\Dom}{{\cal D}om}
\newcommand{\VRan}{{\cal VR}an}
\renewcommand{\emptyset}{\varnothing}
\newcommand{\ans}{\mathbin{\framebox[1mm]{\rule{0cm}{1.2mm}}}}
%\newcommand{\ans}{\;}
\newcommand{\todo}[1]{\fbox{\sc To do: #1}}
\newcommand{\dexp}{D} % a disjunctive expression
\newcommand{\cconj}{\ensuremath \mathop{\pr{\&}}} % concurrent conj. in math
\newcommand{\ttbs}{\mbox{\tt\char92}} % backslash in tt font
\newcommand{\ttus}{\mbox{\tt\char95}} % underscore in tt font
\newcommand{\sem}[1]{\ensuremath [\![#1]\!]} % double square brackets
\newcommand{\eval}[1]{{\cal E}\!val\sem{#1}}
\newcommand{\To}{\Rightarrow}
\newcommand{\infrule}[2]{\begin{array}{@{}c@{}} #1 \\ \hline #2 \end{array}}
\newcommand{\pindex}[1]{\index{#1@{\tt #1}}} % program elements in index
\newcommand{\comment}[1]{} % ignore the argument
\catcode`\_=\active
\let_=\sb
\catcode`\_=12
% commands for the syntax:
\newcommand{\term}[1]{{\tt #1}}
\newcommand{\opt}[1]{\rm [\it #1{\rm ]}}
\newcommand{\offside}[3]{\term{\{}\seq{#1}{#2}{#3} \term{\}}}
\newcommand{\seq}[3]{{\it #1$_1$ \term{#2} $\ldots$ \term{#2} #1$_#3$}}
\newcommand{\head}[1]{\makebox[1.3in][r]{\it #1}}
\newcommand{\por}{\\\head{}\makebox[3em][c]{$|$}}
\newcommand{\nextline}{\\\head{}\makebox[3em]{}}
\newcommand{\marg}[1]{\hspace{\fill}$(#1)$}
\newcommand{\lexicon}[1]{\production #1 {\rm see lexicon} end}
\def\production #1 #2 end {\head{#1}\makebox[3em]{\rm ::=}\it #2}
\newcommand{\translation}[1]{\bigskip\fbox{\begin{minipage}{15cm}\textbf{Translation: }{#1}\end{minipage}}\bigskip}
\makeindex
\begin{document}
%%------------------------------------------------------------------------------
%%------------------------------------------------------------------------------
\section {Records}
\label{records}
A record is a data structure for bundling several data of various types.
It consits of typed data fields where each field is associated with
a unique label. These labels can be used to construct, select or update
fields in a record.
Unlike labeled data fields in Haskell, records are
no syntactic sugar but a real extension of the
language\footnote{
The current version allows to transform records
into abstract data types. Future extensions may not have
this facility.}.
The basic concept is described in \cite{Leijen05} but the current
version doesn't yet provide all features mentioned there.
The restrictions are explained in section \ref{sec-restrinrecs}.
\subsection{Record Type Declaration}
\label{sec-recordtypedecl}
It is necessary to define a record type before a record
can be constructed or used. The declaration has the following form:
\startprog
type $R$ $\alpha_1$ \ldots $\alpha_n$ = \{ $l_1$ :: $\tau_1$, \ldots, $l_m$ :: $\tau_m$ \}
\stopprog
It introduces a new $n$-ary record type $R$ which represents a
record consisting of $m$ fields. Each field has a unique label $l_i$
representing a value of the type $\tau_i$. Labels
are identifiers which refer to the corresponding
fields. The following examples define some records types:
\startprog
type Person = \{name :: String, age :: Int\}
type Address = \{person :: Person, street :: String, city :: String\}
type Branch a b = \{left :: a, right :: b\}
\stopprog
It is possible to summarize different labels which have the same
type. E.g. the record \pr{Address} can also be declared as follows:
\startprog
type Address = \{person :: Person, street,city :: String\}
\stopprog
The fields can occur in an arbitrary order. The example above
can also be written as
\startprog
type Address = \{street,city :: String, person :: Person\}
\stopprog
The record type can be used in every type expression to represent
the corresponding record, e.g.
\startprog
data BiTree = Node (Branch BiTree BiTree) | Leaf Int
\stopprog
\startprog
getName :: Person -> String
getName \ldots
\stopprog
Labels can only be used in the context of
records. They do not share the name space with
functions/constructors/variables or type constructors/type variables.
For instance it is possible to use
the same identifier for a label and a function at the same time. Label
identifiers cannot be shadowed by other identifiers.
Like in type synonym declarations, recursive or mutually
dependent record declarations are not allowed. Records can only
be declared at top level. Further restrictions are described in
section \ref{sec-restrinrecs}.
\subsection{Record Construction}
\label{sec-recordconstr}
The record construction generates a record with initial values for
each data field. It has the following form:
\startprog
\{ $l_1$ = $v_1$, \ldots, $l_m$ = $v_m$ \}
\stopprog
It generates a record where each label $l_i$ refers to the
value $v_i$. The type of the record results from the record type
declaration where the labels $l_i$ are defined.
A mix of labels from different
record types is not allowed. All labels must be specified with
exactly one assignment. Examples for record constructions are
\startprog
\{name = "Johnson", age = 30\} -- generates a record of type 'Person'
\{left = True, right = 20\} -- generates a record of type 'Branch'
\stopprog
Assignments to labels can occur in an arbitrary order. For instance a
record of type \pr{Person} can also be generated as follows:
\startprog
\{age = 30, name = "Johnson"\} -- generates a record of type 'Person'
\stopprog
Unlike labeled fields in record type declarations,
record constructions can be used in expressions without any restrictions
(as well as all kinds of record expressions). For instance the following
expression is valid:
\startprog
\{person = \{name = "Smith", age = 20\}, -- generates a record of
street = "Main Street", -- type 'Address'
city = "Springfield"\}
\stopprog
\subsection{Field Selection}
\label{sec-fieldsel}
The field selection is used to extract data from records.
It has the following form:
\startprog
$r$ -> $l$
\stopprog
It returns the value to which the label $l$ refers to from the
record expression $r$. The label must occur in the declaration of
the record type of $r$.
An example for a field selection is:
\startprog
pers -> name
\stopprog
This returns the value of the label \pr{name} from the record \pr{pers}
(which has the type \pr{Person}).
Sequential application of field selections are also possible:
\startprog
(addr -> person) -> age
\stopprog
The value of the label \pr{age} is extracted from a record which itself
is the value of the label \pr{person} in the record \pr{addr}
(which has the type \pr{Address}). When a field selection is used in
expressions, it has to be parenthesized.
\subsection{Field Update}
\label{sec-fieldupd}
Records can be updated by reassigning a new value to a label:
\startprog
\{$l_1$ := $v_1$, \ldots, $l_k$ := $v_k$ | $r$\}
\stopprog
The label $l_i$ is associated with the new value $v_i$ which
replaces the current value in the record $r$.
The labels must occur in the declaration
of the record type of $r$. In contarst to record constructions,
it is not necessary to specifiy all labels of a record.
Assignments can occur in an arbitrary order. It is not allowed to
specify more than one assignement for a label in a record update.
Examples for record updates are:
\startprog
\{name := "Scott", age := 25 | pers\}
\{person := \{name := "Scott", age := 25 | pers\} | addr\}
\stopprog
In these examples \pr{pers} is a record of type \pr{Person} and \pr{addr}
is a record of type \pr{Address}.
\subsection{Records in Pattern Matching}
\label{sec-recsinpm}
It is possible to apply pattern matching to records (e.g. in functions,
let expressions or case branches). There are two kinds of record patterns
available:
\startprog
\{$l_1$ = $p_1$, \ldots, $l_n$ = $p_n$\}
\{$l_1$ = $p_1$, \ldots, $l_k$ = $p_k$ | _\}
\stopprog
In both cases each label $l_i$ is specified with a pattern $p_i$.
All labels must occur only once in the record pattern.
The first case is used to match the whole record. Thus all labels
of the record must occur in the pattern.
The second case is used to match only a part of
the record. Here it is not necessary to specify all labels.
This case is reprsented by a vertical bar followed by the undescore
(anonymous variable). It is
not allowed to use a pattern term instead of the underscore.
When tyring to match a record against a record pattern, the
patterns of the specified labels are matched against
the corresponding values in the record expression. On succes, all pattern
variables occurring in the patterns are replaced by their actual expression.
If none of the patterns matches, the computation fails.
Here are some expamples of pattern matching with records:
\startprog
isSmith30 :: Person -> Bool
isSmith30 \{name = "Smith", age = 30\} = True
\stopprog
\startprog
startsWith :: Char -> Person -> Bool
startsWith c \{name = (d:_) | _\} = c == d
\stopprog
\startprog
getPerson :: Address -> Person
getPerson \{person = p | _\} = p
\stopprog
As shown in the last example, a field selections can also be realized with
pattern matching.
\subsection{Export of Records}
\label{sec-exprecs}
Exporting record types and labels is very similar to exporting
data types and constructors. There are three ways
to specify an export:
\begin{itemize}
\item \pr{module $M$ (\ldots, $R$, \ldots) where} \\
exports the record $R$ without any of its labels.
\item \pr{module $M$ (\ldots, $R$(..), \ldots) where} \\
exports the record $R$ together with all its labels.
\item \pr{module $M$ (\ldots, $R$($l_1$,\ldots,$l_k$), \ldots) where} \\
exports the record $R$ together with the labels $l_1$, \ldots, $l_k$.
\end{itemize}
Note that imported labels cannot be overwritten in record declarations
of the importing module. It is also not possible to import equal labels
from different modules.
\subsection{Restrictions in the Usage of Records}
\label{sec-restrinrecs}
In contrast to the basic concept in \cite{Leijen05}, Curry provides a
simplier version of records. Some of the features described there are
currently not supported or even restricted.
\begin{itemize}
\item Labels must be unique within the whole scope of the program.
In particular it is not allowed to define the same label within
different records, not even when they are imported from other
modules. But it is possible to use equal identifiers for other
entities without restrictions, since labels have an independent
name space.
\item The record type representation with labeled fields can only be
used as the right-hand-side of a record type declaration. It is
not allowed to use it in any other type annotation.
\item Records are not extensible or reducible. The structure of a
record is specified in its record declaration and cannot be
modified at the runtime of the program.
\item Empty records are not allowed.
\item It is not allowed to use a pattern term
at the right side of the vertical bar in a record pattern
except for the underscore (anonymous pattern variable).
\item Labels cannot be sequentially associated with multiple values
(record fields don't behave like stacks).
\end{itemize}
%%------------------------------------------------------------------------------
%%------------------------------------------------------------------------------
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment