2013-12-27 04:21:57 +00:00
# Expressions
Lean is based on dependent type theory, and is very similar to the one
used in the [Boole ](https://github.com/avigad/boole ) and
[Coq ](http://coq.inria.fr/ ) systems. In contrast to Coq, Lean is
classical.
In Lean, we have the following kind of expressions: _constants_ ,
,_function applications_, _(heterogeneous) equality_ , _local variables_ ,
_lambdas_, _dependent function spaces_ (aka _Pis_ ), _let expressions_ ,
and _Types_ .
## Constants
Constants are essentially references to variable declarations, definitions, axioms and theorems in the
2014-01-05 21:16:47 +00:00
environment. In the following example, we use the command `variables` to declare `x` and `y` as integers.
The `check` command displays the type of the given expression. The `x` and `y` in the `check` command
are constants. They reference the objects declared using the command `variables` .
2013-12-27 04:21:57 +00:00
```lean
2014-01-05 21:16:47 +00:00
variables x y : Nat
check x + y
2013-12-27 04:21:57 +00:00
```
2014-01-05 21:16:47 +00:00
In the following example, we define the constant `s` as the sum of `x` and `y` using the `definition` command.
The `eval` command evaluates (normalizes) the expression `s + 1` . In this example, `eval` will just expand
2013-12-27 04:21:57 +00:00
the definition of `s` , and return `x + y + 1` .
```lean
2014-01-05 21:16:47 +00:00
definition s := x + y
eval s + 1
2013-12-27 04:21:57 +00:00
```
## Function applications
In Lean, the expression `f t` is a function application, where `f` is a function that is applied to `t` .
2014-01-05 21:16:47 +00:00
In the following example, we define the function `max` . The `eval` command evaluates the application `max 1 2` ,
2013-12-27 04:21:57 +00:00
and returns the value `2` . Note that, the expression `if (x >= y) x y` is also a function application.
```lean
2014-01-05 21:16:47 +00:00
definition max (x y : Nat) : Nat := if (x >= y) x y
eval max 1 2
2013-12-27 04:21:57 +00:00
```
2014-01-05 21:16:47 +00:00
The expression `max 1` is also a valid expression in Lean, and it has type `Nat -> Nat` .
2013-12-27 04:21:57 +00:00
```lean
2014-01-05 21:16:47 +00:00
check max 1
2013-12-27 04:21:57 +00:00
```
In the following command, we define the function `inc` , and evaluate some expressions using `inc` and `max` .
```lean
2014-01-05 21:16:47 +00:00
definition inc (x : Nat) : Nat := x + 1
eval inc (inc (inc 2))
eval max (inc 2) 2 = 3
2013-12-27 04:21:57 +00:00
```
## Heterogeneous equality
For technical reasons, in Lean, we have heterogenous and homogeneous equality. In a nutshell, heterogenous is mainly used internally, and
homogeneous are used externally when writing programs and specifications in Lean.
Heterogenous equality allows us to compare elements of any type, and homogenous equality is just a definition on top of the heterogenous equality that expects arguments of the same type.
The expression `t == s` is a heterogenous equality that is true iff `t` and `s` have the same interpretation.
In the following example, we evaluate the expressions `1 == 2` and `2 == 2` . The first evaluates to `false` and the second to `true` .
```lean
2014-01-05 21:16:47 +00:00
eval 1 == 2
eval 2 == 2
2013-12-27 04:21:57 +00:00
```
2014-01-09 03:06:28 +00:00
Since we can compare elements of different types, the following
expression is type correct, but Lean normalizer/evaluator will *not*
reduce it.
2013-12-27 04:21:57 +00:00
```lean
2014-01-09 03:06:28 +00:00
eval 2 == true
2013-12-27 04:21:57 +00:00
```
We strongly discourage users from directly using heterogeneous equality. The main problem is that it is very easy to
2014-01-09 03:06:28 +00:00
write nonsensical expressions like the one above. The expression `t = s` is a homogeneous equality.
It expects `t` and `s` to have the same type. Thus, the expression `2 = true` is type incorrect in Lean.
2013-12-27 04:21:57 +00:00
The symbol `=` is just notation. Internally, homogeneous equality is defined as:
```
2014-01-05 21:16:47 +00:00
definition eq {A : (Type U)} (x y : A) : Bool := x == y
infix 50 = : eq
2013-12-27 04:21:57 +00:00
```
The curly braces indicate that the first argument of `eq` is implicit. The symbol `=` is just a syntax sugar for `eq` .
2014-01-09 16:33:52 +00:00
In the following example, we use the `set_option` command to force Lean to display implicit arguments and
2013-12-27 04:21:57 +00:00
disable notation when pretty printing expressions.
```lean
2014-01-09 16:33:52 +00:00
set_option pp::implicit true
set_option pp::notation false
2014-01-05 21:16:47 +00:00
check 1 = 2
-- restore default configuration
2014-01-09 16:33:52 +00:00
set_option pp::implicit false
set_option pp::notation true
2014-01-05 21:16:47 +00:00
check 1 = 2
2013-12-27 04:21:57 +00:00
```