• Nenhum resultado encontrado

6.4 Optimization at the Meta-Level

7.1.2 Expressions

FoCaLiZe is a functional programming language. In FoCaLiZe, functions are rst-class values as in the λ-calculus so we can dene functions returning functions as values and functions taking functions as arguments. Unfortunately, Zenon is a rst-order theorem prover so it cannot reason about such higher-order functions. Due to this mismatch, the rst-order fragment of expressions plays an important role in FoCaLiZe.

In this section, we dene the syntax of this rst-order fragment of expressions, the typing rules for expressions and the operational semantics of the language.

7.1.2.1 First-Order Fragment

First-order terms play an especially important role in FoCaLiZe because they are the expressions allowed to appear in logical formulae. We describe their syntax in Figure 7.2 where x and f range over distinct enumerable sets. Because of the special treatment of unit and bool as basic types, their constructors (), true, and false are reserved keywords.

The usual notations for string and integer literals is also assumed. A function symbol can be applied to a list of terms, in particular they cannot be passed as argument to other

7.1. FOCALIZE COMPUTATIONAL LANGUAGE

Constant c ::= () Inhabitant of unit

true True boolean constant

false False boolean constant

". . . " String literal

n Integer literal

Pattern p ::= c Constant pattern

x Pattern variable

_ Universal pattern

p asx Named pattern

C(p1, . . . , pn) Constructed pattern

Terms t ::= c Constant

x Variable

f(t1, . . . , tn) Application

t1=t2 Equality test

letx := t1 int2 Local term denition letf(x1, . . . , xn) := t1 int2 Local function denition let recf(x1, . . . , xn) := t1 int2 Local recursive denition

C(t1, . . . , tn) Constructed term

(t1, . . . , tn) Tuple

ift1 thent2 elset3 Conditional

matcht with |p1 → t1 . . . |pn → tn Pattern matching Figure 7.2: Syntax of FoCaLiZe rst-order terms

7.1. FOCALIZE COMPUTATIONAL LANGUAGE

functions. Anonymous functions are not part of the fragment. A polymorphic equality is assumed but it is rarely used because a user-dened equality is often used in FoCaLiZe programs. Denitions, recursive or not, can be introduced by the let keyword. Terms can be constructed by applying a constructor to arguments or by wrapping together terms in a tuple. Finally, terms can be inspected by pattern-matching and the usual if then else conditional. A pattern can either be a literal constant, which matches exactly that constant and nothing else, a variable, which is bound to the matched term, a wildcard, which matches any term without binding it to a variable, a named pattern, which when matched binds the variable to the matched term, or a constructor applied to sub-patterns, which matches terms constructed by this constructor applied to terms matching the sub- patterns. Moreover, patterns have to be linear: variables in patterns can occur at most once.

This presentation is not minimal. In particular, the conditional could be derived from pattern matching and the equality could be axiomatized. We include conditional and equality because they are simple to translate to Deduction modulo and Dedukti and will be used in Section 8.1.3 to compile pattern matching. This presentation is not complete either as it lacks two constructs which are seldom used in FoCaLiZe: mutual recursive denitions and records.

7.1.2.2 Full Syntax of Expressions

The syntax of expressions is given in Figure 7.3. Contrary to rst-order terms, function symbols are not distinct from variables, the local denition letx := e1 ine2 is seen as the special case of the parametric denition let x (x1, . . . , xn) := e1 in e2 with n= 0. Moreover, anonymous functions and recursive functions can be introduced respectively by theλandµbinders (µis actually not part of FoCaLiZe concrete syntax but it is convenient to dene the semantics of recursive denitions). Heads of application are no longer limited to function symbols but constructors still need to be applied to arguments.

7.1. FOCALIZE COMPUTATIONAL LANGUAGE

Expressione::= c Constant

x Variable

λ(x1, . . . , xn).e Abstraction

µx.e Anonymous recursion

e(e1, . . . , en) Application

e1 =e2 Equality test

letx (x1, . . . , xn) := e1 ine2 Local denition let recx (x1, . . . , xn) := e1 ine2 Local recursive denition C(e1, . . . , en) Constructed expression

(e1, . . . , en) Tuple

ife1 thene2 elsee3 Conditional

matchewith |p1 → e1 . . . |pn → en Pattern matching Figure 7.3: Syntax of FoCaLiZe expressions

Typing Contex Γ ::= ∅ Empty context

Γ, x:σ Variable declaration Figure 7.4: Syntax of FoCaLiZe typing contexts

7.1. FOCALIZE COMPUTATIONAL LANGUAGE

Constants

(Type Unit)

Γ⊢c() :unit (Type True)

Γ⊢ctrue:bool

(Type False)

Γ⊢cfalse:bool (Type String)

Γ⊢c”. . .” :string

(Type Int)

Γ⊢cn:int Patterns

Γ⊢cc:τ

(PType Constant)

Γ⊢pc:τ,∅

(PType Var)

Γ⊢p x:τ,(x:τ)

(PType Wildcard)

Γ⊢p _:τ,∅ Γ⊢p p:τ,Γ

(PType Named)

Γ⊢p pas x:τ,(Γ, x:τ) (C: Πα1. . . αk.(τ1, . . . , τn)→τ)∈Γ Γ⊢p piiρ,Γi (i= 1. . . n)

(PType Constr)

Γ⊢pC(p1, . . . , pn) :τ ρ,(Γ1, . . . ,Γn)

Figure 7.5: Typing rules for FoCaLiZe constants and patterns

7.1. FOCALIZE COMPUTATIONAL LANGUAGE

Γ⊢cc:τ

(Type Constant)

Γ⊢c:τ

(x: Πα1. . . .Παk. τ)∈Γ

(Type Var)

Γ⊢x:τ{α11, . . . , αnn} Γ, x11, . . . , xnn⊢e:τ

(Type Abs)

Γ⊢λ(x1, . . . , xn).e: (τ1, . . . , τn)→τ

Γ, x:τ ⊢e:τ

(Type Rec)

Γ⊢µx.e:τ Γ⊢e: (τ1, . . . , τn)→τ Γ⊢eii (i= 1. . . n)

(Type App)

Γ⊢e(e1, . . . , en) :τ

1, . . . , αk}= (FV(τ1)∪. . .∪FV(τn)∪FV(τ))\FV(Γ)

Γ, x11, . . . , xnn⊢e:τ Γ, x: Πα1. . . αk.(τ1, . . . , τn)→τ ⊢e

(Type Let)

Γ⊢letx (x1, . . . , xn) := eine Γ, x: (τ1, . . . , τn)→τ,

x11, . . . , xnn⊢e:τ

1, . . . , αk}= (FV(τ1)∪. . .∪FV(τn)∪FV(τ))\FV(Γ) Γ, x: Πα1. . . .Παk.(τ1, . . . , τn)→τ ⊢e

(Type LetRec)

Γ⊢let rec x (x1, . . . , xn) := eine

(C: Πα1. . . .Παk.(τ1, . . . , τn)→τ)∈Γ Γ⊢eiiρ (i= 1. . . n)

(Type Constr)

Γ⊢C(e1, . . . , en) :τ ρ Γ⊢e1:τ Γ⊢e2

(Type Eq)

Γ⊢e1 =e2:bool

Γ⊢e1 :bool Γ⊢e2:τ Γ⊢e3

(Type If)

Γ⊢if e1 thene2 elsee3:τ Γ⊢eii (i= 1. . . n)

(Type Tuple)

Γ⊢(e1, . . . , en) :τ1×. . .×τn

Γ⊢e:τ1 Γ⊢p pi1i (i= 1. . . n) Γ,Γi⊢ei2 (i= 1. . . n)

(Type Match)

Γ⊢matchewith |p1 → e1. . .|pn → en2

Figure 7.6: Typing rules for FoCaLiZe expressions

7.1. FOCALIZE COMPUTATIONAL LANGUAGE

Value v ::= c

λ(x1, . . . , xn).e µx.e

C(v1, . . . , vn) (v1, . . . , vn) Evaluation Context E ::= □

E(v1, . . . , vn)

v(v1, . . . , vi−1, E, ei+1, . . . , en) v=E

E =e

let x := E ine

C(v1, . . . , vi−1, E, ei+1, . . . , en) (v1, . . . , vi−1, E, ei+1, . . . , en) ifE theneelsee

match E with |p1 → e1. . .|pn → en Figure 7.7: Syntax of FoCaLiZe values and evaluation contexts

7.1.2.3 Typing

The typing rules for FoCaLiZe computational language are given in Figure 7.5 and Figure 7.6. They are dened with three inductive judgments:

ˆ Γ⊢cc:τ means that the constant c has typeτ in context Γ.

ˆ Γ⊢p p:τ,Γ means that the pattern p has type τ in contextΓ and binds variables according toΓ.

ˆ Γ⊢e:τ means that the expressionehas type τ in context Γ.

The typing context Γ associates type schemes to variables and constructors; its syntax is given in Figure 7.4. In these typing rules,ρ denotes substitutions.

7.1.2.4 Semantics

We give an operational call-by-value semantics to FoCaLiZe computational language.

The syntax for values and evaluation contexts is given in Figure 7.7. Values form a subset

7.1. FOCALIZE COMPUTATIONAL LANGUAGE

e↝e (Semantics Context)

E[e]↝E[e]

(Semantics Beta)

(λ(x1, . . . , xn).e)(v1, . . . , vn)↝e{x1\v1, . . . , xn\vn}

(Semantics Let)

letx := v ine↝e{x\v}

(Semantics LetFun)

letx (x1, . . . , xn) := e1 ine2 ↝letx := λ(x1, . . . , xn).e1 ine2 (Semantics LetRec)

let rec x (x1, . . . , xn) := e1 ine2 ↝ letx := µx.λ(x1, . . . , xn).e1 ine2

(Semantics Mu)

(µx.e)(v1, . . . , vn)↝(e{x\µx.e})(v1, . . . , vn)

(Semantics If True)

if true then e2 elsee3↝e2

(Semantics If False)

if false then e2 elsee3↝e3

ρ=mgu(v, p1)

(Semantics Match First)

match v with |p1 → e1. . .|pn → en↝e1ρ v does not unify with p1

(Semantics Match Next)

match v with|p1 → e1. . .|pn → en↝ matchv with |p2 → e2. . .|pn → en

(Semantics Match Error)

match v with ∅↝ERROR

(Semantics Eq ConstTrue)

c=c↝true c1 ̸≡c2

(Semantics Eq ConstFalse)

c1=c2 ↝false

Figure 7.8: Operational semantics for FoCaLiZe expressions