--- title: Name Resolver Specification 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 ============ 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`; 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 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: 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)). 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 ------------- * `this` is the context instance and `parent` is its parent. ```clafer Sp1 [ this = this ] Sp2 sp2a [ some parent ] ``` `[ 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 Pt1 ? Pt2 [ some parent.Pt1 ] ``` `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 ? [ some children ] ``` results in `'children' is not a child of type 'S3'`. 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 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 ] ``` 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 ? ``` * At the same depth, the first declared wins, without an error: `dc` is `this.dx.dc`. ```clafer De2 [ some dc ] dx dc ? dy dc ? ``` * A shallower descendant wins over a deeper one, wherever it is: `dd` is `this.dz3.dd`. ```clafer De3 [ some dd ] dx3 dy3 dd ? dz3 dd ? ``` * Inherited children are descendants: `dq` is `this.dq`. ```clafer abstract DeBase dq ? De4 : DeBase [ some dq ] ``` Ancestors --------- * An ancestor is found by its name: `An1` is `this.parent`. ```clafer An1 ? an1b [ some An1 ] ``` * So is any clafer in the subtree of an ancestor, e.g., a sibling's child: `an2v` is `this.parent.an2x.an2v`. ```clafer An2 an2x an2v ? an2z [ some an2v ] ``` * including the children that an ancestor inherits: `an3v` is `this.parent.an3v`. ```clafer abstract AnBase an3v ? An3 : AnBase an3z [ some an3v ] ``` * 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 ? ``` 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: `an4v` is `this.parent.an4x.an4v`, although a top-level `an4v` exists. ```clafer an4v ? An4 an4x an4v ? an4z [ some an4v ] ``` 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 ``` 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 ] ``` * 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. ```clafer abstract GlA glw ? Gl4 glw ? Gl5 [ some glw ] ``` * 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 NvP nvq ? NvR : NvP nvs ? nvt ? [ some NvR.nvq ] [ some NvR.nvs ] ``` * 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.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 Port speed -> integer link -> Port Port1 : Port [ link.speed = 3 ] [ link.dref.speed = 3 ] A5 * a * refA5 ->> A5 [ some this.a ] [ some 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](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 ] ``` * 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. ```clafer abstract SuAbs SuColon : SuAbs SuSet -> SuAbs SuBag ->> SuAbs ``` * A concrete clafer cannot be a super: ``` C D : C ``` results in `No superclafer found: C`. * 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 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 `M`s 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. ```clafer abstract RdA abstract rd * abstract RdB : RdA rd : rd 2 rdb ? [ some rdb ] RdC : RdB [ some rd ] ``` 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: ```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} ```