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
e74be202
Commit
e74be202
authored
Dec 05, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
funclang series5: Fixed mathematical evaluations combied with variables.
parent
c6ffbeba
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
23 deletions
+18
-23
funclang-taddeus/series5/ass13.ml
funclang-taddeus/series5/ass13.ml
+15
-23
funclang-taddeus/series5/test.ml
funclang-taddeus/series5/test.ml
+3
-0
No files found.
funclang-taddeus/series5/ass13.ml
View file @
e74be202
...
...
@@ -3,27 +3,14 @@
(* Perform all possible beta-reductions on an expression until the normal form
* has been reached *)
let
rec
eval
exp
=
let
get_int_value
=
function
Num
i
->
i
|
_
->
raise
(
Failure
"Not an integer"
)
let
eval_monop
=
function
(
Neg
,
Num
i
)
->
Num
(
-
i
)
|
(
Not
,
Bool
b
)
->
Bool
(
not
b
)
|
_
->
raise
(
Failure
"Unknown unary operator"
)
in
let
get_bool_value
=
function
Bool
b
->
b
|
_
->
raise
(
Failure
"Not a boolean"
)
in
let
eval_monop
op
a
=
match
op
with
Neg
->
Num
(
-
(
get_int_value
a
))
|
Not
->
Bool
(
not
(
get_bool_value
a
))
in
let
eval_binop
op
x
y
=
match
op
with
And
|
Or
->
let
a
=
get_bool_value
x
in
let
b
=
get_bool_value
y
in
Bool
(
if
op
=
And
then
a
&&
b
else
a
||
b
)
|
_
->
let
a
=
get_int_value
x
in
let
b
=
get_int_value
y
in
(
match
op
with
let
eval_binop
binop
=
match
binop
with
(
op
,
Num
a
,
Num
b
)
->
(
match
op
with
(* Arithmethic or relational operation is possible on numbers *)
Add
->
Num
(
a
+
b
)
|
Sub
->
Num
(
a
-
b
)
|
Mul
->
Num
(
a
*
b
)
...
...
@@ -35,11 +22,16 @@ let rec eval exp =
|
Le
->
Bool
(
a
<=
b
)
|
Gt
->
Bool
(
a
>
b
)
|
Ge
->
Bool
(
a
>=
b
)
|
_
->
raise
(
Failure
"Unknown operator"
))
|
_
->
raise
(
Failure
"Unknown arithmetic/relational operator"
))
|
(
op
,
Bool
a
,
Bool
b
)
->
(
match
op
with
(* Locical operation is possible on booleans *)
And
|
Or
->
Bool
(
if
op
=
And
then
a
&&
b
else
a
||
b
)
|
_
->
raise
(
Failure
"Unknown locical operator"
))
|
(
op
,
e1
,
e2
)
->
BinopAp
(
op
,
eval
e1
,
eval
e2
)
in
match
exp
with
MonopAp
(
op
,
e
)
->
eval_monop
op
(
eval
e
)
|
BinopAp
(
op
,
e1
,
e2
)
->
eval_binop
op
(
eval
e1
)
(
eval
e2
)
MonopAp
(
op
,
e
)
->
eval_monop
(
op
,
eval
e
)
|
BinopAp
(
op
,
e1
,
e2
)
->
eval_binop
(
op
,
e1
,
e2
)
|
Fun
(
x
,
e
)
->
Fun
(
x
,
eval
e
)
|
FunAp
(
Fun
(
x
,
Var
y
)
,
a
)
when
y
=
x
->
eval
a
(* identity function *)
|
FunAp
(
Fun
(
y
,
e
)
,
a
)
->
(* prevent recursion *)
...
...
funclang-taddeus/series5/test.ml
View file @
e74be202
...
...
@@ -87,3 +87,6 @@ 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"
))));;
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