Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
uva
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
uva
Commits
fab70057
Commit
fab70057
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
funclang series5: Finished nameslash solution and added eval function for ass13.
parent
d72727c8
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
funclang-taddeus/series5/ass12.ml
+15
-21
15 additions, 21 deletions
funclang-taddeus/series5/ass12.ml
funclang-taddeus/series5/ass13.ml
+19
-0
19 additions, 0 deletions
funclang-taddeus/series5/ass13.ml
funclang-taddeus/series5/test.ml
+16
-1
16 additions, 1 deletion
funclang-taddeus/series5/test.ml
with
50 additions
and
22 deletions
funclang-taddeus/series5/ass12.ml
+
15
−
21
View file @
fab70057
...
...
@@ -44,7 +44,7 @@ let rec expr2string =
|
Cond
(
cond
,
e1
,
e2
)
->
"if "
^
expr2string
(
cond
)
^
" then "
^
expr2string
(
e1
)
^
" else "
^
expr2string
(
e2
)
|
Fun
(
arg
,
e
)
->
"fun
c
"
^
arg
^
" -> "
^
expr2string
(
e
)
|
Fun
(
arg
,
e
)
->
"fun "
^
arg
^
" -> "
^
expr2string
(
e
)
|
FunAp
(
e1
,
e2
)
->
"("
^
expr2string
(
e1
)
^
" "
^
expr2string
(
e2
)
^
")"
|
Let
(
var
,
e1
,
e2
)
->
"let "
^
var
^
" = "
^
expr2string
(
e1
)
^
" in "
^
expr2string
(
e2
)
...
...
@@ -71,33 +71,27 @@ let freevars e =
in
free_vars
[]
e
let
cnt
=
ref
0
let
getNewId
id
=
(
cnt
:=
!
cnt
+
1
;
"_"
^
id
^
"_"
^
string_of_int
(
!
cnt
))
(* Substitue all occurences of y by a in exp *)
let
rec
subs
exp
y
a
=
match
exp
with
|
Var
x
when
x
=
y
->
a
|
Num
_
|
Bool
_
|
Var
_
->
exp
let
rec
subs
exp
y
a
=
match
exp
with
Var
x
when
x
=
y
->
a
|
MonopAp
(
op
,
e
)
->
MonopAp
(
op
,
subs
e
y
a
)
|
BinopAp
(
op
,
e1
,
e2
)
->
BinopAp
(
op
,
subs
e1
y
a
,
subs
e2
y
a
)
|
Fun
(
arg
,
_
)
when
arg
=
y
->
exp
|
Fun
(
x
,
_
)
when
x
=
y
->
exp
|
Fun
(
x
,
e
)
->
let
free_a
=
freevars
a
in
if
List
.
mem
x
free_a
the
n
(* Replace x with a new variable w to avoid free variable
*
corruption in a
*)
(*
let all_free_vars = free_a @
(
free
vars e)
in
let rec
copy_
x =
let new_x =
getNewId x
in
if List.mem new_x all_free_vars then
copy_
x else new_x
in
*)
let
w
=
getNewId
x
in
let
free_e
=
freevars
e
i
n
if
List
.
mem
y
free_e
&&
List
.
mem
x
free_a
then
(
*
Replace x with a new variable w to avoid name clash
*)
let
all_free_vars
=
free_a
@
free
_e
in
let
rec
add_prime
x
=
let
new_x
=
x
^
"'"
in
if
List
.
mem
new_x
all_free_vars
then
add_prime
x
else
new_x
in
let
w
=
add_prime
x
in
Fun
(
w
,
subs
(
subs
e
x
(
Var
w
))
y
a
)
else
Fun
(
x
,
subs
e
y
a
)
|
FunAp
(
e1
,
e2
)
->
FunAp
(
subs
e1
y
a
,
subs
e2
y
a
)
|
Cond
(
cond
,
e1
,
e2
)
->
Cond
(
subs
cond
y
a
,
subs
e1
y
a
,
subs
e2
y
a
)
|
Let
(
x
,
e1
,
e2
)
when
x
=
y
->
Let
(
x
,
e1
,
e2
)
|
Let
(
x
,
e1
,
e2
)
->
Let
(
x
,
e1
,
e2
)
|
LetRec
(
x
,
e1
,
e2
)
->
LetRec
(
x
,
e1
,
e2
)
|
Let
(
x
,
e1
,
e2
)
when
x
!=
y
->
Let
(
x
,
subs
e1
y
a
,
subs
e2
y
a
)
|
_
->
exp
This diff is collapsed.
Click to expand it.
funclang-taddeus/series5/ass13.ml
0 → 100644
+
19
−
0
View file @
fab70057
#
use
"ass12.ml"
(* Perform all possible beta-reductions on an expression until the normal form
* has been reached *)
let
rec
eval
exp
=
match
exp
with
MonopAp
(
op
,
e
)
->
MonopAp
(
op
,
eval
e
)
|
BinopAp
(
op
,
e1
,
e2
)
->
BinopAp
(
op
,
eval
e1
,
eval
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 *)
let
result
=
(
subs
e
y
a
)
in
if
result
=
exp
then
result
else
eval
result
|
FunAp
(
e1
,
e2
)
->
(* prevent recursion *)
let
result
=
FunAp
(
eval
e1
,
eval
e2
)
in
if
result
=
exp
then
result
else
eval
result
|
Cond
(
cond
,
e1
,
e2
)
->
Cond
(
eval
cond
,
eval
e1
,
eval
e2
)
|
Let
(
x
,
e1
,
e2
)
->
eval
(
subs
e2
x
e1
)
|
LetRec
(
x
,
e1
,
e2
)
->
LetRec
(
x
,
eval
e1
,
eval
e2
)
|
_
->
exp
This diff is collapsed.
Click to expand it.
funclang-taddeus/series5/test
_ass12
.ml
→
funclang-taddeus/series5/test.ml
+
16
−
1
View file @
fab70057
#
use
"ass12.ml"
;;
#
use
"ass13.ml"
;;
(* let a = 1 in b *)
print_endline
(
expr2string
(
Let
(
"a"
,
(
Num
1
)
,
(
Var
"b"
))));;
...
...
@@ -62,5 +63,19 @@ 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
)))
;;
(* fun u -> fun w -> fun a -> a *)
print_endline
(
expr2string
(
subs
ass1b
));;
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
);;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment