ML Edinburgh LCF ML Curry-Howard ML ( ) ( ) ( ) ( ) 1

Similar documents
Jacques Garrigue

Parametric Polymorphism

Objective Caml 3.12 Jacques Garrigue ( ) with Alain Frisch (Lexifi), OCaml developper team (INRIA)

Copyright c 2008 Zhenjiang Hu, All Right Reserved.

# let st1 = {name = "Taro Yamada"; id = };; val st1 : student = {name="taro Yamada"; id=123456} { 1 = 1 ;...; n = n } # let string_of_student {n

: gettoken(1) module P = Printf exception End_of_system (* *) let _ISTREAM = ref stdin let ch = ref ( ) let read () = (let c =!ch in ch := inp

「計算と論理」 Software Foundations その4

# let rec sigma (f, n) = # if n = 0 then 0 else f n + sigma (f, n-1);; val sigma : (int -> int) * int -> int = <fun> sigma f n ( : * -> * ) sqsum cbsu

ML λ λ 1 λ 1.1 λ λ λ e (λ ) ::= x ( ) λx.e (λ ) e 1 e 2 ( ) ML λx.e Objective Caml fun x -> e x e let 1

jssst-ocaml.mgp

Functional Programming

fp.gby

「計算と論理」 Software Foundations その3

プログラミング言語 8 字句解析器(lexer)と構文解析器(parser)

koba/class/soft-kiso/ 1 λ if λx.λy.y false 0 λ typed λ-calculus λ λ 1.1 λ λ, syntax τ (types) ::= b τ 1 τ 2 τ 1

Int Int 29 print Int fmt tostring 2 2 [19] ML ML [19] ML Emacs Standard ML M M ::= x c λx.m M M let x = M in M end (M) x c λx.

(CC Attribution) Lisp 2.1 (Gauche )

ML 演習 第 4 回

slide9.dvi

ML 演習 第 4 回


3 3.1 algebraic datatype data k = 1 1,1... 1,n1 2 2,1... 2,n2... m m,1... m,nm 1 m m m,1,..., m,nm m 1, 2,..., k 1 data Foo x y = Alice x [y] B

Copyright c 2006 Zhenjiang Hu, All Right Reserved.

Dive into Algebraic Effects

情報科学概論 第1回資料

monad.gby

Coq/SSReflect/MathComp による定理証明 サンプルページ この本の定価 判型などは, 以下の URL からご覧いただけます. このサンプルページの内容は, 初版 1 刷発行時のものです.

parser.y 3. node.rb 4. CD-ROM

プログラミングD - Java

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value =

連載 構文解析器結合子 山下伸夫 (( 株 ) タイムインターメディア ) 文字列の分解 1 1 Haskell lines Prelude lines :: String -> [String] lines "" = [] lines s = let (l,s'

untitled

org/ghc/ Windows Linux RPM 3.2 GHCi GHC gcc javac ghc GHCi(ghci) GHCi Prelude> GHCi :load file :l file :also file :a file :reload :r :type expr :t exp

haskell.gby

Clipboard

1. A0 A B A0 A : A1,...,A5 B : B1,...,B

exec.dvi

(Basic Theory of Information Processing) Fortran Fortan Fortan Fortan 1

( ) ( ) lex LL(1) LL(1)


コンピュータ概論

Emacs ML let start ::= exp (1) exp ::= (2) fn id exp (3) ::= (4) (5) ::= id (6) const (7) (exp) (8) let val id = exp in

Terminal ocaml $ ocaml Objective Caml version # 1+2;;<ret> # 1+2;; - : int = 3 ;; OCaml <ret> - : int = 3 OC

コンパイラ演習 第 7 回

スライド 1

離散数学 第 1回 論理 (1):命題論理

2

My関数の作成演習問題集

untitled

I 11

r02.dvi

ohp02.dvi

Microsoft PowerPoint - 13Kadai.pptx

平成 19 年度 ( 第 29 回 ) 数学入門公開講座テキスト ( 京都大学数理解析研究所, 平成 19 ~8 年月 72 月日開催 30 日 ) 1 PCF (Programming language for Computable Functions) PCF adequacy adequacy

Boo Boo 2 Boo 2.NET Framework SDK 2 Subversion 2 2 Java 4 NAnt 5 Boo 5 5 Boo if 11 for 11 range 12 break continue 13 pass

ex01.dvi

つくって学ぶプログラミング言語 RubyによるScheme処理系の実装

e ::= c op(e 1,..., e n ) if e 1 then e 2 else e 3 let x = e 1 in e 2 x let rec x y 1... y n = e 1 in e 2 e e 1... e n (e 1,..., e n ) let (x 1,..., x

listings-ext

Microsoft PowerPoint - lectureNote6.ppt

構造化プログラミングと データ抽象

パズルをSugar制約ソルバーで解く

ohp07.dvi

VDM-SL VDM VDM-SL Toolbox VDM++ Toolbox 1 VDM-SL VDM++ Web bool

記号と準備


class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value =

Condition DAQ condition condition 2 3 XML key value

r3.dvi


6-1

ohp03.dvi

プログラミングD - Java

¥×¥í¥°¥é¥ß¥ó¥°±é½¬I Exercise on Programming I [1zh] ` `%%%`#`&12_`__~~~ alse

ex01.dvi

離散数理工学 第 2回 数え上げの基礎:漸化式の立て方

test.gby

Introduction_analysis_and_query_dsl_for_print.key

構造化プログラミングと データ抽象

第12回 モナドパーサ

untitled

() / (front end) (back end) (phase) (pass) 1 2

cpp1.dvi

Ver.1 1/17/2003 2

I: 2 : 3 +

( ) 1.1 Polychoric Correlation Polyserial Correlation Graded Response Model Partial Credit Model Tetrachoric Correlation ( ) 2 x y x y s r 1 x 2

サンプル一覧表.PDF

untitled

SystemC 2.0を用いた簡易CPUバスモデルの設計

PowerPoint Presentation

第9回 型とクラス

r08.dvi

r03.dvi

x, y x 3 y xy 3 x 2 y + xy 2 x 3 + y 3 = x 3 y xy 3 x 2 y + xy 2 x 3 + y 3 = 15 xy (x y) (x + y) xy (x y) (x y) ( x 2 + xy + y 2) = 15 (x y)

損保ジャパンの現状2011

離散数理工学 第 2回 数え上げの基礎:漸化式の立て方

2 1/2 1/4 x 1 x 2 x 1, x 2 9 3x 1 + 2x 2 9 (1.1) 1/3 RDA 1 15 x /4 RDA 1 6 x /6 1 x 1 3 x 2 15 x (1.2) (1.3) (1.4) 1 2 (1.5) x 1

, , B 305, ,

Python Speed Learning

Transcription:

More Logic More Types ML/OCaml GADT Jacques Garrigue ( ) Jacques Le Normand (Google) Didier Rémy (INRIA) @garriguejej ocamlgadt

ML Edinburgh LCF ML Curry-Howard ML ( ) ( ) ( ) ( ) 1

( ) ML type nebou and chikoku ;; let mp (args : (nebou -> chikoku) * nebou) = let (f, x) = args in f x ;; val mp : (nebou -> chikoku) * nebou -> chikoku = <fun> 2

?! let mp args = let (f, x) = args in f x ;; val mp : ( a -> b) * a -> b = <fun> modus ponens P, Q.(P Q) P Q 3

Brouwer-Heyting-Kolmogorov A B = 1, A 2, B ML type ( a, b) sum = Left of a Right of b 4

type myint = [ Int of int] type mybool = [ Bool of bool] type myint_mybool = [myint mybool] type myint_mybool = [ Bool of bool Int of int ] 5

< x : int; y : int; color : string > p. p#x : int p#y : int p#color : string 6

Girard System F Cardelli F DiCosmo 7

GADT ( ) Coq type _ expr = Int : int -> int expr Add : (int -> int -> int) expr App : ( a -> b) expr * a expr -> b expr App (Add, Int 3) : (int -> int) expr 8

OCaml 3.12 GADT let rec eval : type t. t expr -> t = function Int n -> n (* t = int *) Add -> (+) (* t = int -> int -> int *) App (f, x) -> eval f (eval x) (* *) val eval : a expr -> a = <fun> eval (App (App (Add, Int 3), Int 4));; - : int = 7 9

let rec f : type t 1... t n.τ = body let rec f : α 1... α n.[α 1...α n /t 1...t n ]τ = fun (type t 1 )... (type t n ) (body : τ) body 10

let rec neg : a. a -> a = function (n : int) -> -n (b : bool) -> not b (a, b) -> (neg a, neg b) x -> x val neg : a -> a ( OK) GADT 11

type _ data = Int : int -> int data Bool : bool -> bool data Pair : a data * b data -> ( a * b) data let rec neg : type a. a data -> a data = function Int n -> Int (-n) Bool b -> Bool (not b) Pair (a, b) -> Pair (neg a, neg b) 12

type _ ty = Tint : int ty Tbool : bool ty Tpair : a ty * b ty -> ( a * b) ty let rec print : type a. a ty -> a -> string = fun t d -> match t, d with Tint, n -> string_of_int n Tbool, b -> if b then "true" else "false" Tpair (ta, tb), (a, b) -> Printf.sprintf "(%s, %s)" (print ta a) (print tb b) 13

GADT type zero (* *) type _ succ type _ nat = (* *) NZ : zero nat (* *) NS : a nat -> a succ nat type (_,_) equal = Eq : ( a, a) equal (* *) let convert : type a b. (a,b) equal -> a -> b = fun Eq x -> x let rec samenat : type a b. a nat -> b nat -> (a,b) equal option = fun a b -> match a, b with (* *) NZ, NZ -> Some Eq (* a = b = zero *) NS a, NS b -> (* a = a succ, b = b succ *) begin match samenat a b with Some Eq -> Some Eq (* a = b => a = b *) None -> None end _ -> None (* *) 14

GADT OCaml GADT type (_,_,_) plus = PlusZ : a nat -> (zero, a, a) plus PlusS : ( a, b, c) plus -> ( a succ, b, c succ) plus let rec plus_func : type a b m n. (a,b,m) plus -> (a,b,n) plus -> (m,n) equal = fun p1 p2 -> match p1, p2 with PlusZ _, PlusZ _ -> Eq (* a = zero => m = n = b *) PlusS p1, PlusS p2 -> (* a = a succ, m = m succ,... *) let Eq = plus_func p1 p2 in Eq (* m = n => m = n *) 15

GADT let rec plus_assoc : (* *) type a b c ab bc m n. (a,b,ab) plus -> (ab,c,m) plus -> (b,c,bc) plus -> (a,bc,n) plus -> (m,n) equal = fun p1 p2 p3 p4 -> match p1, p4 with PlusZ b, PlusZ bc -> (* a = zero => bc = n *) let Eq = plus_func p2 p3 in Eq (* m = bc => m = n *) PlusS p1, PlusS p4 -> (* a = a succ, n = n succ,... *) let PlusS p2 = p2 in (* m = m succ *) let Eq = plus_assoc p1 p2 p3 p4 in Eq 16

GADT GADT type (_,_) rel = R1 : (int, bool) rel R2 : ( a, b) rel -> ( b, a) rel let rec any = R2 (R2 any) ;; val any : ( a, b) rel = R2... 17

GADT (Tim Sheard ) ( ) Menhir GADT DSL: GUI, 18

OCaml GADT 1 let rec equal : type a. a data -> a data -> bool = fun a b -> match a, b with Int m, Int n -> m - n = 0 Bool b, Bool c -> if b then c else not c Pair(a1,a2), Pair(b1,b2) -> equal a1 b1 && equal a2 b2 a b 19

type (_,_) eq = Eq : ( a, a) eq module M : sig type t and u val eq : (t,u) eq end = struct type t = int and u = int let eq = Eq end match M.eq with Eq -> " t = u!" 20

2 ( ) ( ) (Pervasives) int bool! 21

OCaml GADT 2 GADT type _ t = Int : int t let f (type a) (x : a t) = match x with Int -> 1 (* a = int *) f int a f? 22

let f (type a) (x : a t) = match x with Int -> true (* a = int *) let g (type a) (x : a t) (y : a) = match x with Int -> (y > 0) f g f f g y 23

let g (type a) (x : a t) (y : a) = match x with Int -> if y > 0 then y else 0 Error: This instance of int is ambiguous 24

Haskell OCaml Haskell GADT 4.00 2005 OutsideIn 25

OCaml 4.00 GADT ( ocamlgadt ) 26