Revision a6165d18b949f568caafe69ed3aeb7a771318114 (click the page title to view the current version)
Name Resolver Specification
Back to Clafer Tools
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. 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
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:
- as a super clafer:
a : X; - as the target of a reference:
a -> Xanda ->> X; - 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 clafers called X
is referred to, or reports that there is none, or that there are
several.
Seeing the result
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.
The phases
The compiler resolves names in this order:
- UIDs. Every clafer gets its UID.
- Supers (
a : X). The name of a super is looked up among the abstract clafers only (see Supers). - Reference targets (
a -> X,a ->> X). The target is an expression, which is resolved globally, without the context of the clafer (see Reference targets). - Expressions in constraints, assertions and
objectives, in the context of the clafer that contains them (see Expressions). First, siblings with the same name
are reported as
Duplicate name: b. - 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).
- Types, which add the implicit
drefs, e.g.,[ link.speed = 3 ]compareslink.dref.speed.dref, and the root, which resolves the uses ofrootand ofparentat the top level (seedoc/root.md).
With --check-duplicates, the compiler also requires
every clafer name to be unique in the whole model
(--check-duplicates: Duplicate clafer names: b).
Expressions
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
thisis the context instance andparentis its parent.
[ this = this ] stays, and [ some parent ]
becomes [(#this.parent) >= 1].
rootis the root of the model, which contains the top-level clafers;parentof an instance of a top-level clafer is the root (#112).
parent.Pt1 becomes Pt1.
noclaferis the empty set, andclaferis the set of all instances.childrenis reserved, but the type checker rejects it:
S3
a ?
b ?
[ some children ]
results in 'children' is not a child of type 'S3'.
Primitive types
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
binb.bicis the variableb, andbicis the child ofBi1thatbhas by inheritance. The variable hides the top-level claferb.
- A variable can range over a path:
Subclafers
- The descendants are searched breadth first, so the nearest one wins:
dbinsome dbisDe1.da.db, notDe1.da.db.db.
- At the same depth, the first declared wins, without an error:
dcisthis.dx.dc.
- A shallower descendant wins over a deeper one, wherever it is:
ddisthis.dz3.dd.
- Inherited children are descendants:
dqisthis.dq.
Ancestors
- An ancestor is found by its name:
An1isthis.parent.
- So is any clafer in the subtree of an ancestor, e.g., a sibling’s
child:
an2visthis.parent.an2x.an2v.
- including the children that an ancestor inherits:
an3visthis.parent.an3v.
- The name must be unique in the subtree of the top-level ancestor. A
nearer clafer does not hide a farther one:
vbelow is in the parentxand in its siblingy.
Car
x
v ?
z
[ some v ]
y
v ?
results in
clafer "v" is not unique. Available paths:
Car.x.x__v
Car.y.y__v
- For the same reason, a clafer with the name of its ancestor makes the name ambiguous:
A2 ?
A2 ?
B
[ some A2 ]
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:
an4visthis.parent.an4x.an4v, although a top-levelan4vexists.
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.
The whole model
- A clafer anywhere in the model is found by its name:
Gl1isGl1, andglEngineisGl2.glEngine.
- The subtrees of the abstract clafers are searched first:
glwisGlA.glw, theglwof all instances ofGlA, and theglwofGl4is not considered.
- 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.
- A grandchild is not a child:
R
s
t ?
[ some R.t ]
results in
Name resolver: 't' not found within paths:R.
- After a reference, a name is a child of the target:
link.speedbecomeslink.dref.speed, the same aslink.dref.speed. This holds forthistoo:this.ainrefA5isthis.dref.a.
The bare a in refA5 resolves to
this.dref.a as well, as a child of the target.
- After
root., a name is a top-level clafer, even when a nested clafer has the same name (#57): inRt2,Rt1isthis.Rt1androot.Rt1is the top-levelRt1.
- A nested clafer is not a top-level clafer:
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.
- A concrete clafer cannot be a super:
C
D : C
results in No superclafer found: C.
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
abstract M
abstract B
abstract M
c : M
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 Ms are in different
namespaces, but M is ambiguous as a super.)
Redefinition
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.
In RdC, rd is the redefined rd
of RdB, found first among the descendants.
A redefinition that widens the cardinality is refused:
abstract A
abstract d ?
B : A
d : d 2
results in
Improper cardinality refinement for clafer 'd'.
See also test/positive/redefinition.cfr in the
compiler.
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:
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), 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); 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.rdresults inclafer "rd" cannot be defined because the name should be unique in the same namespacewith the paths of both. A barerdinRdCworks, since descendants take the first found. - The same name in a super and a sub without
redefinition. When
abstract Aandabstract B : Aboth declare a concreteItem, an instance ofC : Bhas both, and a bareIteminCisB’s, found first;C.Itemis ambiguous. - Reference targets ignore the context (Reference targets).
childrenis reserved but rejected by the type checker.
Related issues
- #57 the root
keyword:
root.bfor a top-levelbthat a nestedbhides - #67 and #78: extending a nested abstract clafer
- #112:
parentat 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.
