# Concepts

> Declare the kinds of things in your domain with concepts, and declare which models satisfy them

Source: https://taxilang.org/docs/language/concepts

A concept names a kind of thing in your domain, like a person, a company or a bond.
It lists the values that thing has, without saying how they're structured.

```taxi
type FirstName inherits String
type LastName inherits String
type DateOfBirth inherits Date
type EmailAddress inherits String
type PhoneNumber inherits String

[[ A natural person. ]]
concept Person {
   FirstName,
   LastName,
   DateOfBirth,
   EmailAddress?,
   PhoneNumber[]
}
```

Models then declare that they `satisfy` the concept, and the compiler checks that they have what it needs.
This page covers both parts.

For how concepts fit with intents and relationships, see the [overview](https://taxilang.org/docs/language/ontology).

## Declaring a concept

A concept has a name and a list of members. Each member is a type, not a field: there are no field names.
Separate members with commas.

Members can be:

- semantic types, like `FirstName`
- enums
- other concepts

A member can't be a model, or a union or intersection of types. The compiler reports `CONCEPT_MEMBER_INVALID_TYPE` for these.
Each type can only appear once in a concept (`CONCEPT_DUPLICATE_MEMBER`).

You can add docs to a concept and to each of its members, using `[[ ]]`, as you would for a type or a field.

### How many of each member

Each member says how many values of that type a thing has. This is called its cardinality.

| Member | Meaning |
|---|---|
| `FirstName` | Exactly one |
| `EmailAddress?` | Zero or one |
| `PhoneNumber[]` | One or more, collected from everywhere they appear |

In the `Person` concept above, a person has exactly one first name, last name and date of birth.
They may have an email address, and they have at least one phone number.

These rules matter when a model satisfies the concept. See [How members are matched to fields](#how-members-are-matched-to-fields).

### Concepts as members

A member can be another concept. This says that one thing contains another:

```taxi
type EmployeeId inherits String
type LegalEntityName inherits String

concept LegalEntity { LegalEntityName }
concept Employment { EmployeeId, LegalEntity }
```

Here, an employment has an employee id and a legal entity.

## Building on other concepts with `inherits`

A concept can build on one or more other concepts using `inherits`. It gets all of their members, and can add its own.

```taxi
type FirstName inherits String
type LastName inherits String
type EmailAddress inherits String
type EmployeeId inherits String

concept Person { FirstName, LastName }
concept Contactable { EmailAddress }

concept Employee inherits Person, Contactable {
   EmployeeId
}
```

`Employee` has four members: `FirstName`, `LastName`, `EmailAddress` and `EmployeeId`.

A concept can only inherit from other concepts (`CONCEPT_INHERITS_NON_CONCEPT`).

### Narrowing an inherited member

A child concept can restate an inherited member to make it stricter.
You can go from `T[]` to `T?` to `T`, but not the other way.

```taxi
type PhoneNumber inherits String

concept Person { PhoneNumber[] }

// An employee has at most one phone number
concept Employee inherits Person { PhoneNumber? }
```

Making a member less strict is an error (`CONCEPT_MEMBER_WIDENED`).

### When two parents disagree

If two parents both have the same member, with the same cardinality, they merge into one member.

If they have the same member with different cardinality, the compiler reports `CONCEPT_MEMBER_CARDINALITY_CONFLICT`. Fix it by restating the member in the child:

```taxi
type EmailAddress inherits String

concept Person { EmailAddress? }
concept Contactable { EmailAddress[] }

// Restating EmailAddress chooses exactly one
concept Employee inherits Person, Contactable { EmailAddress }
```

## Models that satisfy a concept

A model declares that it satisfies a concept with `satisfies`:

```taxi
type FirstName inherits String
type LastName inherits String
type DateOfBirth inherits Date
type EmailAddress inherits String
type PhoneNumber inherits String

concept Person {
   FirstName,
   LastName,
   DateOfBirth,
   EmailAddress?,
   PhoneNumber[]
}

model Human satisfies Person {
   names: {
      first: FirstName
      surname: LastName
   }
   dob: DateOfBirth
   contact: EmailAddress?
   phones: PhoneNumber[]
}
```

The compiler checks every member of `Person` against the fields of `Human`:

- `FirstName` and `LastName` are found in the nested `names` object.
- `DateOfBirth` is found in `dob`.
- `EmailAddress?` is found in `contact`. It's optional in the model, which is fine for an optional member.
- `PhoneNumber[]` is found in `phones`.

Field names don't matter. The compiler matches on types.

If a member can't be found, the compiler reports `CONCEPT_NOT_SATISFIED`, and lists the fields it did find.
For example, if `Human` only had the fields `first: FirstName?`, `dob` and `phones`:

```
Human does not satisfy Person: no field provides FirstName (exactly one).
first: FirstName? can't be used, as it is optional, and FirstName is required.
Available fields: first: FirstName?, dob: DateOfBirth, phones: PhoneNumber[]
```

A few more things to know:

- A model can satisfy several concepts: `model Staff satisfies Person, Contactable`.
- A model that satisfies a child concept also satisfies its parents. A model that satisfies `Employee` is a `Person` too.
- A model can use `inherits` and `satisfies` together: `model Human inherits Base satisfies Person`. Fields from `Base` count.
- Satisfying a concept doesn't give the model any fields, and doesn't make the model a subtype of the concept.
- Only models can satisfy concepts. `satisfies` on a `type` is an error (`SATISFIES_ON_SCALAR`).

> **Note: Satisfaction must be declared**
>
> A model only satisfies a concept if it declares `satisfies`. A model that happens to have all the right fields, but doesn't declare it, isn't treated as satisfying the concept. Matching models to concepts by their structure alone isn't supported yet.

### How members are matched to fields

The compiler looks through all of a model's fields, including nested objects and inherited fields.
Fields whose type is the member type, or inherits from it, are candidates.

Which candidates count depends on the member's cardinality:

| Member | Which fields count | Satisfied when |
|---|---|---|
| `T` | Required fields that aren't inside a collection | Exactly one field |
| `T?` | Required or optional fields that aren't inside a collection | Zero or one field |
| `T[]` | Every field: required, optional, collections, and fields inside collections | At least one field. All of them are collected. |

So:

- An optional field (`email: EmailAddress?`) can't satisfy a required member (`EmailAddress`).
- A required field can satisfy an optional member.
- A field inside a collection (`addresses: { phone: PhoneNumber }[]`) can't satisfy a single member. It can satisfy a `[]` member.
- A single field (`mobile: PhoneNumber`) can satisfy a `[]` member.

For a `[]` member, several fields are never a problem. The values from all of them are collected together:

```taxi
type PhoneNumber inherits String

concept Contactable { PhoneNumber[] }

model Contact satisfies Contactable {
   mobile: PhoneNumber
   home: PhoneNumber?
   others: PhoneNumber[]
   addresses: { phone: PhoneNumber }[]
}
```

All four fields provide `Contactable`'s phone numbers.

### Members that are concepts

When a member is a concept, it's satisfied by a field whose type is a model that satisfies that concept:

```taxi
type EmployeeId inherits String
type LegalEntityName inherits String

concept LegalEntity { LegalEntityName }
concept Employment { EmployeeId, LegalEntity }

model Employer satisfies LegalEntity {
   name: LegalEntityName
}

model Job satisfies Employment {
   id: EmployeeId
   employer: Employer
}
```

`Job` satisfies `Employment` because `employer` is an `Employer`, which satisfies `LegalEntity`.

The field's model must declare `satisfies LegalEntity`. The enclosing model's own fields don't count: putting a `name: LegalEntityName` field directly on `Job` wouldn't satisfy the `LegalEntity` member.

## Resolving ambiguous fields

If more than one field could satisfy a single member (`T` or `T?`), the compiler can't choose. It reports `CONCEPT_MEMBER_AMBIGUOUS`, and names every candidate.

Pick the right field by adding `satisfies Concept::Member` to it:

```taxi
type FirstName inherits String
type LastName inherits String
type DateOfBirth inherits Date
type EmailAddress inherits String
type PhoneNumber inherits String

concept Person {
   FirstName,
   LastName,
   DateOfBirth,
   EmailAddress?,
   PhoneNumber[]
}

model Contact satisfies Person {
   first: FirstName
   last: LastName
   dob: DateOfBirth
   primary: EmailAddress satisfies Person::EmailAddress
   secondary: EmailAddress
   phones: PhoneNumber[]
}
```

`Contact` has two email fields. The binding on `primary` says it's the one that provides `Person`'s `EmailAddress`. The `secondary` field is ignored for that member.

Without the binding, the compiler reports:

```
Contact does not satisfy Person: EmailAddress (zero or one) is ambiguous, as it could
come from any of primary: EmailAddress, secondary: EmailAddress.
Resolve with 'satisfies Person::EmailAddress' on the intended field, or introduce a more specific type
```

As the message says, the other fix is a more specific type, like `type PrimaryEmailAddress inherits EmailAddress`.

Ambiguity can come from inheritance too. If `FirstName` and `LastName` both inherit from `Name`, then a concept member `Name` matches both a `FirstName` field and a `LastName` field.

### Rules for bindings

- The concept must be one the model declares in `satisfies`, or one of its parent concepts. For example, a model that satisfies `Employee` can bind `Person::EmailAddress` if `Employee inherits Person`. Otherwise the compiler reports `MEMBER_BINDING_UNDECLARED_CONCEPT`.
- The type after `::` must be a member of the concept (`MEMBER_BINDING_INVALID_TARGET`).
- The field must be able to provide the member: its type must match, and it must meet the member's cardinality (`MEMBER_BINDING_TYPE_MISMATCH`). For example, an optional field can't be bound to a required member.
- Only one field can bind each member (`MEMBER_BINDING_DUPLICATE`).
- You can't bind a `[]` member, as it collects every field anyway (`MEMBER_BINDING_ON_GATHER`).
- Bindings are only allowed in models that declare `satisfies` (`MEMBER_BINDING_WITHOUT_SATISFIES`).
- Bindings work on fields inside nested objects too.

A single field can bind members of several concepts:

```taxi
type EmailAddress inherits String

concept Contactable { EmailAddress }
concept Mailable { EmailAddress }

model Staff satisfies Contactable, Mailable {
   home: EmailAddress
   work: EmailAddress satisfies Contactable::EmailAddress, Mailable::EmailAddress
}
```

A binding only applies to the concepts it names. If `work` only named `Contactable::EmailAddress`, then `Mailable`'s `EmailAddress` would still be ambiguous.

## Where concepts can be used

A concept has no data of its own, so it can't be used anywhere a value would be created.
The compiler reports `CONCEPT_NOT_CONSTRUCTIBLE` if you use a concept as:

- a model field type (including `Person[]` and `Array<Person>`)
- an operation parameter or return type
- a function parameter or return type
- a query parameter
- a `given` value in a query
- the parent of a model or type (`model Employee inherits Person`). Use `satisfies` instead.

Concepts can be used:

- as members of other concepts
- as parents of other concepts
- in `satisfies` on a model
- as intent parameters and return types. See [Intents](https://taxilang.org/docs/language/intents).
- as the two ends of a relationship. See [Relationships](https://taxilang.org/docs/language/relationships).
- in `find` queries

You also can't use `type extension` on a concept (`CONCEPT_NOT_EXTENSIBLE`), or give a concept type arguments (`CONCEPT_GENERICS_UNSUPPORTED`).

## Querying concepts

You can ask for a concept in `find`, the same way you would ask for a model:

```taxi
type PersonId inherits String
type FirstName inherits String
type LastName inherits String
type EmailAddress inherits String

concept Person { PersonId, FirstName, LastName }

model CrmContact satisfies Person {
   id: PersonId
   first: FirstName
   last: LastName
   email: EmailAddress
}

service CrmApi {
   operation contact(PersonId): CrmContact
}
```

```taxi
find { Person }
find { Person[] }
find { Person( FirstName == 'Jane' ) }
find { Person } as { name: FirstName }
```

The result is data from a model that satisfies `Person`, like `CrmContact`. You can project it with `as`, as you would any other result.

Criteria in `find { Person( ... ) }` can only refer to members of the concept. `find { Person( EmailAddress == 'jane@acme.com' ) }` is an error (`CONCEPT_MEMBER_NOT_FOUND`), because `EmailAddress` isn't a member of `Person`. This is true even though `CrmContact` has an email address.

If several models that satisfy `Person` can be reached from your query's inputs, the query is ambiguous. Narrow it down by restricting the services it uses with `using { }`.

> **Warning: Query execution is coming**
>
> Queries for concepts compile, and they're type-checked against your schema. Support for running them is still being built in the query engine.

## Next steps

- [Intents](https://taxilang.org/docs/language/intents): declaring actions in terms of concepts
- [Relationships](https://taxilang.org/docs/language/relationships): declaring how concepts are linked, and narrowing those links in child concepts
