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

Removed old documentation of Curry records

parent 56f18678
%%% ------------------------------------------------------------------
\hypersetup{pdftitle={Curry: An Integrated Functional Logic Language}}
\hypersetup{pdfauthor={Michael Hanus}}
%%% ------------------------------------------------------------------
% environment for typing program texts:
\newcommand{\pr}[1]{\mbox{\tt #1}} % program text in normal text
\newcommand{\var}{{\cal V}ar}
\newcommand{\Dom}{{\cal D}om}
\newcommand{\VRan}{{\cal VR}an}
\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{\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
% 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{\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}
\section {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
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}
It is necessary to define a record type before a record
can be constructed or used. The declaration has the following form:
type $R$ $\alpha_1$ \ldots $\alpha_n$ = \{ $l_1$ :: $\tau_1$, \ldots, $l_m$ :: $\tau_m$ \}
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:
type Person = \{name :: String, age :: Int\}
type Address = \{person :: Person, street :: String, city :: String\}
type Branch a b = \{left :: a, right :: b\}
It is possible to summarize different labels which have the same
type. E.g. the record \pr{Address} can also be declared as follows:
type Address = \{person :: Person, street,city :: String\}
The fields can occur in an arbitrary order. The example above
can also be written as
type Address = \{street,city :: String, person :: Person\}
The record type can be used in every type expression to represent
the corresponding record, e.g.
data BiTree = Node (Branch BiTree BiTree) | Leaf Int
getName :: Person -> String
getName \ldots
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}
The record construction generates a record with initial values for
each data field. It has the following form:
\{ $l_1$ = $v_1$, \ldots, $l_m$ = $v_m$ \}
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
\{name = "Johnson", age = 30\} -- generates a record of type 'Person'
\{left = True, right = 20\} -- generates a record of type 'Branch'
Assignments to labels can occur in an arbitrary order. For instance a
record of type \pr{Person} can also be generated as follows:
\{age = 30, name = "Johnson"\} -- generates a record of type 'Person'
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:
\{person = \{name = "Smith", age = 20\}, -- generates a record of
street = "Main Street", -- type 'Address'
city = "Springfield"\}
\subsection{Field Selection}
The field selection is used to extract data from records.
It has the following form:
$r$ -> $l$
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:
pers -> name
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:
(addr -> person) -> age
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}
Records can be updated by reassigning a new value to a label:
\{$l_1$ := $v_1$, \ldots, $l_k$ := $v_k$ | $r$\}
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:
\{name := "Scott", age := 25 | pers\}
\{person := \{name := "Scott", age := 25 | pers\} | addr\}
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}
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
\{$l_1$ = $p_1$, \ldots, $l_n$ = $p_n$\}
\{$l_1$ = $p_1$, \ldots, $l_k$ = $p_k$ | _\}
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:
isSmith30 :: Person -> Bool
isSmith30 \{name = "Smith", age = 30\} = True
startsWith :: Char -> Person -> Bool
startsWith c \{name = (d:_) | _\} = c == d
getPerson :: Address -> Person
getPerson \{person = p | _\} = p
As shown in the last example, a field selections can also be realized with
pattern matching.
\subsection{Export of Records}
Exporting record types and labels is very similar to exporting
data types and constructors. There are three ways
to specify an export:
\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$.
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}
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.
\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).
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
\ No newline at end of file
Supports Markdown
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