Skip to content

Commit

Permalink
Add lambda expression for specification
Browse files Browse the repository at this point in the history
  • Loading branch information
nushio3 committed Feb 11, 2016
1 parent 6ece3f3 commit 091c6ca
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 42 deletions.
Binary file modified specification/formura-specification.pdf
Binary file not shown.
7 changes: 6 additions & 1 deletion specification/formura-specification.tex
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,10 @@ \subsection{式}
\formura の式は、
即値 \<literal>
、変数名 \<variable-name>
、二項演算子式 \<binary-operator-expression>
、単項演算子式 \<unary-operator-expression>
、二項演算子式 \<binary-operator-expression>
、if文 \<if-then-else-expression>
、ラムダ式 \<lambda-expression>
、括弧式 \<parenthesis-expression>
、格子アクセス式 \<grid-access-expression>
、要素アクセス式 \<projection-expression>
Expand All @@ -537,6 +538,7 @@ \subsection{式}
<binary-operator-expression> |
<unary-operator-expression> |
<if-then-else-expression> |
<lambda-expression> |
<parenthesis-expression> |
<grid-access-expression> |
<projection-expression> |
Expand All @@ -548,6 +550,8 @@ \subsection{式}

<if-then-else-expression> ::= `if' <expression> `then' <expression> `else' <expression>

<lambda-expression> ::= `fun' <parenthesis-expression> <expression>

<parenthesis-expression> ::= `(' <expression> `)'

<offset-expression> ::= <variable-name> `[' <offset-expression> `,' ... `]'
Expand Down Expand Up @@ -692,6 +696,7 @@ \subsection{\formura 文法の全定義}
\alt <expression> <binary-operator> <expression>
\alt <unary-operator> <expression>
\alt `if' <expression> `then' <expression> `else' <expression>
\alt `fun' `(' <expression> `)' <expression>
\alt `(' <expression> `)'
\alt <variable-name> `[' <expression> `,' ... `]'
\alt <function-name> `(' [ <expression> `,' ... ] `)'
Expand Down
Binary file added specification/formura-users-manual.pdf
Binary file not shown.
51 changes: 10 additions & 41 deletions specification/formura-users-manual.tex
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ \subsection{コード実行の流れ}
\begin{screen}
\begin{verbatim}
simulation_state = init();
for (t = 0; t < T_MAX; ++t) {
fun (t = 0; t < T_MAX; ++t) {
simulation_state = step(simulation_state);
}
\end{verbatim}
Expand Down Expand Up @@ -362,7 +362,7 @@ \subsection{3次元拡散方程式}
float :: Dx, Dt
Dx = 4.2
Dt = 0.1
dens_next = dens + Dx ** 2/Dt * Σ for(i) ∂ i (∂ i dens)
dens_next = dens + Dx ** 2/Dt * Σ fun(i) ∂ i (∂ i dens)
end function
\end{lstlisting}

Expand All @@ -381,49 +381,18 @@ \subsection{3次元拡散方程式}
end function
\end{lstlisting}

さらに、\verb`for(a) expr`の形式を利用して次のように書くこともできます。
ここで、\verb`for(a) expr`は、
さらに、\verb`fun (a) expr`の形式を利用して次のように書くこともできます。
ここで、\verb`fun (a) expr`は、
\verb`a`を引数にとって、
\verb`expr`を返す関数、という意味です。プログラミング言語の用語でいえばラムダ式です。

\begin{lstlisting}[mathescape]
ddx(a) = for (a) (a[i+1/2,j,k] - a[i-1/2,j,k])/2
\end{lstlisting}

\verb`fun(a) expr`のほうがいいでしょうか?

\begin{lstlisting}[mathescape]
ddx(a) = fun (a) (a[i+1/2,j,k] - a[i-1/2,j,k])/2
\end{lstlisting}


さきほどの拡散方程式のコードを、ラムダ式を活用して書き直すと、次のようになります。

\begin{lstlisting}[mathescape]
dimension :: 3
axes :: x, y, z

ddx = for(a) (a[i+1/2,j,k] - a[i-1/2,j,k])/2
ddy = for(a) (a[i,j+1/2,k] - a[i,j-1/2,k])/2
ddz = for(a) (a[i,j,k+1/2] - a[i,j,k-1/2])/2

∂ = (ddx,ddy,ddz)

Σ = for (e) e(0) + e(1) + e(2)

begin function init() returns dens_init
float [] :: dens_init = 0
end function

begin function dens_next = step(dens)
float :: Dx, Dt
Dx = 4.2
Dt = 0.1
dens_next = dens + Dt / Dx**2 * Σ for(i) (∂ i . ∂ i) dens
end function
\end{lstlisting}

ラムダ式が\verb`fun`だと、次のように見えます。
さきほどの拡散方程式のコードを、ラムダ式を活用して書き直すと、次のようになります。

\begin{lstlisting}[mathescape]
dimension :: 3
Expand Down Expand Up @@ -485,12 +454,12 @@ \subsection{2次元反応拡散方程式}
temporal_blocking_interval :: 4
monitor_interval :: 20

ddx = for(a) (a[i+1/2,j] - a[i-1/2,j])
ddy = for(a) (a[i,j+1/2] - a[i,j-1/2])
ddx = fun(a) (a[i+1/2,j] - a[i-1/2,j])
ddy = fun(a) (a[i,j+1/2] - a[i,j-1/2])

∂ = (ddx,ddy)

Σ = for (e) e(0) + e(1)
Σ = fun (e) e(0) + e(1)

begin function init() returns (U,V)
float [] :: U = 0, V = 0
Expand All @@ -500,8 +469,8 @@ \subsection{2次元反応拡散方程式}
float :: k = 0.05, F = 0.015, Du = 2e-5, Dv = 1e-5
float :: dt = 1.0, dx = 1.0

dU_dt = -U * V**2 + F * (1-U) + Du/dx**2 * Σ for(i) (∂ i . ∂ i) U
dV_dt = U * V**2 - (F+k) * V + Dv/dx**2 * Σ for(i) (∂ i . ∂ i) V
dU_dt = -U * V**2 + F * (1-U) + Du/dx**2 * Σ fun(i) (∂ i . ∂ i) U
dV_dt = U * V**2 - (F+k) * V + Dv/dx**2 * Σ fun(i) (∂ i . ∂ i) V

U_next = U + dt * dU_dt
V_next = V + dt * dV_dt
Expand Down

0 comments on commit 091c6ca

Please sign in to comment.