Changes from 8a57f3b03992351f46c4537fd0d9351592278d2b to a6165d18b949f568caafe69ed3aeb7a771318114---
title: Name Resolver Specification
toc: false
toc: yes
...
> Back to [Clafer Tools](/ClaferTools/Overview)
This page describes how the Clafer compiler, as of version 0.5.7, determines which clafer a name refers to. The rules are those of `Language.Clafer.Intermediate.ResolverName` and `ResolverInheritance` in the [compiler](https://github.com/gsdlab/clafer). Every rule is illustrated by an example. The examples in the rendered blocks form the model of this page, which the wiki compiles; the examples that the compiler rejects are shown as plain text, with the compiler's message.
Introduction
============
The goal of the name resolver in the Clafer compiler is to determine which clafer in the model is referred to by a given reference by name. In a Clafer model, names can repeat as each clafer declaration includes a new namespace. For example, there can be many clafers called `X` in various places in the model. The clafer `X` can be referred to in these two ways:
In a Clafer model, names can repeat, since each clafer opens a new namespace: there can be many clafers called `x` in various places in the model. A name refers to a clafer in three places:
1. as a super clafer: `a : X` (done in `ResolveInheritance.hs`), `a -> X`, and `a ->> X` (`ResolveName.hs`), and
2. inside expressions in constraints and goals: `[ some X ]`, `X + 1`, and `<< max sum X.ref >>`.
1. as a super clafer: `a : X`;
2. as the target of a reference: `a -> X` and `a ->> X`;
3. in expressions of constraints, assertions and objectives: `[ some X ]`, `[ X.size > 1 ]`, `assert [ ... ]` and `<< min sum X.cost >>`.
The name resolver decides which of the potentially multiple `X`s is being referred to.
Clafer `X` can be resolved as
The name resolver decides which of the clafers called `X` is referred to, or reports that there is none, or that there are several.
* `Special` -- `this`, `parent`, `children`, `ref`
* `TypeSpecial` -- primitive type: integer, string, (need to add real, natural, etc.)
* `Binding` -- local variable (in constraints with quantifier)
* `Subclafers` -- in descendants (BFS)+ inheritance
* `Reference` -- same as `Subclafers` + reachable via references
* `Ancestor` -- in ancestors
* `AbsClafer` -- abstract , top-level clafer
* `TopClafer` -- concrete, top-level clafer
Seeing the result
-----------------
Currently (Clafer 0.3.3), the name resolver applies the following sequence of resolution strategies (in that exact order) given the current environment `env` and the clafer's `id`:
`clafer -m clafer` prints the *explicit model*: the model with every name resolved and every navigation written out from `this`, from a variable, or from the top level. For example, `[ some v ]` in `z` below is printed as `[(#this.parent.v) >= 1]`. In the explicit model, every clafer is named by its UID, which is its name qualified only as much as needed to be unique, with `__` between the names: `size` when no other clafer is called `size`, `x__size` for the `size` in `x` otherwise. A UID also appears in the paths that the error messages list.
`resolve env id [resolveSpecial, resolveBind, resolveDescendants, resolveAncestor pos, resolveTopLevel pos, resolveNone pos]`
The phases
==========
The strategy `resolveNone` reports an error that the clafer with the given `id` was not found.
The compiler resolves names in this order:
1. **UIDs.** Every clafer gets its UID.
2. **Supers** (`a : X`). The name of a super is looked up among the abstract clafers only (see [Supers](#supers)).
3. **Reference targets** (`a -> X`, `a ->> X`). The target is an expression, which is resolved globally, without the context of the clafer (see [Reference targets](#reference-targets)).
4. **Expressions** in constraints, assertions and objectives, in the context of the clafer that contains them (see [Expressions](#expressions)). First, siblings with the same name are reported as `Duplicate name: b`.
5. **Refinement checks.** A clafer that extends a nested abstract clafer must be properly nested, and a redefinition may only narrow the cardinality, turn a bag reference into a set reference, and narrow the target (see [Redefinition](#redefinition)).
6. **Types**, which add the implicit `dref`s, e.g., `[ link.speed = 3 ]` compares `link.dref.speed.dref`, and **the root**, which resolves the uses of `root` and of `parent` at the top level (see [`doc/root.md`](https://github.com/gsdlab/clafer/blob/master/doc/root.md)).
Test Cases
==========
With `--check-duplicates`, the compiler also requires every clafer name to be unique in the whole model (`--check-duplicates: Duplicate clafer names: b`).
Resolving Special
-----------------
Expressions
===========
* `this` refers to `S1`
The first name of a path
------------------------
The first name of a path, e.g., `a` in `a.b.c` or in `some a`, is resolved by the following strategies, in this order. The first strategy that finds the name wins; the result is named after the strategy (the `HowResolved` of the compiler).
| Strategy | Finds | The name becomes | When several match |
|---|---|---|---|
| `Special` | `this`, `parent`, `dref`, `root`, `clafer`, `noclafer`, `children` | itself | — |
| `TypeSpecial` | `integer`, `int`, `string`, `decimal`, `real`, `double` | the primitive type | — |
| `Binding` | a variable of a quantifier, innermost first | the variable | the innermost |
| `Subclafers` | a descendant of the context clafer, own or inherited, breadth first | `this.a.b` | the first found |
| `Ancestor` | a clafer in the subtree of an ancestor of the context, own or inherited | `this.parent.parent.x.b` | error |
| `TargetChild` | a child of the target of the context, when the context is a reference | `this.dref.b` | error |
| `AbsClafer` | a clafer in the subtree of an abstract top-level clafer, anywhere in the model | `A.b` | error |
| `TopClafer` | a clafer in the subtree of a concrete top-level clafer, anywhere in the model | `C.b` | error |
When no strategy finds the name, the compiler reports `Name resolver: 'a' not found within paths: ...`.
Note that `AbsClafer` and `TopClafer` search the whole model, not only the top-level clafers: a name that is unique in the model resolves wherever it is used. A name found in an abstract clafer's subtree hides the same name in a concrete clafer's subtree.
The names after a dot
---------------------
A name after a dot, e.g., `b` in `a.b`, is resolved among the children of the clafer reached so far, by these strategies:
| Strategy | Finds | The name becomes | When several match |
|---|---|---|---|
| `Special` | `parent`, `dref`, ... | itself | — |
| `Subclafers` | a child, own or inherited | `a.b` | error |
| `Reference` | a child of the reference's target, own or inherited | `a.dref.b` | error |
After `root.`, a name is resolved among the top-level clafers only.
Special names
-------------
* `this` is the context instance and `parent` is its parent.
```clafer
S1
[ this ]
Sp1
[ this = this ]
Sp2
sp2a
[ some parent ]
```
* `parent` refers to `S2`
`[ this = this ]` stays, and `[ some parent ]` becomes `[(#this.parent) >= 1]`.
* `root` is the root of the model, which contains the top-level clafers; `parent` of an instance of a top-level clafer is the root ([#112](https://github.com/gsdlab/clafer/issues/112)).
```clafer
S2
a
[ parent ]
Pt1 ?
Pt2
[ some parent.Pt1 ]
```
* `children` refers to `a ++ b`
`parent.Pt1` becomes `Pt1`.
* `noclafer` is the empty set, and `clafer` is the set of all instances.
* `children` is reserved, but the type checker rejects it:
```
S3
a
b
[ children ]
a ?
b ?
[ some children ]
```
results in `resolver: unknown uid 'children'`
results in `'children' is not a child of type 'S3'`.
Resolving TypeSpecial
---------------------
Primitive types
---------------
* `integer`, `string`, `real` resolve to primitive types
```clafer
Ty1 -> integer
Ty2 -> int
Ty3 -> string
```
`decimal`, `real` and `double` are primitive types too. A model with `decimal` has a Choco output but no Alloy output; a model with `real` or `double` has neither.
Bindings
--------
* `b` in `b.bic` is the variable `b`, and `bic` is the child of `Bi1` that `b` has by inheritance. The variable hides the top-level clafer `b`.
```clafer
age -> integer
age2 -> int
name -> string
abstract Bi1
bic ?
Bi2 : Bi1 2
b ?
[ all b : Bi1 | some b.bic ]
[ some b : Bi1 | some b.bic ]
```
* A variable can range over a path:
```clafer
Bq
bqb *
bqc ?
[ all x : Bq.bqb | some x.bqc ]
```
interestRate -> real
interestRate -> double
Subclafers
----------
* The descendants are searched breadth first, so the nearest one wins: `db` in `some db` is `De1.da.db`, not `De1.da.db.db`.
```clafer
De1
[ some db ]
da ?
db ?
db ?
```
results in `resolver: real not found within` and `resolver: double not found within`
* At the same depth, the first declared wins, without an error: `dc` is `this.dx.dc`.
Resolving Binding
-----------------
```clafer
De2
[ some dc ]
dx
dc ?
dy
dc ?
```
* `b` in `b.c` resolves to `b` in `b : B1`. `c` in `b.c` resolves to `B1.c` via inheritance.
* A shallower descendant wins over a deeper one, wherever it is: `dd` is `this.dz3.dd`.
```clafer
abstract B1
c ?
De3
[ some dd ]
dx3
dy3
dd ?
dz3
dd ?
```
[ all b : B1 | b.c ]
[ all disj b1; b2 : B1 | b1.c && b2.c ]
[ some b : B1 | b.c ]
[ no b : B1 | b.c ]
* Inherited children are descendants: `dq` is `this.dq`.
```clafer
abstract DeBase
dq ?
De4 : DeBase
[ some dq ]
```
Resolving Subclafers
--------------------
Ancestors
---------
* `b` in `some b` resolves to `a.b` and not to `a.b.b`
* An ancestor is found by its name: `An1` is `this.parent`.
```clafer
Sub
[ some b ]
a ?
b ?
b ?
An1 ?
an1b
[ some An1 ]
```
Resolving Reference
-------------------
* So is any clafer in the subtree of an ancestor, e.g., a sibling's child: `an2v` is `this.parent.an2x.an2v`.
* `a` in `some a` should resolve to `A5.a`
```clafer
An2
an2x
an2v ?
an2z
[ some an2v ]
```
* including the children that an ancestor inherits: `an3v` is `this.parent.an3v`.
```clafer
A5 *
a *
abstract AnBase
an3v ?
An3 : AnBase
an3z
[ some an3v ]
```
refA5 ->> A5
[ some this.a ]
* The name must be unique in the subtree of the top-level ancestor. A nearer clafer does not hide a farther one: `v` below is in the parent `x` and in its sibling `y`.
```
Car
x
v ?
z
[ some v ]
y
v ?
```
* `B` in `-> A.B` should resolve to `A.B`
results in
```
refAB -> A.B *
[ some B ]
clafer "v" is not unique. Available paths:
Car.x.x__v
Car.y.y__v
```
results in an error `B` not found
* For the same reason, a clafer with the name of its ancestor makes the name ambiguous:
Resolving Ancestor
------------------
```
A2 ?
A2 ?
B
[ some A2 ]
```
* `A1` in `some A1` resolves to the top-level `A1`
results in
```
clafer "A2" is not unique. Available paths:
root__A2.A2__A2
root__A2
```
* An ancestor's subtree wins over the rest of the model: `an4v` is `this.parent.an4x.an4v`, although a top-level `an4v` exists.
```clafer
A1 ?
B
[ some A1 ]
an4v ?
An4
an4x
an4v ?
an4z
[ some an4v ]
```
* `A2` in `some A2` resolves to `A2.A2` and not the top-level `A2`
Children of a reference's target
--------------------------------
In a reference clafer, a child of the target is found as if it were a child of the reference: `machine` is `this.dref.machine`.
```clafer
abstract Service
machine ?
services -> Service *
[ some machine ]
Svc1 : Service
```
A2 ?
A2 ?
B
[ some A2 ]
The whole model
---------------
* A clafer anywhere in the model is found by its name: `Gl1` is `Gl1`, and `glEngine` is `Gl2.glEngine`.
```clafer
Gl1 ?
[ some Gl1 ]
Gl2
glEngine ?
Gl3
[ some glEngine ]
```
results in an unexplained compilation error.
* The subtrees of the abstract clafers are searched first: `glw` is `GlA.glw`, the `glw` of all instances of `GlA`, and the `glw` of `Gl4` is not considered.
Resolving AbsClafer
-----------------
```clafer
abstract GlA
glw ?
Gl4
glw ?
Gl5
[ some glw ]
```
* `Abs` in `: Abs`, `-> Abs`, and `->> Abs` resolves to `abstract Abs`
* Otherwise, the name must be unique in the model:
```
Car
engine ?
Boat
engine ?
Bike
[ some engine ]
```
results in
```
clafer "engine" is not unique. Available paths:
Car.Car__engine
Boat.Boat__engine
```
* A constraint at the top level has no context, so only the whole model is searched:
```
A
b ?
B
b ?
[ some b ]
```
results in `clafer "b" is not unique`.
Navigation
----------
* After a dot, a name is a child, own or inherited, of the clafer reached so far.
```clafer
abstract Abs
abstract NvP
nvq ?
NvR : NvP
nvs ?
nvt ?
[ some NvR.nvq ]
[ some NvR.nvs ]
```
AbsColon : Abs
AbsSet -> Abs
AbsMultiSet ->> Abs
* A grandchild is not a child:
```
R
s
t ?
[ some R.t ]
```
* nested abstract
results in `Name resolver: 't' not found within paths:R`.
* After a reference, a name is a child of the target: `link.speed` becomes `link.dref.speed`, the same as `link.dref.speed`. This holds for `this` too: `this.a` in `refA5` is `this.dref.a`.
```clafer
abstract Year
abstract Month
abstract Port
speed -> integer
link -> Port
Port1 : Port
[ link.speed = 3 ]
[ link.dref.speed = 3 ]
A5 *
a *
refA5 ->> A5
[ some this.a ]
[ some a ]
```
y2013 : Year
month : Month 12
The bare `a` in `refA5` resolves to `this.dref.a` as well, as a child of the target.
// this is not allowed - improper nesting
// myMonth : Month
* After `root.`, a name is a top-level clafer, even when a nested clafer has the same name ([#57](https://github.com/gsdlab/clafer/issues/57)): in `Rt2`, `Rt1` is `this.Rt1` and `root.Rt1` is the top-level `Rt1`.
```clafer
Rt1 ?
Rt2
Rt1 ?
[ some Rt1 ]
[ some root.Rt1 ]
```
Resolving TopClafer
-------------------
* A nested clafer is not a top-level clafer:
* `T` in `some T` resolves to `T ?`
```
A
b ?
[ some root.b ]
```
results in `Name resolver: 'b' not found within paths:`.
Supers
======
* The name of a super is looked up among the abstract clafers, wherever they are, and must be unique among them. `:`, `->` and `->>` can all refer to an abstract clafer.
```clafer
T ?
abstract SuAbs
SuColon : SuAbs
SuSet -> SuAbs
SuBag ->> SuAbs
```
[ some T ]
* A concrete clafer cannot be a super:
```
C
D : C
```
Resolving Redefinition
----------------------
results in `No superclafer found: C`.
* (this affects name resolution for inheritance)
* A nested abstract clafer can be extended inside a clafer that extends its parent ([#67](https://github.com/gsdlab/clafer/issues/67), [#78](https://github.com/gsdlab/clafer/issues/78)):
```clafer
abstract Year
abstract Month
y2013 : Year
month : Month 12
```
but not elsewhere:
```
abstract Year
abstract Month
myMonth : Month
```
results in `Improperly nested clafer 'myMonth'`.
* Two abstract clafers with the same name cannot be told apart as supers:
```
abstract A
d 0..1
abstract M
abstract B
abstract M
c : M
```
/* redefines A.d, narrows cardinality */
results in `clafer "M" cannot be defined because the name should be unique in the same namespace. Available paths: A__M B__M`. (The message is misleading: the two `M`s are in different namespaces, but `M` is ambiguous as a super.)
abstract B : A
d 1..1
b ?
Redefinition
------------
refA -> A
A clafer with the name of an inherited abstract clafer, which it extends, *redefines* it: `rd : rd` in `RdB` extends the `rd` that `RdB` inherits from `RdA`, even if other abstract clafers are called `rd`. The redefinition may narrow the cardinality (`*` to `2`), turn a bag reference into a set reference (`->>` to `->`), and narrow the target to a subclafer of the target.
[ refA.ref.d.b ] this should produce error 'b' not found
```clafer
abstract RdA
abstract rd *
abstract RdB : RdA
rd : rd 2
rdb ?
[ some rdb ]
RdC : RdB
[ some rd ]
```
refB -> B
In `RdC`, `rd` is the redefined `rd` of `RdB`, found first among the descendants.
[ refB.ref.d.b ] // this should work
A redefinition that widens the cardinality is refused:
```
abstract A
abstract d ?
B : A
d : d 2
```
Related BugBot Issues
=====================
results in `Improper cardinality refinement for clafer 'd'`.
* [231 name resolver should search through nested clafers](http://gsd.uwaterloo.ca:8888/question/962/name-resolver-should-search-through-nested-clafers)
* [217 reference to nested clafers](http://gsd.uwaterloo.ca:8888/question/918/reference-to-nested-clafers)
* [216 inheritance among concrete clafers](http://gsd.uwaterloo.ca:8888/question/917/inheritance-among-concrete-clafers)
* [206 Parent Required when Defining Ad-Hoc Set](http://gsd.uwaterloo.ca:8888/question/891/parent-required-when-defining-ad-hoc-set)
* [169 No match in record selector sident](http://gsd.uwaterloo.ca:8888/question/729/no-match-in-record-selector-sident)
* [145 Quantify over union](http://gsd.uwaterloo.ca:8888/question/675/quantify-over-union)
* [95 children clafers not visible via reference to a reference](http://gsd.uwaterloo.ca:8888/question/451/children-clafers-not-visible-via-reference-to-a)
* [65 summary working with local clafers and global](http://gsd.uwaterloo.ca:8888/question/268/summary-working-with-local-clafers-and-global)
* [60 add support for hierarchy specification and instantiation](http://gsd.uwaterloo.ca:8888/question/252/add-support-for-hierarchy-specification-and)
* [48 add the lookup navigation operator to clafer](http://gsd.uwaterloo.ca:8888/question/212/add-the-lookup-navigation-operator-to-clafer)
See also `test/positive/redefinition.cfr` in the compiler.
Issues 48 and 65 are just proposals, we have not decided yet.
Reference targets
=================
The target of a reference is resolved globally, as at the top level: the clafers around the reference are not considered. In `link -> port` below, `port` is not `Car.port`, but any clafer called `port`:
```
Car
port
link -> port
Boat
port
```
results in
```
clafer "port" is not unique. Available paths:
Car.Car__port
Boat.Boat__port
```
A path from the top level names the target:
```clafer
RefCar
refPort
refLink -> RefCar.refPort
RefBoat
refPort
```
A path from `this` or `parent` cannot be used: `link -> parent.port` results in `Name resolver: 'port' not found within paths:`.
Known limitations
=================
* **Descendants at the same depth.** The first one declared wins, silently ([De2](#subclafers)), while everywhere else a name that matches several clafers is an error.
* **A nearer clafer does not hide a farther one** in the subtree of the top-level ancestor ([Ancestors](#ancestors)); the name must be qualified.
* **Navigating to a redefined clafer.** After a dot, the redefined clafer and the clafer it redefines are both children, so the name is ambiguous: `RdC.rd` results in `clafer "rd" cannot be defined because the name should be unique in the same namespace` with the paths of both. A bare `rd` in `RdC` works, since descendants take the first found.
* **The same name in a super and a sub without redefinition.** When `abstract A` and `abstract B : A` both declare a concrete `Item`, an instance of `C : B` has both, and a bare `Item` in `C` is `B`'s, found first; `C.Item` is ambiguous.
* **Reference targets ignore the context** ([Reference targets](#reference-targets)).
* **`children`** is reserved but rejected by the type checker.
Related issues
==============
* [#57 the root keyword](https://github.com/gsdlab/clafer/issues/57): `root.b` for a top-level `b` that a nested `b` hides
* [#67](https://github.com/gsdlab/clafer/issues/67) and [#78](https://github.com/gsdlab/clafer/issues/78): extending a nested abstract clafer
* [#112](https://github.com/gsdlab/clafer/issues/112): `parent` at the top level is the root
The positive tests of the compiler that concern names: `resolution.cfr`, `resolve_name_in_ancestor.cfr`, `same_child_name_in_super_and_sub.cfr`, `redefinition.cfr`, `extends_nested_abstract_in_context.cfr`, `gi57-root-keyword.cfr`, `parent_of_top_level.cfr` and `unique_names_for_same_names.cfr`.
```{.clafer .summary}
```