repos / bachelor-thesis.git


Evgenii Akentev  ·  2019-01-18

Typingrules.lagda

  1\section{Typing rules for behaviour layer}
  2
  3\if{False}
  4\begin{code}
  5-- Imports
  6open import Prelude
  7open import Syntax
  8open import Typesystem
  9\end{code}
 10\fi
 11
 12There were created 28 typing rules for behaviour layer during the development. Some of them are almost similar to the rules from the basement of this work~\cite{nielsen2013type}. I demonstrate the subset of the typing rules.
 13
 14In ~\cite{nielsen2013type}, if a behaviour $b$ is typed with respect to an environment $\Gamma$ and changes it during type checking to an environment $\Gamma_1$, then it is written as $\Gamma \vdash_{B} b \vartriangleright \Gamma_1$ in the form of Hoare triple.
 15
 16\begin{code}
 17data _B__ : Context  Behaviour  Context  Set where
 18\end{code}
 19
 20The following constructors of this type express the typing rules and they are used for type checking behaviours. The first constructor is for the \AgdaInductiveConstructor{nil} behaviour. Since \AgdaInductiveConstructor{nil} does nothing, the context before and the context after are equal.
 21
 22\begin{code}
 23  t-nil : {Γ : Context}
 24         Γ B nil  Γ
 25\end{code}
 26
 27The \AgdaInductiveConstructor{t-if} constructor is intended for \AgdaInductiveConstructor{if\_then\_else} behaviour. It takes three arguments: a type which states that the expression $e$ has type \AgdaInductiveConstructor{bool}, two types which state that the behaviours $b_1$ and $b_2$ are well-typed. The result of this constructor is well typed \AgdaInductiveConstructor{if\_then\_else} behaviour.
 28
 29\begin{code}
 30  t-if : {Γ Γ : Context} {b b : Behaviour} {e : Expr}
 31        Γ  e  bool
 32        Γ B b  Γ
 33        Γ B b  Γ
 34        Γ B if e then b else b  Γ
 35\end{code}
 36
 37To construct a well-typed \AgdaInductiveConstructor{while[\_]\_} behaviour there is the \AgdaInductiveConstructor{t-while} constructor which takes a well-typed with respect to the environment expression $e$ and a well-typed behaviour $b$. The typing rule does not change the environment after the behaviour.
 38
 39\begin{code}
 40  t-while : {Γ : Context} {b : Behaviour} {e : Expr}
 41           Γ  e  bool
 42           Γ B b  Γ
 43           Γ B while[ e ] b  Γ
 44\end{code}
 45
 46The following constructor is for sequential programs. It takes two well typed behaviours and returns a well-typed \AgdaInductiveConstructor{seq} behaviour. Notice how contexts of typing rules are arranged. $\Gamma \vdash_{B} b_1 \vartriangleright \Gamma_1$ and $\Gamma_1 \vdash_{B} b_2 \vartriangleright \Gamma_2$ are connected into $\Gamma \vdash_{B} b_1 : b_2 \vartriangleright \Gamma_2$.
 47
 48\begin{code}
 49  t-seq : {Γ Γ Γ : Context} {b b : Behaviour}
 50         Γ B b  Γ
 51         Γ B b  Γ
 52         Γ B b  b  Γ
 53\end{code}
 54
 55The \AgdaInductiveConstructor{t-par} constructor is for \AgdaInductiveConstructor{\_\_} (`parallel`) behaviour. The parallel behaviour is typable if each its thread is well typed and the resulting environments are disjoint. In this work, I assume that the last statement is true.
 56
 57\begin{code}
 58  t-par : {Γ Γ Γ' Γ₂' : Context} {b b : Behaviour}
 59         Γ B b  Γ'
 60        → Γ₂ ⊢B b₂ ▹ Γ₂'
 61         (& Γ Γ) B b  b  (& Γ' Γ₂')
 62\end{code}
 63
 64The next four constructors are for assignment behaviour. I start with the \AgdaInductiveConstructor{t-assign-new} typing rule which is intended for a simple context. It takes a fact that an expression $e$ is well-typed with respect to an environment $\Gamma$, a fact that a variable $x$ is not in $\Gamma$ and returns an assignment behaviour where the resulting environment has variable $x$ with the type of expression $e$.
 65
 66\begin{code}
 67  t-assign-new :  {n} {Γ : Ctx n} {x : Variable} {e : Expr} {Tₓ Tₑ : Type}
 68                 Γ  e  Tₑ
 69                ¬ (x  Tₓ   Γ)
 70                 Γ B x  e   (x  Tₑ  Γ)
 71\end{code}
 72
 73This constructor is for the case when the initial context is a disjoint union and the assignment of variable $x$ is done in the left context.
 74
 75\begin{code}
 76  t-assign-new-left :  {n m} {Γ : Ctx n} {Γ : Ctx m}  {x : Variable}
 77                      {e : Expr} {Tₓ Tₑ : Type}
 78                     & ( Γ) ( Γ)  e  Tₑ
 79                     ¬ (x  Tₓ  & ( Γ) ( Γ))
 80                     & ( Γ) ( Γ) B x  e  & ( (x  Tₑ  Γ)) ( Γ)
 81\end{code}
 82
 83The constructor \AgdaInductiveConstructor{t-assign-new-right} is similar to the previous one. It adds variable $x$ in the right context of a disjoint union.
 84
 85\begin{code}
 86  t-assign-new-right :  {n m} {Γ : Ctx n} {Γ : Ctx m}  {x : Variable}
 87                       {e : Expr} {Tₓ Tₑ : Type}
 88                      & ( Γ) ( Γ)  e  Tₑ
 89                      ¬ (x  Tₓ  & ( Γ) ( Γ))
 90                      & ( Γ) ( Γ) B x  e  & ( Γ) ( (x  Tₑ  Γ))
 91\end{code}
 92
 93The next constructor is for an assignment behaviour when the variable is already defined in the initial context. It requires a type of expression to be equal to a type of variable.
 94
 95\begin{code}
 96  t-assign-exists : {Γ : Context} {x : Variable} {e : Expr} {Tₓ Tₑ : Type}
 97                   Γ  e  Tₑ
 98                   x  Tₓ  Γ
 99                   Tₑ  Tₓ
100                   Γ B x  e  Γ
101\end{code}