-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ocaml): refactoring ocaml-diff (#25)
* 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
1 parent
fec2f34
commit b8d4ef8
Showing
600 changed files
with
14,560 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.dockerignore | ||
Dockerfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.dockerignore | ||
Dockerfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.dockerignore | ||
Dockerfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.