Skip to content

Commit

Permalink
refactor(ocaml): refactoring ocaml-diff (#25)
Browse files Browse the repository at this point in the history
* refactor(ocaml): refactoring ocaml-diff

* Update src.ml

* Update src.ml

* Update src.ml

* fix: build command

* fix: syntax error

---------

Co-authored-by: sdw0316 <[email protected]>
  • Loading branch information
henrylee97 and sdw0316 authored Nov 9, 2023
1 parent fec2f34 commit b8d4ef8
Show file tree
Hide file tree
Showing 600 changed files with 14,560 additions and 0 deletions.
2 changes: 2 additions & 0 deletions OCaml/diff-1/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.dockerignore
Dockerfile
6 changes: 6 additions & 0 deletions OCaml/diff-1/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM ghcr.io/sambyeol/ocaml-devcontainer:4.14.1-debian-root

USER root
RUN mkdir -p /workspace
WORKDIR /workspace
COPY . /workspace
24 changes: 24 additions & 0 deletions OCaml/diff-1/buggy/src.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
exception EMPTYLIST

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

let rec diff ((aexp : aexp), (str : string)) : aexp =
match aexp with
| Const a -> Const 0
| Var b -> if str = b then Const 1 else Const 0
| Power (pstr, i) ->
if str = pstr then Times [ Const i; Power (pstr, i - 1) ] else Const 0
| Times alst -> (
match alst with
| [] -> Const 0
| [ h ] -> diff (h, str)
| h :: t ->
Sum [ Times (diff (h, str) :: t); Times [ h; diff (Times t, str) ] ] )
| Sum [] -> Const 0
| Sum (h :: []) -> Const 0
| Sum (h :: t) -> Sum [ diff (h, str); diff (Sum t, str) ]
61 changes: 61 additions & 0 deletions OCaml/diff-1/buggy/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
open Src

type env = (string * int) list

(* Input checker *)
let rec gather_vars : aexp -> string list -> string list
= fun ae vars ->
match ae with
| Const n -> vars
| Var x | Power (x, _) -> x::vars
| Times l | Sum l -> List.fold_left (fun vars ae -> gather_vars ae vars) vars l

let rec gather_vars2 : env -> string list -> string list
= fun env vars ->
match env with
| [] -> vars
| (x, v)::tl -> gather_vars2 tl (x::vars)

let rec all_bound : string list -> string list -> bool
= fun vars1 vars2 ->
match vars1 with
| [] -> true
| hd::tl -> if (List.mem hd vars2) then all_bound tl vars2 else false

let input_check : aexp -> env -> bool
= fun ae env ->
let (vars1, vars2) = (gather_vars ae [], gather_vars2 env []) in
all_bound vars1 vars2

let rec find_env : env -> string -> int
= fun env x ->
match env with
| [] -> raise (Failure (x ^ "Not Found"))
| (y, v)::tl -> if (y = x) then v else find_env tl x

let rec ae_eval : aexp -> (string * int) list -> int
= fun e env ->
match e with
| Const n -> n
| Var x -> find_env env x
| Power (x, n) ->
if (n <= 0) then 1 else (find_env env x) * (ae_eval (Power (x, n-1)) env)
| Times l ->
begin
match l with
| [] -> 1
| [hd] -> ae_eval hd env
| hd::tl -> (ae_eval hd env) * (ae_eval (Times tl) env)
end
| Sum l ->
begin
match l with
| [] -> 0
| [hd] -> ae_eval hd env
| hd::tl -> (ae_eval hd env) + (ae_eval (Sum tl) env)
end

let grading : (aexp * string) -> env -> int
= fun (ae, str) env ->
let diff_result = diff (ae, str) in
if input_check ae env then ae_eval diff_result env else raise (Failure "Invalid")
31 changes: 31 additions & 0 deletions OCaml/diff-1/buggy/testcases
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
(Const 1,"x") ; [("x",1)] => 0;
(Var "y","x") ; [("y",1)] => 0;
(Power ("x", 1), "y") ; [("x", 1)] => 0;
(Power ("x", 1), "x") ; [("x", 2)] => 1;
(Power ("x", 0), "x") ; [("x", 1)] => 0;
(Power ("x", 1), "x") ; [("x", 1)] => 1;
(Power ("x", 2),"y") ; [("x",5)] => 0;
(Power ("y", 5), "y") ; [("y", 2)] => 80;
(Times [Var "x"], "x") ; [("x", 3)] => 1;
(Times [Var "x"; Var "x"], "x") ; [("x", 0)] => 0;
(Times [Var "x"; Var "x"], "x") ; [("x", 1)] => 2;
(Times [Const 1], "x") ; [("x", 1)] => 0;
(Times ([Const 1; Var "x"]), "x") ; [("x", 1)] => 1;
(Sum [Const 0; Var "x"],"x") ; [("x",10)] => 1;
(Sum [Power ("x", 2); Times [Const 2; Var "x"]; Const 1], "x") ; [("x", 2)] => 6;
(Sum [Power ("x", 2); Power ("x", 2); Const 1], "x") ; [("x",3)] => 12;
(Sum [Power ("x", 2); Power ("x", 2); Const 1], "y") ; [("x",1)] => 0;
(Sum [Const 2; Power ("x",2); Power ("x",3)], "x") ; ["x",2] => 16;
(Times ([Const (-1); Const (-1); Var "x"]), "x") ; [("x", 1)] => 1;
(Times [Power ("x", 3); Power ("y", 2)], "x") ; [("x", 10); ("y", 5)] => 7500;
(Sum [Times [Sum [Var "x"; Var "y"]; Times [Var "x"; Var "y"]]; Power ("x", 2)], "x") ; [("x", 3); ("y", 4)] => 46;
(Times [Times [Sum [Var "x"; Var "y"]; Var "x"]; Var "x"], "x") ; [("x", 2); ("y", 5)] => 32;
(Times [Const 2; Sum [Var "x"; Var "y"]; Power ("x", 3)], "x") ; [("x", 2); ("y", 1)] => 88;
(Times [Sum [Var "x"; Var "y"; Var "z"]; Power ("x", 2); Sum[Times [Const 3; Var "x"]; Var "z"]], "x") ; [("x", 2); ("y", 1); ("z", 1)] => 188;
(Times [Sum [Var "x"; Var "y"; Var "z"]; Power ("x", 2); Sum[Times [Const 3; Var "x"]; Var "z"]], "y") ; [("x", 1); ("y", 1); ("z", 1)] => 4;
(Sum ([Var ("x"); Var ("x")]), "x") ;[("x", 1)] => 2;
(Sum ([Const (1)]), "x") ;[("x", 1)] => 0;
(Times ([Var ("x")]), "x") ;[("x", 1)] => 1;
}

12 changes: 12 additions & 0 deletions OCaml/diff-1/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"language": "ocaml",
"id": "diff-1",

"buggyPath": "buggy",
"referencePath": null,

"buildCommand": "ocamlc src.ml test.ml",
"testCommand": null,

"categories": ["functional"]
}
2 changes: 2 additions & 0 deletions OCaml/diff-10/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.dockerignore
Dockerfile
6 changes: 6 additions & 0 deletions OCaml/diff-10/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM ghcr.io/sambyeol/ocaml-devcontainer:4.14.1-debian-root

USER root
RUN mkdir -p /workspace
WORKDIR /workspace
COPY . /workspace
32 changes: 32 additions & 0 deletions OCaml/diff-10/buggy/src.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
exception Error of string

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

let rec diff ((aexp : aexp), (str : string)) : aexp =
let rec diff_times (orihd : aexp) (li : aexp list) (s : string) (n : int) :
aexp =
match li with
| hd :: tl ->
if hd = orihd && n = 1 then Const 0
else
Sum
(List.append
[ Times (diff (hd, s) :: tl) ]
[ diff_times orihd (List.append tl [ hd ]) s 1 ])
in

match aexp with
| Const _ -> Const 0
| Var s -> if s = str then Const 1 else Const 0
| Power (s, i) ->
if s = str then Times [ Const i; Power (s, i - 1) ] else Const 0
| Times l ->
if l = [] then raise (Error "no times!") else diff_times (List.hd l) l str 0
| Sum (hd :: tl) ->
if tl = [] then diff (hd, str)
else Sum (List.append [ diff (hd, str) ] [ diff (Sum tl, str) ])
61 changes: 61 additions & 0 deletions OCaml/diff-10/buggy/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
open Src

type env = (string * int) list

(* Input checker *)
let rec gather_vars : aexp -> string list -> string list
= fun ae vars ->
match ae with
| Const n -> vars
| Var x | Power (x, _) -> x::vars
| Times l | Sum l -> List.fold_left (fun vars ae -> gather_vars ae vars) vars l

let rec gather_vars2 : env -> string list -> string list
= fun env vars ->
match env with
| [] -> vars
| (x, v)::tl -> gather_vars2 tl (x::vars)

let rec all_bound : string list -> string list -> bool
= fun vars1 vars2 ->
match vars1 with
| [] -> true
| hd::tl -> if (List.mem hd vars2) then all_bound tl vars2 else false

let input_check : aexp -> env -> bool
= fun ae env ->
let (vars1, vars2) = (gather_vars ae [], gather_vars2 env []) in
all_bound vars1 vars2

let rec find_env : env -> string -> int
= fun env x ->
match env with
| [] -> raise (Failure (x ^ "Not Found"))
| (y, v)::tl -> if (y = x) then v else find_env tl x

let rec ae_eval : aexp -> (string * int) list -> int
= fun e env ->
match e with
| Const n -> n
| Var x -> find_env env x
| Power (x, n) ->
if (n <= 0) then 1 else (find_env env x) * (ae_eval (Power (x, n-1)) env)
| Times l ->
begin
match l with
| [] -> 1
| [hd] -> ae_eval hd env
| hd::tl -> (ae_eval hd env) * (ae_eval (Times tl) env)
end
| Sum l ->
begin
match l with
| [] -> 0
| [hd] -> ae_eval hd env
| hd::tl -> (ae_eval hd env) + (ae_eval (Sum tl) env)
end

let grading : (aexp * string) -> env -> int
= fun (ae, str) env ->
let diff_result = diff (ae, str) in
if input_check ae env then ae_eval diff_result env else raise (Failure "Invalid")
31 changes: 31 additions & 0 deletions OCaml/diff-10/buggy/testcases
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
(Const 1,"x") ; [("x",1)] => 0;
(Var "y","x") ; [("y",1)] => 0;
(Power ("x", 1), "y") ; [("x", 1)] => 0;
(Power ("x", 1), "x") ; [("x", 2)] => 1;
(Power ("x", 0), "x") ; [("x", 1)] => 0;
(Power ("x", 1), "x") ; [("x", 1)] => 1;
(Power ("x", 2),"y") ; [("x",5)] => 0;
(Power ("y", 5), "y") ; [("y", 2)] => 80;
(Times [Var "x"], "x") ; [("x", 3)] => 1;
(Times [Var "x"; Var "x"], "x") ; [("x", 0)] => 0;
(Times [Var "x"; Var "x"], "x") ; [("x", 1)] => 2;
(Times [Const 1], "x") ; [("x", 1)] => 0;
(Times ([Const 1; Var "x"]), "x") ; [("x", 1)] => 1;
(Sum [Const 0; Var "x"],"x") ; [("x",10)] => 1;
(Sum [Power ("x", 2); Times [Const 2; Var "x"]; Const 1], "x") ; [("x", 2)] => 6;
(Sum [Power ("x", 2); Power ("x", 2); Const 1], "x") ; [("x",3)] => 12;
(Sum [Power ("x", 2); Power ("x", 2); Const 1], "y") ; [("x",1)] => 0;
(Sum [Const 2; Power ("x",2); Power ("x",3)], "x") ; ["x",2] => 16;
(Times ([Const (-1); Const (-1); Var "x"]), "x") ; [("x", 1)] => 1;
(Times [Power ("x", 3); Power ("y", 2)], "x") ; [("x", 10); ("y", 5)] => 7500;
(Sum [Times [Sum [Var "x"; Var "y"]; Times [Var "x"; Var "y"]]; Power ("x", 2)], "x") ; [("x", 3); ("y", 4)] => 46;
(Times [Times [Sum [Var "x"; Var "y"]; Var "x"]; Var "x"], "x") ; [("x", 2); ("y", 5)] => 32;
(Times [Const 2; Sum [Var "x"; Var "y"]; Power ("x", 3)], "x") ; [("x", 2); ("y", 1)] => 88;
(Times [Sum [Var "x"; Var "y"; Var "z"]; Power ("x", 2); Sum[Times [Const 3; Var "x"]; Var "z"]], "x") ; [("x", 2); ("y", 1); ("z", 1)] => 188;
(Times [Sum [Var "x"; Var "y"; Var "z"]; Power ("x", 2); Sum[Times [Const 3; Var "x"]; Var "z"]], "y") ; [("x", 1); ("y", 1); ("z", 1)] => 4;
(Sum ([Var ("x"); Var ("x")]), "x") ;[("x", 1)] => 2;
(Sum ([Const (1)]), "x") ;[("x", 1)] => 0;
(Times ([Var ("x")]), "x") ;[("x", 1)] => 1;
}

12 changes: 12 additions & 0 deletions OCaml/diff-10/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"language": "ocaml",
"id": "diff-10",

"buggyPath": "buggy",
"referencePath": null,

"buildCommand": "ocamlc src.ml test.ml",
"testCommand": null,

"categories": ["functional"]
}
2 changes: 2 additions & 0 deletions OCaml/diff-100/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.dockerignore
Dockerfile
6 changes: 6 additions & 0 deletions OCaml/diff-100/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM ghcr.io/sambyeol/ocaml-devcontainer:4.14.1-debian-root

USER root
RUN mkdir -p /workspace
WORKDIR /workspace
COPY . /workspace
37 changes: 37 additions & 0 deletions OCaml/diff-100/buggy/src.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
type aexp =
| Const of int
| Var of string
| Power of (string * int)
| Times of aexp list
| Sum of aexp list

let rec diff ((exp : aexp), (var : string)) : aexp =
match exp with
| Const n -> Const 0
| Var k -> if k = var then Const 1 else Var k
| Power (s, n) ->
if s = var then
if n = 0 then Const 0 else Times [ Power (s, n - 1); Const n ]
else Const 0
| Times li ->
let rec timesfun (exp : aexp list) (var : string) : aexp list =
match exp with
| [] -> []
| hd :: tl -> (
match hd with
| Const n -> [ Const n ] @ timesfun tl var
| Var k -> [ diff (hd, var) ] @ timesfun tl var
| Power (s, n) ->
if s = var then
if n = 0 then [ Const 0 ] @ timesfun tl var
else [ Times [ Power (s, n - 1); Const n ] ] @ timesfun tl var
else [ Const 1 ] @ timesfun tl var
| Times li -> [ diff (hd, var) ] @ timesfun tl var
| Sum li -> [ diff (hd, var) ] @ timesfun tl var )
in
Times (timesfun li var)
| Sum li ->
let rec sumfun (exp : aexp list) : aexp list =
match exp with [] -> [] | hd :: tl -> [ diff (hd, var) ] @ sumfun tl
in
Sum (sumfun li)
Loading

0 comments on commit b8d4ef8

Please sign in to comment.