Add examples of doing record updates

This commit is contained in:
Iavor Diatchki 2019-03-01 11:10:28 -08:00
parent 139c7d50ac
commit afe89d2ad4
5 changed files with 83 additions and 59 deletions

Binary file not shown.

View File

@ -2,8 +2,7 @@
% markdown in ../../Syntax.md. If you make changes, please make them
% there and then regenerate the .tex file using the Makefile.
\hypertarget{layout}{%
\section{Layout}\label{layout}}
\section{Layout}\label{layout}
Groups of declarations are organized based on indentation. Declarations
with the same indentation belong to the same group. Lines of text that
@ -33,8 +32,7 @@ containing \texttt{y} and \texttt{z}. This group ends just before
\texttt{g}, because \texttt{g} is indented less than \texttt{y} and
\texttt{z}.
\hypertarget{comments}{%
\section{Comments}\label{comments}}
\section{Comments}\label{comments}
Cryptol supports block comments, which start with \texttt{/*} and end
with \texttt{*/}, and line comments, which start with \texttt{//} and
@ -49,8 +47,7 @@ Examples:
/* This is a /* Nested */ block comment */
\end{verbatim}
\hypertarget{identifiers}{%
\section{Identifiers}\label{identifiers}}
\section{Identifiers}\label{identifiers}
Cryptol identifiers consist of one or more characters. The first
character must be either an English letter or underscore (\texttt{\_}).
@ -67,9 +64,8 @@ name name1 name' longer_name
Name Name2 Name'' longerName
\end{verbatim}
\hypertarget{keywords-and-built-in-operators}{%
\section{Keywords and Built-in
Operators}\label{keywords-and-built-in-operators}}
\hypertarget{keywords-and-built-in-operators}{\section{Keywords and
Built-in Operators}\label{keywords-and-built-in-operators}}
The following identifiers have special meanings in Cryptol, and may not
be used for programmer defined names:
@ -120,9 +116,8 @@ left\tabularnewline
\bottomrule
\end{longtable}
\hypertarget{built-in-type-level-operators}{%
\section{Built-in Type-level
Operators}\label{built-in-type-level-operators}}
Operators}\label{built-in-type-level-operators}
Cryptol includes a variety of operators that allow computations on the
numeric types used to specify the sizes of sequences.
@ -154,8 +149,7 @@ up)\tabularnewline
\bottomrule
\end{longtable}
\hypertarget{numeric-literals}{%
\section{Numeric Literals}\label{numeric-literals}}
\section{Numeric Literals}\label{numeric-literals}
Numeric literals may be written in binary, octal, decimal, or
hexadecimal notation. The base of a literal is determined by its prefix:
@ -188,8 +182,7 @@ is inferred from context in which the literal is used. Examples:
// a = Integer or [n] where n >= width 10
\end{verbatim}
\hypertarget{bits}{%
\section{Bits}\label{bits}}
\section{Bits}\label{bits}
The type \texttt{Bit} has two inhabitants: \texttt{True} and
\texttt{False}. These values may be combined using various logical
@ -220,8 +213,7 @@ Operator & Associativity & Description\tabularnewline
\bottomrule
\end{longtable}
\hypertarget{multi-way-conditionals}{%
\section{Multi-way Conditionals}\label{multi-way-conditionals}}
\section{Multi-way Conditionals}\label{multi-way-conditionals}
The \texttt{if\ ...\ then\ ...\ else} construct can be used with
multiple branches. For example:
@ -235,8 +227,7 @@ x = if y % 2 == 0 then 1
else 7
\end{verbatim}
\hypertarget{tuples-and-records}{%
\section{Tuples and Records}\label{tuples-and-records}}
\section{Tuples and Records}\label{tuples-and-records}
Tuples and records are used for packaging multiple values together.
Tuples are enclosed in parentheses, while records are enclosed in curly
@ -264,6 +255,8 @@ of record components is not important. Examples:
{ x = 1, y = 2 } == { y = 2, x = 1 } // True
\end{verbatim}
\subsection{Accessing Fields}\label{accessing-fields}
The components of a record or a tuple may be accessed in two ways: via
pattern matching or by using explicit component selectors. Explicit
component selectors are written as follows:
@ -306,8 +299,30 @@ f p = x + y where
(x, y) = p
\end{verbatim}
\hypertarget{sequences}{%
\section{Sequences}\label{sequences}}
\subsection{Updating Fields}\label{updating-fields}
The components of a record or a tuple may be updated using the following
notation:
\begin{verbatim}
// Example values
r = { x = 15, y = 20 } // a record
t = (True,True) // a tuple
n = { pt = r, size = 100 } // nested record
// Setting fields
{ r | x = 30 } == { x = 30, y = 20 }
{ t | 0 = False } == (False,True)
// Update relative to the old value
{ r | x -> x + 5 } == { x = 20, y = 20 }
// Update a nested field
{ n | pt.x = 10 } == { pt = { x = 10, y = 20 }, size = 100 }
{ n | pt.x -> x + 10 } == { pt = { x = 25, y = 20 }, size = 100 }
\end{verbatim}
\section{Sequences}\label{sequences}
A sequence is a fixed-length collection of elements of the same type.
The type of a finite sequence of length \texttt{n}, with elements of
@ -368,16 +383,14 @@ There are also lifted pointwise operations.
p1 # p2 // Split sequence pattern
\end{verbatim}
\hypertarget{functions}{%
\section{Functions}\label{functions}}
\section{Functions}\label{functions}
\begin{verbatim}
\p1 p2 -> e // Lambda expression
f p1 p2 = e // Function definition
\end{verbatim}
\hypertarget{local-declarations}{%
\section{Local Declarations}\label{local-declarations}}
\section{Local Declarations}\label{local-declarations}
\begin{verbatim}
e where ds
@ -387,9 +400,7 @@ Note that by default, any local declarations without type signatures are
monomorphized. If you need a local declaration to be polymorphic, use an
explicit type signature.
\hypertarget{explicit-type-instantiation}{%
\section{Explicit Type
Instantiation}\label{explicit-type-instantiation}}
\section{Explicit Type Instantiation}\label{explicit-type-instantiation}
If \texttt{f} is a polymorphic value with type:
@ -404,9 +415,8 @@ you can evaluate \texttt{f}, passing it a type parameter:
f `{ tyParam = 13 }
\end{verbatim}
\hypertarget{demoting-numeric-types-to-values}{%
\section{Demoting Numeric Types to
Values}\label{demoting-numeric-types-to-values}}
Values}\label{demoting-numeric-types-to-values}
The value corresponding to a numeric type may be accessed using the
following notation:
@ -423,8 +433,7 @@ accommodate the value of the type:
`t : {n} (fin n, n >= width t) => [n]
\end{verbatim}
\hypertarget{explicit-type-annotations}{%
\section{Explicit Type Annotations}\label{explicit-type-annotations}}
\section{Explicit Type Annotations}\label{explicit-type-annotations}
Explicit type annotations may be added on expressions, patterns, and in
argument definitions.
@ -437,22 +446,19 @@ p : t
f (x : t) = ...
\end{verbatim}
\hypertarget{type-signatures}{%
\section{Type Signatures}\label{type-signatures}}
\section{Type Signatures}\label{type-signatures}
\begin{verbatim}
f,g : {a,b} (fin a) => [a] b
\end{verbatim}
\hypertarget{type-synonym-declarations}{%
\section{Type Synonym Declarations}\label{type-synonym-declarations}}
\section{Type Synonym Declarations}\label{type-synonym-declarations}
\begin{verbatim}
type T a b = [a] b
\end{verbatim}
\hypertarget{modules}{%
\section{Modules}\label{modules}}
\section{Modules}\label{modules}
A \textbf{\emph{module}} is used to group some related definitions.
@ -465,8 +471,7 @@ f : [8]
f = 10
\end{verbatim}
\hypertarget{hierarchical-module-names}{%
\section{Hierarchical Module Names}\label{hierarchical-module-names}}
\section{Hierarchical Module Names}\label{hierarchical-module-names}
Module may have either simple or \textbf{\emph{hierarchical}} names.
Hierarchical names are constructed by gluing together ordinary
@ -485,8 +490,7 @@ when searching for module \texttt{Hash::SHA256}, Cryptol will look for a
file named \texttt{SHA256.cry} in a directory called \texttt{Hash},
contained in one of the directories specified by \texttt{CRYPTOLPATH}.
\hypertarget{module-imports}{%
\section{Module Imports}\label{module-imports}}
\section{Module Imports}\label{module-imports}
To use the definitions from one module in another module, we use
\texttt{import} declarations:
@ -507,8 +511,7 @@ import M // import all definitions from `M`
g = f // `f` was imported from `M`
\end{verbatim}
\hypertarget{import-lists}{%
\section{Import Lists}\label{import-lists}}
\section{Import Lists}\label{import-lists}
Sometimes, we may want to import only some of the definitions from a
module. To do so, we use an import declaration with an
@ -532,8 +535,7 @@ Using explicit import lists helps reduce name collisions. It also tends
to make code easier to understand, because it makes it easy to see the
source of definitions.
\hypertarget{hiding-imports}{%
\section{Hiding Imports}\label{hiding-imports}}
\section{Hiding Imports}\label{hiding-imports}
Sometimes a module may provide many definitions, and we want to use most
of them but with a few exceptions (e.g., because those would result to a
@ -556,8 +558,7 @@ import M hiding (h) // Import everything but `h`
x = f + g
\end{verbatim}
\hypertarget{qualified-module-imports}{%
\section{Qualified Module Imports}\label{qualified-module-imports}}
\section{Qualified Module Imports}\label{qualified-module-imports}
Another way to avoid name collisions is by using a
\textbf{\emph{qualified}} import.
@ -600,8 +601,7 @@ Such declarations will introduces all definitions from \texttt{A} and
\texttt{X} but to use them, you would have to qualify using the prefix
\texttt{B:::}.
\hypertarget{private-blocks}{%
\section{Private Blocks}\label{private-blocks}}
\section{Private Blocks}\label{private-blocks}
In some cases, definitions in a module might use helper functions that
are not intended to be used outside the module. It is good practice to
@ -642,8 +642,7 @@ private
helper2 = 3
\end{verbatim}
\hypertarget{parameterized-modules}{%
\section{Parameterized Modules}\label{parameterized-modules}}
\section{Parameterized Modules}\label{parameterized-modules}
\begin{verbatim}
module M where
@ -661,9 +660,7 @@ f : [n]
f = 1 + x
\end{verbatim}
\hypertarget{named-module-instantiations}{%
\section{Named Module
Instantiations}\label{named-module-instantiations}}
\section{Named Module Instantiations}\label{named-module-instantiations}
One way to use a parameterized module is through a named instantiation:
@ -700,9 +697,8 @@ Note that the only purpose of the body of \texttt{N} (the declarations
after the \texttt{where} keyword) is to define the parameters for
\texttt{M}.
\hypertarget{parameterized-instantiations}{%
\section{Parameterized
Instantiations}\label{parameterized-instantiations}}
Instantiations}\label{parameterized-instantiations}
It is possible for a module instantiations to be itself parameterized.
This could be useful if we need to define some of a module's parameters
@ -737,9 +733,8 @@ In this case \texttt{N} has a single parameter \texttt{x}. The result of
instantiating \texttt{N} would result in instantiating \texttt{M} using
the value of \texttt{x} and \texttt{12} for the value of \texttt{y}.
\hypertarget{importing-parameterized-modules}{%
\section{Importing Parameterized
Modules}\label{importing-parameterized-modules}}
Modules}\label{importing-parameterized-modules}
It is also possible to import a parameterized module without using a
module instantiation:

Binary file not shown.

View File

@ -234,6 +234,10 @@ ordering of record components is not important. Examples:
{ x = 1, y = 2 } == { x = 1, y = 2 } // True
{ x = 1, y = 2 } == { y = 2, x = 1 } // True
Accessing Fields
----------------
The components of a record or a tuple may be accessed in two ways: via
pattern matching or by using explicit component selectors. Explicit
component selectors are written as follows:
@ -270,6 +274,31 @@ patterns use braces. Examples:
f p = x + y where
(x, y) = p
Updating Fields
---------------
The components of a record or a tuple may be updated using the following
notation:
// Example values
r = { x = 15, y = 20 } // a record
t = (True,True) // a tuple
n = { pt = r, size = 100 } // nested record
// Setting fields
{ r | x = 30 } == { x = 30, y = 20 }
{ t | 0 = False } == (False,True)
// Update relative to the old value
{ r | x -> x + 5 } == { x = 20, y = 20 }
// Update a nested field
{ n | pt.x = 10 } == { pt = { x = 10, y = 20 }, size = 100 }
{ n | pt.x -> x + 10 } == { pt = { x = 25, y = 20 }, size = 100 }
Sequences
=========

Binary file not shown.