Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
uva
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
uva
Commits
73502961
Commit
73502961
authored
Dec 05, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
funclang series5: Prettified test functions so that the test result is more readable.
parent
e74be202
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
56 deletions
+53
-56
funclang-taddeus/series5/ass12.ml
funclang-taddeus/series5/ass12.ml
+7
-1
funclang-taddeus/series5/test.ml
funclang-taddeus/series5/test.ml
+46
-55
No files found.
funclang-taddeus/series5/ass12.ml
View file @
73502961
...
...
@@ -45,7 +45,13 @@ let rec expr2string =
^
" then "
^
expr2string
(
e1
)
^
" else "
^
expr2string
(
e2
)
|
Fun
(
arg
,
e
)
->
"fun "
^
arg
^
" -> "
^
expr2string
(
e
)
|
FunAp
(
e1
,
e2
)
->
"("
^
expr2string
(
e1
)
^
" "
^
expr2string
(
e2
)
^
")"
|
FunAp
(
e1
,
e2
)
->
(* Add parenthesis around argument when needed *)
let
eval_e2
=
expr2string
(
e2
)
in
let
eval_e2
=
(
if
(
String
.
get
eval_e2
0
)
!=
'
(
'
&&
String
.
contains
eval_e2
'
'
then
"("
^
eval_e2
^
")"
else
eval_e2
)
in
"("
^
expr2string
(
e1
)
^
" "
^
eval_e2
^
")"
|
Let
(
var
,
e1
,
e2
)
->
"let "
^
var
^
" = "
^
expr2string
(
e1
)
^
" in "
^
expr2string
(
e2
)
|
LetRec
(
var
,
e1
,
e2
)
->
"let rec "
^
var
^
" = "
^
expr2string
(
e1
)
...
...
funclang-taddeus/series5/test.ml
View file @
73502961
#
use
"ass13.ml"
;;
(* let a = 1 in b *)
print_endline
(
expr2string
(
Let
(
"a"
,
(
Num
1
)
,
(
Var
"b"
))));;
(* a + 3 *)
print_endline
(
expr2string
(
BinopAp
(
Add
,
Var
"a"
,
Num
3
)));;
(* -a *)
print_endline
(
expr2string
(
MonopAp
(
Neg
,
Var
"a"
)));;
let
show_freevars
e
=
let
free
=
match
freevars
e
with
#
use
"ass13.ml"
let
test
tested_method
e
expect
yield
=
if
yield
=
expect
then
print_endline
"SUCCESS"
else
Printf
.
printf
"FAILURE: Method: %s
Input: %s
Expected: %s
Got: %s
\n
"
tested_method
(
expr2string
e
)
expect
yield
let
test_expr2string
expect
e
=
test
"expr2string"
e
expect
(
expr2string
e
)
let
test_freevars
expect
e
=
let
yield
=
match
freevars
e
with
[]
->
""
|
[
x
]
->
x
|
h
::
t
->
List
.
fold_left
(
fun
a
b
->
a
^
", "
^
b
)
h
t
in
print_endline
(
"
\n
Free variables in:
\n
"
^
(
expr2string
e
)
^
"
\n
are: ["
^
free
^
"]"
)
;;
test
"freevars"
e
expect
yield
let
test_eval
expect
e
=
test
"eval"
e
expect
(
expr2string
(
eval
e
));;
test_expr2string
"let a = 1 in b"
(
Let
(
"a"
,
(
Num
1
)
,
(
Var
"b"
)));;
test_expr2string
"a + 3"
(
BinopAp
(
Add
,
Var
"a"
,
Num
3
));;
test_expr2string
"-a"
(
MonopAp
(
Neg
,
Var
"a"
));;
test_expr2string
"a * 3"
(
BinopAp
(
Mul
,
Var
"a"
,
Num
3
));;
(* Write the expressions of assignment series 1 in our data types *)
let
l
arg
body
=
Fun
(
arg
,
body
);;
let
app
func
arg
=
FunAp
(
func
,
arg
);;
(* Define resursive factorial function for test usage *)
let
fac
=
let
eq
=
BinopAp
(
Eq
,
Var
"n"
,
Num
0
)
in
let
sub
=
BinopAp
(
Sub
,
Var
"n"
,
Num
1
)
in
let
mul
=
BinopAp
(
Mul
,
Var
"n"
,
app
(
Var
"fac"
)
sub
)
in
let
body
=
Cond
(
eq
,
Num
1
,
mul
)
in
LetRec
(
"fac"
,
Fun
(
"n"
,
body
)
,
Var
"fac"
);;
test_expr2string
(
"let rec fac = fun n -> if n = 0 then 1 else "
^
"n * (fac (n - 1)) in fac"
)
fac
;;
(* Write the expressions of assignment series 1 in our data types *)
let
ass1a
=
let
c
=
Var
"c"
in
let
bcc
=
l
"b"
(
l
"c"
c
)
in
...
...
@@ -47,46 +64,20 @@ let ass1b =
let
c
=
Var
"c"
in
(
app
(
app
(
app
(
app
uvw
xyz
)
a
)
b
)
c
);;
(* [a] *)
show_freevars
(
Fun
(
"b"
,
(
BinopAp
(
Add
,
Var
"a"
,
Num
3
))));;
(* [] *)
show_freevars
(
Fun
(
"a"
,
(
BinopAp
(
Add
,
Var
"a"
,
Num
3
))));;
test_freevars
"a"
(
Fun
(
"b"
,
(
BinopAp
(
Add
,
Var
"a"
,
Num
3
))));;
test_freevars
""
(
Fun
(
"a"
,
(
BinopAp
(
Add
,
Var
"a"
,
Num
3
))));;
test_freevars
""
ass1a
;;
test_freevars
"a, b, c"
ass1b
;;
let
l
arg
body
=
Fun
(
arg
,
body
);;
let
app
f
e
=
FunAp
(
f
,
e
);;
(* [] *)
show_freevars
ass1a
;;
(* [a, b, c] *)
show_freevars
ass1b
;;
let
show_eval
e
=
print_endline
(
"
\n
Evaluation of:
\n
"
^
(
expr2string
e
)
^
"
\n
is:
\n
"
^
(
expr2string
(
eval
e
)))
;;
(* -5 *)
show_eval
(
BinopAp
(
Sub
,
Num
4
,
Num
9
));;
test_eval
"-5"
(
BinopAp
(
Sub
,
Num
4
,
Num
9
));;
test_eval
"false"
(
BinopAp
(
Lt
,
Num
6
,
Num
5
));;
test_eval
"true"
(
BinopAp
(
Or
,
Bool
true
,
Bool
false
));;
test_eval
"fun u -> fun w -> fun a -> a"
ass1a
;;
test_eval
"(a (b c))"
ass1b
;;
test_eval
"c + b"
(
Let
(
"a"
,
Var
"c"
,
(
BinopAp
(
Add
,
Var
"a"
,
Var
"b"
))));;
(* false *)
show_eval
(
BinopAp
(
Lt
,
Num
6
,
Num
5
));;
(* true *)
show_eval
(
BinopAp
(
Or
,
Bool
true
,
Bool
false
));;
(* fun u -> fun w -> fun a -> a *)
show_eval
ass1a
;;
(* (a (b c)) *)
show_eval
ass1b
;;
(* fun y' -> fun z -> ((y y') z) *)
let
x
=
Var
"x"
in
let
y
=
Var
"y"
in
let
z
=
Var
"z"
in
show_eval
(
app
(
l
"x"
(
l
"y"
(
l
"z"
(
app
(
app
x
y
)
z
))))
y
);;
(* c + b *)
show_eval
(
Let
(
"a"
,
Var
"c"
,
(
BinopAp
(
Add
,
Var
"a"
,
Var
"b"
))));;
test_eval
"fun y' -> fun z -> ((y y') z)"
(
app
(
l
"x"
(
l
"y"
(
l
"z"
(
app
(
app
x
y
)
z
))))
y
);;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment