---
title: HackMD presentation Sample1
tags: presentation
slideOptions:
theme: white
slideNumber: 'c/t'
center: false
transition: 'none'
keyboard: true
width: '93%'
height: '100%'
---
<style>
/* basic design */
.reveal h1, .reveal h2, .reveal h3, .reveal h4, .reveal h5, .reveal h6,
.reveal section, .reveal table, .reveal li, .reveal blockquote, .reveal th, .reveal td, .reveal p {
font-family: 'Meiryo UI', 'Source Sans Pro', Helvetica, sans-serif, 'Helvetica Neue', 'Helvetica', 'Arial', 'Hiragino Sans', 'ヒラギノ角ゴシック', YuGothic, 'Yu Gothic';
text-align: left;
line-height: 1.8;
letter-spacing: normal;
text-shadow: none;
word-wrap: break-word;
color: #444;
}
.reveal h1, .reveal h2, .reveal h3, .reveal h4, .reveal h5, .reveal h6 {font-weight: bold;}
.reveal h1, .reveal h2, .reveal h3 {color: #2980b9;}
.reveal th {background: #DDD;}
.reveal section img {background:none; border:none; box-shadow:none; max-width: 95%; max-height: 95%;}
.reveal blockquote {width: 90%; padding: 0.5vw 3.0vw;}
.reveal table {margin: 1.0vw auto;}
.reveal code {line-height: 1.2;}
.reveal p, .reveal li {padding: 0vw; margin: 0vw;}
.reveal .box {margin: -0.5vw 1.5vw 2.0vw -1.5vw; padding: 0.5vw 1.5vw 0.5vw 1.5vw; background: #EEE; border-radius: 1.5vw;}
/* table design */
.reveal table {background: #f5f5f5;}
.reveal th {background: #444; color: #fff;}
.reveal td {position: relative; transition: all 300ms;}
.reveal tbody:hover td { color: transparent; text-shadow: 0 0 3px #aaa;}
.reveal tbody:hover tr:hover td {color: #444; text-shadow: 0 1px 0 #fff;}
/* blockquote design */
.reveal blockquote {
width: 90%;
padding: 0.5vw 0 0.5vw 6.0vw;
font-style: italic;
background: #f5f5f5;
}
.reveal blockquote:before{
position: absolute;
top: 0.1vw;
left: 1vw;
content: "\f10d";
font-family: FontAwesome;
color: #2980b9;
font-size: 3.0vw;
}
/* font size */
.reveal h1 {font-size: 5.0vw;}
.reveal h2 {font-size: 4.0vw;}
.reveal h3 {font-size: 2.8vw;}
.reveal h4 {font-size: 2.6vw;}
.reveal h5 {font-size: 2.4vw;}
.reveal h6 {font-size: 2.2vw;}
.reveal section, .reveal table, .reveal li, .reveal blockquote, .reveal th, .reveal td, .reveal p {font-size: 2.2vw;}
.reveal code {font-size: 1.6vw;}
/* new color */
.red {color: #EE6557;}
.blue {color: #16A6B6;}
/* split slide */
#right {left: -18.33%; text-align: left; float: left; width: 50%; z-index: -10;}
#left {left: 31.25%; text-align: left; float: left; width: 50%; z-index: -10;}
</style>
<style>
/* specific design */
.reveal h1 {
margin: 0% -100%;
padding: 2% 100% 4% 100%;
color: #fff;
background: #fffa5e; /* fallback for old browsers */
background: linear-gradient(-45deg, #f7f439, #54ffa4, #23A6D5, #238ed5);
background-size: 200% 200%;
animation: Gradient 60s ease infinite;
}
@keyframes Gradient {
0% {background-position: 0% 50%}
50% {background-position: 100% 50%}
100% {background-position: 0% 50%}
}
.reveal h2 {
text-align: center;
margin: -5% -50% 2% -50%;
padding: 4% 10% 1% 10%;
color: #fff;
background: #fffa5e; /* fallback for old browsers */
background: -webkit-linear-gradient(to right, #c74646, #fffa5e); /* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to right, #c74646, #fffa5e); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}
</style>
<!-- --------------------------------------------------------------------------------------- -->
## Rpythonでプログラミング言語を実装
発表者:浅野、脇坂
# 到達目標
RPython(PyPyの動的言語フレームワーク)上で,簡易関数型,簡易論理型など小さな言語を実装(RPython で 2-level functional language を実装してRPythonのコンパイル時処理に対する一種のリフレクションをつくれないか?)
https://hackmd.io/s/SyWDZwDNK(pythonをHackMDでまとめながら進める)
参考資料:
https://rpython.readthedocs.io/en/latest/index.html
https://rinatz.github.io/python-book/
http://morepypy.blogspot.com/2011/04/tutorial-writing-interpreter-with-pypy.html
# RPythonとは
RPythonは、動的言語の実装を作成するために設計された言語。代表的なものにPyPyがあり、これはPython言語の高速な代替実装。
RPythonはPythonの構文をベースにしているので、RPythonのほとんどの概念はPythonと似ているが、組み込み型にはいくつかの注目すべき違いがある。まず、RPythonは強い型付けをする。これによって、listやdictなどの組み込み型の使用にはいくつかの制限がある。また,実装の違いによっても,ある程度の制約がある。例えば,RPythonではset型が実装されていないので,dictを使ってset型(要素を扱うための型)をシミュレートする必要がある。
# Hello,World!
```python=
def entry_point(argv):
print "Hello, World!"
return 0
# ターゲット関数は、RPythonプログラムのメイン関数
# コマンドライン引数を入力とし、エントリーポイント関数を返す
def target(*args):
return entry_point
# Pythonでスクリプトを解釈するためのエントリーポイントとなる。
if __name__ == "__main__":
import sys #別のファイルに書いた関数などのプログラムを読み込む
entry_point(sys.argv)
```
```
Hello,World
```
# 組み込み型
Pythonには 、数値、シーケンス、マッピング、ファイル、クラス、インスタンス、例外などのさまざまな組み込み型が用意されている。これらの型がRPythonでどのように動作するかを確認する。
## 真理値テスト
RPythonの真理値テストはPythonとほとんど同様です。
どのようなオブジェクトでも真理値を調べることができ、ifやwhileの条件や、以下のブール演算のオペランドとして使用できる。以下の値は偽とみなされる。
```python=
def truth_value_testing():
if None or \
False or \
0 or \
0L or \
0.0 or \
'' or \
() or \
[] or \
{}:
print("Some values are True.")
else:
print("None, False, 0, 0L, 0.0, '', (), [], {} are considered False.")
def entry_point(argv):
truth_value_testing()
return 0
def target(*args):
return entry_point
if __name__ == "__main__":
import sys
entry_point(sys.argv)
```
```
None, False, 0, 0L, 0.0, '', (), [], {} are considered False.
```
2つのクラスZeroLenとNonZeroがある。2つはそれぞれ__len__()と__nonzero__()メソッドを定義すると
```python=
class ZeroLen():
def __len__(self):
return 0
class NonZero():
def __nonzero__(self):
return False
def truth_value_testing_inconsistency():
zero_len = ZeroLen()
non_zero = NonZero()
if zero_len:
print("zero_len is True in RPython.") # RPython
else:
print("zero_len is False in Python.") # Python
if non_zero:
print("non_zero is True in RPython.") # RPtyhon
else:
print("non_zero is False in Python.") # Python
def entry_point(argv):
truth_value_testing_inconsistency()
return 0
def target(*args):
return entry_point
if __name__ == "__main__":
import sys
entry_point(sys.argv)
```
RPythonとPythonを使用すると実行結果に一貫性がない。
```
zero_len is False in Python.
non_zero is False in Python.
zero_len is False in Python.
non_zero is True in RPython.
```
## ブール演算
RPythonのブール演算はPythonと同様
```python=
def boolean_operations():
t = True
f = False
empty_dict = {}
if t or f: print("True or False is True")
if t and f: pass
else: print("True and False is False")
if not empty_dict: print("not {} is True")
def entry_point(argv):
boolean_operations()
return 0
def target(*args):
return entry_point
```
## 比較
RPythonでの比較操作もPythonと類似しており
< <= > >= == != is が含まれる
クラスの同一でないインスタンスは、__eq__()メソッドを定義しない限り、等しくないと比較されてしまう。
次の例は、整数とインスタンスの比較を示している。
```python=
class Cat:
def __init__(self, name, height):
self.name = name
self.height = height
def __eq__(self, other):
return self.name == other.name and self.height == other.height
def __cmp__(self, other):
return self.height - other.height
def __gt__(self, other):
return self.height > other.height
def comparisons():
if 1 < 2: print("<: 1 is strictly less than 2")
if 1 <= 2: print("<=: 1 is less than or equal to 2")
if 2 > 1: print(">: 2 is strictly greater than 1")
if 2 >= 1: print(">=: 2 is greater than or equal 1")
if 1 == 1: print ("==: 1 is equal to 1")
if 1 != 2: print("!=: 1 is not equal to 2")
tiger = Cat("Tiger", 9.1)
kitty = Cat("Kitty", 9.8)
puss = tiger
if tiger is puss: print("is: Tiger and Puss are same cat")
if tiger is not kitty: print("is not: Tiger and Kitty are different cats")
# Catクラスで__cmp__()と__gt__()が定義されていても、RPythonでは
# RPythonでは2つのCatインスタンスを比較することはできません。以下の行をアンコメントしてください。
# RPythonでコンパイルすると、未実装の操作: 'gt' が発生します。
#エラーになる。
def entry_point(argv):
comparisons()
return 0
def target(*args):
return entry_point
if __name__ == "__main__":
import sys
entry_point(sys.argv)
```
## 数値型
組み込みの数値型
数値には、プレーン整数(整数とも呼ばれる)、長整数(通常の整数型よりも大きな値を格納)、浮動小数点数の3つのタイプがあります。
複素数は、RPythonの組み込み型としてサポートされていません。ただし、RPythonライブラリを使用して複素数を処理できます。
この表では、RPythonの組み込み型の詳細について説明します。低レベル(LL)型、対応するC型、およびメモリ内のサイズ(64ビット)です。
