jssst-ocaml.mgp

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

Jacques Garrigue

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

Parametric Polymorphism

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

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

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

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

# 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

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

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

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

Copyright c 2006 Zhenjiang Hu, All Right Reserved.

2

Copyright c 2008 Zhenjiang Hu, All Right Reserved.

Clipboard

ML 演習 第 4 回

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

SML#³«È¯ºÇÁ°Àþ

1 # include < stdio.h> 2 # include < string.h> 3 4 int main (){ 5 char str [222]; 6 scanf ("%s", str ); 7 int n= strlen ( str ); 8 for ( int i=n -2; i

(CC Attribution) Lisp 2.1 (Gauche )


ohp07.dvi

25 II :30 16:00 (1),. Do not open this problem booklet until the start of the examination is announced. (2) 3.. Answer the following 3 proble

6-1

monad.gby

ML 演習 第 4 回

haskell.gby

Pascal Pascal Free Pascal CPad for Pascal Microsoft Windows OS Pascal

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

JavaScript 1.! DOM Ajax Shelley Powers,, JavaScript David Flanagan, JavaScript 2

ex01.dvi

slide9.dvi

ex01.dvi

untitled

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

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

1 (2 * 3) 1 2 * 3 Preorder In order Post order 1 * 1 * Breadth-first Depth-first * * 3 Preorder: 1 * 2 3 In order: 1 2 * 3 Post orde

Dive into Algebraic Effects

r3.dvi

untitled


ohp03.dvi

untitled

yacc.dvi

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

fp.gby

II 3 yacc (2) 2005 : Yacc 0 ~nakai/ipp2 1 C main main 1 NULL NULL for 2 (a) Yacc 2 (b) 2 3 y

第12回 モナドパーサ

double 2 std::cin, std::cout 1.2 C fopen() fclose() C++ std::fstream 1-3 #include <fstream> std::fstream fout; int a = 123; fout.open( "data.t

r07.dvi

プログラミングD - Java

r03.dvi

ohp07.dvi

netcdf

Microsoft PowerPoint - ProD0107.ppt

2

2018 年度前期プロジェクト活動 Ritsumeikan Compiler Construction 班活動報告書 佐々木章徳青木雅典西見元希松本幸大

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

超初心者用

1-4 int a; std::cin >> a; std::cout << "a = " << a << std::endl; C++( 1-4 ) stdio.h iostream iostream.h C++ include.h 1-4 scanf() std::cin >>

5 IO Haskell return (>>=) Haskell Haskell Haskell 5.1 Util Util (Util: Tiny Imperative Language) 1 UtilErr, UtilST, UtilCont, Haskell C

CX-Checker CX-Checker (1)XPath (2)DOM (3) 3 XPath CX-Checker. MISRA-C 62%(79/127) SQMlint 76%(13/17) XPath CX-Checker 3. CX-Checker 4., MISRA-C CX- Ch

r08.dvi

JavaScript の使い方

.NETプログラマー早期育成ドリル ~VB編 付録 文法早見表~

Functional Programming

2

コンパイラ演習 第 7 回

4 $ alias elixirc="elixirc --ignore-module-conflict" warning redefining module User (current version loaded from Elixir.User.beam) user.ex1 User alias

AN 100: ISPを使用するためのガイドライン

K227 Java 2


mstrcpy char *mstrcpy(const char *src); mstrcpy malloc (main free ) stdio.h fgets char *fgets(char *s, int size, FILE *stream); s size ( )

double float

syspro-0405.ppt

Microsoft PowerPoint - ml1.ppt


ohp08.dvi

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

Programming D 1/15

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

第10回 モジュール

FS_handbook.indd

kyoto.gby

listings-ext

「プログラミング言語」 SICP 第4章 ~超言語的抽象~ その6

Condition DAQ condition condition 2 3 XML key value

exec.dvi

A/B (2010/10/08) Ver kurino/2010/soft/soft.html A/B

ohp02.dvi

RubyKaigi2009 COBOL


xl 1 program Othello6; 2 {$APPTYPE CONSOLE} 3 uses SysUtils; 4 5 type 6 TMasuNo = 0..99; // 7 TYouso = (Soto,Kara,Kuro,Siro); // 8 TBan = array [TMasu

/* do-while */ #include <stdio.h> #include <math.h> int main(void) double val1, val2, arith_mean, geo_mean; printf( \n ); do printf( ); scanf( %lf, &v

void hash1_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; /* i EMPTY */ void hash1_insert(int *array, int n) if (n < 0 n >=

PL : pl0 ( ) 1 SableCC ( sablecc ) 1.1 sablecc sablecc Étienne Gagnon [1] Java sablecc sablecc ( ) Visitor DepthFirstAdapter 1 (Depth

"CAS を利用した Single Sign On 環境の構築"

r3.dvi

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

Transcription:

Objective Caml Jacques Garrigue Kyoto University garrigue@kurims.kyoto-u.ac.jp Objective Caml? 2 Objective Caml GC() Standard MLHaskell

3 OCaml () OCaml

5 let let x = 1 + 2 ;; val x : int = 3 ;; val-: let pair = (3, "three");; val pair : int * string = (3, "three") intstring 6 let f x y = x+y+1 ;; val f : int -> int -> int = <fun> f 10 18;; - : int = 29 let f = fun x y -> x+y+1 ;; val f : int -> int -> int = <fun> (fun x y -> x+y+1) 10 18;; - : int = 29

7 let rec let rec gcd x y = if x > 0 then gcd (y mod x) x else y ;; val gcd : int -> int -> int : <fun> let gcd x y = let m = ref x and n = ref y in while!m > 0 do let x =!m in m :=!n mod x; n := x done;!n ;; val gcd : int -> int -> int = <fun> 8 if let rec fact = function 0 1 -> 1 x -> x * fact (x - 1) ;; val fact : int -> int let rec up x = if x mod 2 <> 0 then up (x*2) else down (x+1) and down x = if x < 10 then x else up (x/3) ;; val up : int -> int = <fun> val down : int -> int = <fun>

9 let fst (x, y) = x ;; val fst : a * b -> a = <fun> a b a b. a * b -> a fst ("France", 33) ;; - : string = "France" fst (42, true) ;; - : int = 42 fst string * int -> string int * bool -> int 10 let map_array f arr = let len = Array.length arr in let arr = Array.create len (f arr.(0)) in for i = 1 to len-1 do arr.(i) <- f arr.(i) done; arr ;; val map_array : ( a -> b) -> a array -> b array let scalar_prod x v = map_array (fun y -> x*y) v ;; val scalar_prod : int -> int array -> int array

val map_array : ( a -> b) -> a array -> b array map_array 2 1 b array a array a array a b val map_array : ( a -> a) -> a array -> a array val map_array : ( a -> b) -> a array -> c array c 11 ()

() 13 ( ) C union ( ) () ( ) 14 Nil Cons type a list = Nil Cons of a * a list let l = Cons (1, Cons (2, Nil)) val l : int list let rec length = function Nil -> 0 Cons(hd, tl) -> 1 + length tl val length : a list -> int

15 let rec map f = function Nil -> Nil Cons(hd, tl) -> Cons (f hd, map f tl) val map : ( a -> b) -> a list -> b list map (fun x -> x+1) l ;; - : int list = Cons (2, Cons (3, Nil)) 16 type a list = Nil Cons of a * a list Append of a list * a list Append Warning: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: Append (_, _) Append(l1, l2) -> length l1 + length l2

17 ( )

19 ::= ( ) type expr = Num of int Var of string Plus of expr * expr Mult of expr * expr 5 * 2 + 3 (3 + y) * 12 20 let map_expr f e = match e with Num _ Var _ -> e Plus (e1, e2) -> Plus (f e1, f e2) Mult (e1, e2) -> Mult (f e1, f e2) val map_expr : (expr -> expr) -> expr -> expr let rec subst env = function Var x when List.mem_assoc x env -> List.assoc x env e -> map_expr (subst env) e val subst : (string * expr) list -> expr -> expr map

21 map_expreval let rec eval e = match map_expr eval e with Plus (Num x, Num y) -> Num (x + y) Mult (Num x, Num y) -> Num (x * y) e -> e val eval : expr -> expr = <fun> let e = subst ["x", Num 3; "y", Var "x"] (Plus (Var "y", Mult (Var "x", Num 2)));; val e : expr = Plus (Var "x", Mult (Num 3, Num 2)) let e = eval e;; val e : expr = Plus (Var "x", Num 6) 22 Format let rec print_expr?(prio=0) ppf e = let printf fmt = Format.fprintf ppf fmt in match e with Num x -> printf "%d" x Var x -> printf "%s" x Mult (e1, e2) -> printf "@[%a *@ %a@]" (print_expr ~prio:1) e1 (print_expr ~prio:1) e2 Plus (e1, e2) as e -> if prio > 0 then (printf "(%a)") print_expr e else (printf "@[%a +@ %a@]") print_expr e1 print_expr e2 val print_expr :?prio:int -> Format.formatter -> expr -> unit?prio:?(prio=0) (printf <fmt>)

23 Format print_expr Format.std_formatter (big 10);; ((2 + 2) * (5 + 3 + 7) + 3 * (8 + 1) + 5 + 8 + 5) * ((9 + 6 + 7) * (3 * (8 + 5) + 4 + 0 + 0) + (6 + 7) * (6 + 6 + 1) + 5 * (8 + 8) + 0 + 2 + 2) let print_expr ppf = print_expr ppf;; val print_expr : Format.formatter -> expr -> unit #install_printer print_expr ;; e;; - : expr = x + 3 * 2 24 Objective Caml : input_line Str(regexp) PERL scanf stream (camlp4) ocamllex ocamlyacc LALR

scanf 25 let input_point ic = Scanf.sscanf (input_line ic) " %f %f %f" (fun x y z -> (x,y,z));; val input_point : in_channel -> float * float * float = <fun> let input_data ic = let rec loop accu n = if n = 0 then List.rev accu else loop (input_point ic :: accu) (n-1) in Scanf.sscanf (input_line ic) " %d" (loop []) val input_data : in_channel -> (float * float * float) list = <fun> input_data stdin;; 2 1 1.4 5 1.3 2 3 - : (float * float * float) list = [(1., 1.4, 5.); (1.3, 2., 3.)] stream 26 #load"camlp4o.cma";; Camlp4 Parsing version 3.07+beta 2 open Genlex;; let lexer = Genlex.make_lexer ["+";"*";"(";")"] val lexer : char Stream.t -> Genlex.token Stream.t = <fun> stream let s = lexer (Stream.of_string "1 2 3 4");; val s : Genlex.token Stream.t = <abstr> (parser [< x >] -> x) s ;; - : Genlex.token = Int 1 (parser [< Int 1 >] -> "ok") s ;; Exception: Stream.Failure (parser [< Int 1 >] -> "one" [< Int 2 >] -> "two") s ;; - : string = "two"

stream 27 let rec accumulate parse accu = parser [< e = parse accu; s >] -> accumulate parse e s [< >] -> accu val accumulate : ( a -> Genlex.token Stream.t -> a) -> a -> Genlex.token Stream.t -> a let left_assoc parse op wrap = let parse accu = parser [< Kwd k when k = op; s >] -> wrap accu (parse s) in parser [< e1 = parse; e2 = accumulate parse e1 >] -> e2 val left_assoc : (Genlex.token Stream.t -> a) -> string -> ( a -> a -> a) -> Genlex.token Stream.t -> a stream 28 let rec parse_simple = parser [< Int n >] -> Num n [< Ident x >] -> Var x [< Kwd"("; e = parse_expr; Kwd")" >] -> e and parse_mult s = left_assoc parse_simple "*" (fun e1 e2 -> Mult(e1,e2)) s and parse_expr s = left_assoc parse_mult "+" (fun e1 e2 -> Plus(e1,e2)) s val parse_simple : Genlex.token Stream.t -> expr = <fun> val parse_mult : Genlex.token Stream.t -> expr = <fun> val parse_expr : Genlex.token Stream.t -> expr = <fun>

stream 29 let parse_string s = match lexer (Stream.of_string s) with parser [< e = parse_expr; _ = Stream.empty >] -> e val parse_string : string -> expr = <fun> let e = parse_string "5+x*(4+x)";; val e : expr = Plus (Num 5, Mult (Var "x", Plus (Num 4, Var "x"))) eval (subst ["x",3] e);; - : expr = Num 26 30 ( ) stream

32 ( ) let a = Apple val a : [> Apple ] let b = Orange "spain" val b : [> Orange of string ] let l = [a; b] val l : [> Apple Orange of string ] list let show1 = function Apple -> "apple" Orange s -> s ^ " orange" val show1 : [< Apple Orange of string ] -> string

33 as type round = [ Apple Orange of string] function #round as x -> x ;; - : [< round ] -> [> round ] = <fun> let show2 = function #round as x -> show1 x Pear -> "pear" val show2 : [< Apple Pear Orange of string ] -> string List.map show2 [ Pear; Apple; Orange "navel"] ;; - : string list = ["pear"; "apple"; "navel orange"] type num = [ Num of int] let eval_num ( Num n : num) = n val eval_num : num -> int = <fun> type a pexpr = [ num Plus of a * a] let eval_pexpr eval_rec (e : a pexpr) = match e with #num as x -> eval_num x Plus (e1, e2) -> eval_rec e1 + eval_rec e2 val eval_pexpr : ( a -> int) -> a pexpr -> int = <fun> 34

35 let rec eval1 e = eval_pexpr eval1 e val eval1 : ( a pexpr as a) -> int = <fun> eval1 ( Plus ( Num 1, Plus( Num 2, Num 3)));; - : int = 6 type a mexpr = [ num Mult of a * a] let eval_mexpr eval_rec (e : a mexpr) = match e with #num as x -> eval_num x Mult (e1, e2) -> eval_rec e1 * eval_rec e2 val eval_mexpr : ( a -> int) -> a mexpr -> int = <fun> 36 type a expr = [ a pexpr a mexpr] let eval_expr eval_rec (e : a expr) = match e with #pexpr as x -> eval_pexpr eval_rec x #mexpr as x -> eval_mexpr eval_rec x val eval_expr : ( a -> int) -> a expr -> int = <fun> let rec eval e = eval_expr eval e val eval : ( a expr as a) -> int = <fun> eval ( Plus ( Num 3, Mult( Num 5, Num 2)));; - : int = 13

37

signature 39 Caml module type PERSON = sig type t val create : name:string -> age:int -> t val birthday : t -> unit val name : t -> string val age : t -> int end signature t create3 name: 40 signature module Person : PERSON = struct type t = { name: string; mutable age: int } let create ~name ~age = assert (age>=0); {name=name; age=age} let birthday p = p.age <- p.age + 1 let name p = p.name let age p = p.age end t : PERSON agebirthday name ~

41 OCaml class person ~name ~age = object val mutable age = assert (age>=0); age method birthday = age <- age+1 method age = age method name : string = name end ;; class person : name:string -> age:int -> object val mutable age : int method age : int method birthday : unit method name : string end age 42 class person_feb29 ~name ~age : person = object inherit person ~name ~age method birthday = age <- age + 4 end ;; class person_feb29 : name:string -> age:int -> person let family = [new person "kazuo" 24; new person_feb29 "kazuko" 24];; val family : person list = [<obj>; <obj>] List.map (fun x -> x#birthday; x#age) family;; - : int list = [25; 28] person

43 Set.Make OrderedType module type OrderedType = sig type t val compare : t -> t -> int end module IntOrder = struct type t = int let compare x y = x-y end module S = Set.Make(IntOrder) ;; let set = S.add 1 (S.add 2 (S.add 1 S.empty)) ;; val set : S.t = <abstr> Set.elements set ;; - : S.elt list = [1; 2] 44 Objective Caml

Objective Caml 45 INRIA http://caml.inria.fr/ OCaml http://www.ocaml.jp/ (ftp) http://wwwfun.kurims.kyoto-u.ac.jp/soft/