- Overview
- Tutorials
- Getting started
- Get started with Canton and the JSON Ledger API
- Get Started with Canton, the JSON Ledger API, and TypeScript
- Get started with Canton Network App Dev Quickstart
- Using the JSON Ledger API
- Get started with smart contract development
- Basic contracts
- Test templates using Daml scripts
- Build the Daml Archive (.dar) file
- Data types
- Transform contracts using choices
- Add constraints to a contract
- Parties and authority
- Compose choices
- Handle exceptions
- Work with dependencies
- Functional programming 101
- The Daml standard library
- Test Daml contracts
- Next steps
- Application development
- Getting started
- Development how-tos
- Component how-tos
- Explanations
- References
- Application development
- Smart contract development
- Daml language cheat sheet
- Daml language reference
- Overview: Template Structure
- Reference: Templates
- Reference: Choices
- Reference: Updates
- Reference: Data Types
- Reference: Built-in Functions
- Reference: Expressions
- Reference: Functions
- Reference: Daml File Structure
- Reference: Daml Packages
- Reference: Contract Keys
- Reference: Interfaces
- Reference: Exceptions (Deprecated)
- Fixity, Associativity and Precedence
- Daml standard library
- DA.Action.State.Class
- DA.Action.State
- DA.Action
- DA.Assert
- DA.Bifunctor
- DA.Crypto.Text
- DA.Date
- DA.Either
- DA.Exception
- DA.Fail
- DA.Foldable
- DA.Functor
- DA.Internal.Interface.AnyView.Types
- DA.Internal.Interface.AnyView
- DA.List.BuiltinOrder
- DA.List.Total
- DA.List
- DA.Logic
- DA.Map
- DA.Math
- DA.Monoid
- DA.NonEmpty.Types
- DA.NonEmpty
- DA.Numeric
- DA.Optional
- DA.Record
- DA.Semigroup
- DA.Set
- DA.Stack
- DA.Text
- DA.TextMap
- DA.Time
- DA.Traversable
- DA.Tuple
- DA.Validation
- GHC.Show.Text
- GHC.Tuple.Check
- Prelude
- Daml Script
- Smart contract upgrading reference
- Glossary of concepts
Fixity, Associativity and Precedence¶
With normal, prefix operators (e.g. functions), the semantics of f g h is
clear: f is a function that takes g and h as parameters. If we want
f to take the result of applying g to h we write f (g h).
In the case of infix operators (e.g. symbol operators such as + and *,
or functions surrounded by backticks, for example `elem`), it is less clear.
What does x - y - z mean? Subtracting x from y first (i.e.
(x - y) - z) generally yields different results than subtracting z from
y first (i.e. x - (y - z)). In Daml, the subtraction operator - is
defined as a left-assocative operator. That is, when we write x - y - z -
... the parser associates to the left, meaning the parser interprets this as
((x - y) - z) - ....
Some operators are right-associative. We have already encountered one:
function application! A function signature of a -> b -> c -> ... is parsed
as (a -> (b -> (c -> ...))).
Finally, some operators are non-associative. A good example are comparison
operators such as == and >. This means any ambiguous usage of these
operators (e.g. a == b == c or a > b > c) results in a parse error.
Note
Non-associative operators are not to be confused with operators that are both
left- and right-associative, such as + (since (x + y) + z = x + (y +
z))). To obtain a deterministic parser, such operators must be declared as
one of either left-associative or right-associative. In Daml the +
operator has been declared as left-associative
The precedence of operators defines, when combining different operators, which
operator is processed first. For example, in general (and in Daml),
multiplication takes precedence over addition. That is, x + y * z is
parsed as x + (y * z). Operator precedence is expressed as a number, where a
higher number indicates a higher precedence. Operators of same precedence are
associated to the left (e.g. x + y - z is parsed as (x + y) - z.
The fixity and precedence of an operator are declared using the infixl,
infix, and infixr keywords (denoting left-, non-, and
right-associativity, respectfully) that take an integer between 0 and 9
inclusive and an operator the fixity applies to. For example, infixl 6 +
declares that + is a left-associative operator with precedence 6.
These keywords can be used for user-defined operators as well. The following
table shows the fixity and precedence for operators that are built-in to the
Daml language, such as + and -:
Precedence |
Left-associative |
Non-associative |
Right-associative |
|---|---|---|---|
9 |
|
|
|
8 |
|
||
7 |
|
||
6 |
|
|
|
5 |
|
||
4 |
|
|
|
3 |
|
||
2 |
|
||
1 |
|
|
|
0 |
|