Skip to content

Commit

Permalink
Update 2015 courses
Browse files Browse the repository at this point in the history
  • Loading branch information
henrylee97 committed Oct 12, 2023
1 parent 415940e commit 7ce346a
Show file tree
Hide file tree
Showing 70 changed files with 491 additions and 3 deletions.
6 changes: 3 additions & 3 deletions content/courses/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ weight = 60

## 2015 Fall

- COSE 212: Programming Languages
- COSE 312: Compilers
- [COSE 212: Programming Languages]({{< relref "cose212/2015/_index.md" >}})
- [COSE 312: Compilers]({{< relref "cose312/2015/_index.md" >}})

## 2015 Spring

- COSE 215: Theory of Computation
- [COSE 215: Theory of Computation]({{< relref "cose215/2015/_index.md" >}})
- CRE 501: Computational Logic
46 changes: 46 additions & 0 deletions content/courses/cose212/2015/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
+++
draft = false
title = 'COSE212-15F'
+++

# Programming Languages, 2015 Fall

## Course Information

- Instructor: [Hakjoo Oh]({{< ref "/members/hakjoo-oh.md" >}})
- Lecture: 10:30-11:45 on Tuesdays and Thursdays
- Reference:
{{< figure src="./eopl.jpg" width="100px" link="http://www.amazon.com/gp/product/0262062798?ie=UTF8&tag=ucmbread-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0262062798" target="_blank" >}}

## Slides

- Course Overview: [lec0.pdf](./slides/lec0.pdf)
- Inductive Definitions: [lec1.pdf](./slides/lec1.pdf), [lec2.pdf](./slides/lec2.pdf)
- Basics of OCaml: [lec3.pdf](./slides/lec3.pdf)
- Recursive and Higher-Order Programming: [lec4.pdf](./slides/lec4.pdf)
- Expressions [lec5.pdf](./slides/lec5.pdf) [let.ml](./code/let.ml)
- Procedures [lec6.pdf](./slides/lec6.pdf)
- Scoping and Binding [lec7.pdf](./slides/lec7.pdf)
- States [lec8.pdf](./slides/lec8.pdf)
- Type System [lec9.pdf](./slides/lec9.pdf) [lec10.pdf](./slides/lec10.pdf) [lec11.pdf](./slides/lec11.pdf) [lec12.pdf](./slides/lec12.pdf)
- Lambda Calculus [lec13.pdf](./slides/lec13.pdf) [lec14.pdf](./slides/lec14.pdf)

## Homework

- Homework 1 (due 9/25 24:00) [hw1.pdf](./hw/hw1.pdf), [hw1.ml](./hw/hw1.ml)
- Homework 2 (due 10/9 24:00) hw2.pdf, [hw2.ml](./hw/hw2.ml)
- Homework 3 (due 10/23 24:00) [hw3.pdf](./hw/hw3.pdf), [hw3.ml](./hw/hw3.ml)
- Homework 4 (due 11/14 24:00) [hw4.pdf](./hw/hw4.pdf), [Project Page (Github)](https://github.com/hakjoooh/ProgrammingLanguagesProject2015.git)
- Homework 5 (due 12/05 24:00) [hw5.pdf](./hw/hw5.pdf), [hw5.ml](./hw/hw5.ml)

## OCaml Reference

- How to install
- [Installing the Programming Environment (for Windows users)](./guide_ocaml_install.pdf)
- VirtualBox Image (Ubuntu system with OCaml installed)
- [Download VirtualBox](https://www.virtualbox.org/)
- Why ML?
- [Who Teaches Functional Programming?](http://www.pl-enthusiast.net/2014/09/02/who-teaches-functional-programming/)
- [Why finantial companies on Wall street often use OCaml?](http://www.infoq.com/presentations/jane-street-caml-ocaml)
- [프로그래밍 교육에서 실습 언어의 선택](http://ropas.snu.ac.kr/~kwang/paper/position/edu.pdf)
- [값 중심의 프로그래밍](http://ropas.snu.ac.kr/~kwang/paper/maso/1.pdf)
95 changes: 95 additions & 0 deletions content/courses/cose212/2015/code/let.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
type program = exp
and exp =
| CONST of int
| VAR of var
| ADD of exp * exp
| SUB of exp * exp
| ISZERO of exp
| IF of exp * exp * exp
| LET of var * exp * exp
and var = string

let pgm1 = CONST 1
let pgm2 = ADD (ADD (CONST 5, CONST 2), SUB (CONST 3, CONST 10)) (* (5+2)+(3-10) *)
let pgm3 = IF (ISZERO (CONST 1), CONST 3, CONST 4) (* if iszero 1 then 3 else 4 *)
let pgm4 = LET ("x", CONST 5, SUB(VAR "x", CONST 3)) (* let x = 5 in x - 3 *)

(*
let x = 7
in let y = 2
in let y = (let x = x - 1 in x - y)
in (x - 8 - y) // -5
*)
let pgm5 =
LET ("x", CONST 7,
LET ("y", CONST 2,
LET ("y", LET ("x", SUB(VAR "x", CONST 1), SUB (VAR "x", VAR "y")),
SUB (SUB (VAR "x", CONST 8), VAR "y"))))

let pgm6 =
LET ("x", CONST 1,
LET ("y", CONST 2,
ADD (VAR "x", VAR "y")))

let pgm7 = ISZERO (ISZERO (CONST 1)) (* type error *)

type value = Int of int | Bool of bool

module type Env = sig
type t
exception Not_found
val empty : t
val extend : var -> value -> t -> t
val lookup : var -> t -> value
end

module Env : Env = struct
type t = var -> value
exception Not_found
let empty = fun x-> prerr_endline x; raise Not_found
let lookup x e = e x
let extend x v e = fun y -> if x = y then v else (lookup y e)
end

module Env2 : Env = struct
type t = (var * value) list
exception Not_found
let empty = []
let rec lookup x e =
match e with
| [] -> raise Not_found
| (y,v)::tl -> if x = y then v else lookup x tl
let extend x v e = (x,v)::e
end

let rec eval_bop : (int -> int -> int) -> exp -> exp -> Env.t -> value
=fun op e1 e2 env ->
let v1 = eval e1 env in
let v2 = eval e2 env in
(match v1,v2 with
| Int n1, Int n2 -> Int (op n1 n2)
| _ -> raise (Failure "Type Error: non-numeric values"))

and eval : exp -> Env.t -> value
=fun exp env ->
match exp with
| CONST n -> Int n
| VAR x -> Env.lookup x env
| ADD (e1,e2) -> eval_bop (+) e1 e2 env
| SUB (e1,e2) -> eval_bop (-) e1 e2 env
| ISZERO e ->
(let v = eval e env in
match v with
| Bool _ -> raise (Failure "Type Error: subexpression of zero? must be Int type")
| Int n -> if n = 0 then Bool true else Bool false)
| IF (e1,e2,e3) ->
(match eval e1 env with
| Bool true -> eval e2 env
| Bool false -> eval e3 env
| _ -> raise (Failure "Type Error: condition must be Bool type"))
| LET (x,e1,e2) ->
let v1 = eval e1 env in
eval e2 (Env.extend x v1 env)

let run : program -> value
=fun pgm -> eval pgm Env.empty
Binary file added content/courses/cose212/2015/eopl.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
36 changes: 36 additions & 0 deletions content/courses/cose212/2015/hw/hw1.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(* Problem 1 *)
let rec pascal : int * int -> int
=fun (x,y) -> 1 (* TODO *)

(* Problem 2 *)
let rec sigma : (int -> int) -> int -> int -> int
=fun f a b -> 1 (* TODO *)

(* Problem 3 *)
let rec max : int list -> int
=fun l -> 1 (* TODO *)

let rec min : int list -> int
=fun l -> 1 (* TODO *)

(* Problem 4 *)
type formula =
True
| False
| Neg of formula
| Or of formula * formula
| And of formula * formula
| Imply of formula * formula
| Equiv of formula * formula

let rec eval : formula -> bool
=fun f -> true (* TODO *)

(* Problem 5 *)
type nat = ZERO | SUCC of nat

let rec natadd : nat -> nat -> nat
=fun n1 n2 -> ZERO(* TODO *)

let rec natmul : nat -> nat -> nat
=fun n1 n2 -> ZERO (* TODO *)
Binary file added content/courses/cose212/2015/hw/hw1.pdf
Binary file not shown.
43 changes: 43 additions & 0 deletions content/courses/cose212/2015/hw/hw2.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(*********************)
(* Problem 1: filter *)
(*********************)
let rec filter pred lst = [] (* TODO *)

(*********************)
(* Problem 2: zipper *)
(*********************)
let rec zipper : int list * int list -> int list
=fun (a,b) -> [] (* TODO *)

(*******************)
(* Problem 3: iter *)
(*******************)

let rec iter : int * (int -> int) -> (int -> int)
=fun (n,f) -> f (* TODO *)

(*********************)
(* Problem 4: Diff *)
(*********************)
type aexp =
| Const of int
| Var of string
| Power of string * int
| Times of aexp list
| Sum of aexp list

let rec diff : aexp * string -> aexp
=fun (aexp,x) -> aexp (* TODO *)

(*************************)
(* Problem 5: Calculator *)
(*************************)
type exp = X
| INT of int
| ADD of exp * exp
| SUB of exp * exp
| MUL of exp * exp
| DIV of exp * exp
| SIGMA of exp * exp * exp
let calculator : exp -> int
=fun e -> 0 (* TODO *)
112 changes: 112 additions & 0 deletions content/courses/cose212/2015/hw/hw3.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
(* 1. You can modify the given function specifications as recursive. *)
(* 2. However, do not modify the function names or types. *)
(* 3. It is free to define any helper functions. *)

(***********************************)
(** Problem 1 **)
(***********************************)

module Problem1 = struct
type mobile = branch * branch
and branch = SimpleBranch of length * weight
| CompoundBranch of length * mobile
and length = int
and weight = int

let balanced : mobile -> bool
=fun (lb,rb) -> false (* TODO *)
end

(***********************************)
(** Problem 2 **)
(***********************************)

module Problem2 = struct
type exp = V of var
| P of var * exp
| C of exp * exp
and var = string

let check : exp -> bool
=fun e -> true (* TODO *)
end

(***********************************)
(** Problem 3 **)
(***********************************)
module Problem3 = struct
type program = exp
and exp =
| CONST of int
| VAR of var
| ADD of exp * exp
| SUB of exp * exp
| ISZERO of exp
| IF of exp * exp * exp
| LET of var * exp * exp
| LETREC of var * var * exp * exp
| PROC of var * exp
| CALL of exp * exp
and var = string

type value = Int of int | Bool of bool
| Procedure of var * exp * env
| RecProcedure of var * var * exp * env
and env = var -> value

let empty_env = fun _ -> raise (Failure "Environment is empty")
let extend_env (x,v) e = fun y -> if x = y then v else (e y)
let apply_env e x = e x

let run : program -> value
=fun pgm -> Int 0 (* TODO *)
end

(***********************************)
(** Problem 4 **)
(***********************************)

module Problem4 = struct
type program = exp
and exp =
| CONST of int
| VAR of var
| ADD of exp * exp
| SUB of exp * exp
| ISZERO of exp
| IF of exp * exp * exp
| LET of var * exp * exp
| PROC of var * exp
| CALL of exp * exp
and var = string

type nl_program = nl_exp
and nl_exp =
| NL_CONST of int
| NL_VAR of int
| NL_ADD of nl_exp * nl_exp
| NL_SUB of nl_exp * nl_exp
| NL_ISZERO of nl_exp
| NL_IF of nl_exp * nl_exp * nl_exp
| NL_LET of nl_exp * nl_exp
| NL_PROC of nl_exp
| NL_CALL of nl_exp * nl_exp

let translate : program -> nl_program
=fun pgm -> NL_CONST 0 (* TODO *)
end

(***********************************)
(** Problem 5 **)
(***********************************)

module Problem5 = struct
open Problem4
type nl_value = NL_Int of int
| NL_Bool of bool
| NL_Procedure of nl_exp * nl_env
and nl_env = nl_value list

let nl_run : nl_program -> nl_value
=fun pgm -> NL_Int 0 (* TODO *)
end
Binary file added content/courses/cose212/2015/hw/hw3.pdf
Binary file not shown.
Binary file added content/courses/cose212/2015/hw/hw4.pdf
Binary file not shown.
Loading

0 comments on commit 7ce346a

Please sign in to comment.