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
d72727c8
Commit
d72727c8
authored
Dec 04, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
funclang series5: Added test file for ass12.
parent
d2387098
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
30 deletions
+61
-30
funclang-taddeus/series5/ass12.ml
funclang-taddeus/series5/ass12.ml
+32
-6
funclang-taddeus/series5/test_ass12.ml
funclang-taddeus/series5/test_ass12.ml
+29
-24
No files found.
funclang-taddeus/series5/ass12.ml
View file @
d72727c8
...
...
@@ -13,6 +13,7 @@ type expr = Num of int (* integer constant *)
|
Let
of
string
*
expr
*
expr
(* variable binding *)
|
LetRec
of
string
*
expr
*
expr
(* recursive variable binding *)
(* Convert a given expression to a string representation *)
let
rec
expr2string
=
let
monop2string
=
function
Neg
->
"-"
...
...
@@ -50,6 +51,7 @@ let rec expr2string =
|
LetRec
(
var
,
e1
,
e2
)
->
"let rec "
^
var
^
" = "
^
expr2string
(
e1
)
^
" in "
^
expr2string
(
e2
)
(* Find all free variables within an expression *)
let
freevars
e
=
let
rec
free_vars
bound_vars
=
function
Num
_
|
Bool
_
->
[]
...
...
@@ -69,9 +71,33 @@ let freevars e =
in
free_vars
[]
e
(*let rec subs e var replacement =
let free = freevars e in
match e with
| Num _ | Bool _ -> e
| Var v when List.mem v bound_vars -> []
| Var v -> [v]*)
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
|
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
,
e
)
->
let
free_a
=
freevars
a
in
if
List
.
mem
x
free_a
then
(* Replace x with a new variable w to avoid free variable
* corruption in a *)
(*let all_free_vars = free_a @ (freevars 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
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
)
funclang-taddeus/series5/test_ass12.ml
View file @
d72727c8
...
...
@@ -20,27 +20,32 @@ let show_freevars e =
;;
(* Write the expressions of assignment series 1 in our data types *)
let
u
=
Var
"u"
in
let
c
=
Var
"c"
in
let
bcc
=
l
"b"
(
l
"c"
c
)
in
let
a
=
Var
"a"
in
let
aaa
=
l
"a"
(
app
a
a
)
in
let
aaa_aaa
=
app
aaa
aaa
in
let
v
=
Var
"v"
in
let
uvw
=
l
"u"
(
app
(
l
"v"
(
l
"w"
v
))
(
l
"a"
a
))
in
let
1
a
=
(
app
(
app
bcc
aaa_aaa
)
uvw
);;
let
v
=
Var
"v"
in
let
w
=
Var
"w"
in
let
uvw
=
l
"u"
(
l
"v"
(
l
"w"
(
app
(
app
u
v
)
w
)))
in
let
x
=
Var
"x"
in
let
y
=
Var
"y"
in
let
z
=
Var
"z"
in
let
xyz
=
l
"x"
(
l
"y"
(
l
"z"
(
app
x
(
app
y
z
))))
in
let
a
=
Var
"a"
in
let
b
=
Var
"b"
in
let
c
=
Var
"c"
in
let
1
b
=
(
app
(
app
(
app
(
app
uvw
xyz
)
a
)
b
)
c
);;
let
l
arg
body
=
Fun
(
arg
,
body
);;
let
app
func
arg
=
FunAp
(
func
,
arg
);;
let
ass1a
=
let
c
=
Var
"c"
in
let
bcc
=
l
"b"
(
l
"c"
c
)
in
let
a
=
Var
"a"
in
let
aaa
=
l
"a"
(
app
a
a
)
in
let
aaa_aaa
=
app
aaa
aaa
in
let
v
=
Var
"v"
in
let
uvw
=
l
"u"
(
app
(
l
"v"
(
l
"w"
v
))
(
l
"a"
a
))
in
(
app
(
app
bcc
aaa_aaa
)
uvw
);;
let
ass1b
=
let
u
=
Var
"u"
in
let
v
=
Var
"v"
in
let
w
=
Var
"w"
in
let
uvw
=
l
"u"
(
l
"v"
(
l
"w"
(
app
(
app
u
v
)
w
)))
in
let
x
=
Var
"x"
in
let
y
=
Var
"y"
in
let
z
=
Var
"z"
in
let
xyz
=
l
"x"
(
l
"y"
(
l
"z"
(
app
x
(
app
y
z
))))
in
let
a
=
Var
"a"
in
let
b
=
Var
"b"
in
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
))));;
...
...
@@ -52,10 +57,10 @@ let l arg body = Fun (arg, body);;
let
app
f
e
=
FunAp
(
f
,
e
);;
(* [] *)
show_freevars
1
a
;;
show_freevars
ass
1a
;;
(* [a, b, c] *)
show_freevars
1
b
;;
show_freevars
ass
1b
;;
(* fun u -> fun w -> fun a -> a *)
(*print_endline (expr2string (subs 1b));;*)
print_endline
(
expr2string
(
subs
ass1b
));;
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