Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Proost
Manage
Activity
Members
Labels
Plan
Issues
33
Issue boards
Milestones
Code
Merge requests
18
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package Registry
Model registry
Operate
Terraform modules
Analyze
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
loutr
Proost
Commits
7495353b
Verified
Commit
7495353b
authored
2 years ago
by
v-lafeychine
Browse files
Options
Downloads
Patches
Plain Diff
feat(term): Early beta-reduction
parent
011ba7b0
No related branches found
No related tags found
1 merge request
!2
Resolve "Term definitions"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
core/src/term.rs
+62
-7
62 additions, 7 deletions
core/src/term.rs
with
62 additions
and
7 deletions
core/src/term.rs
+
62
−
7
View file @
7495353b
use
std
::
fmt
::{
Display
,
Formatter
};
use
std
::
fmt
::{
Display
,
Formatter
};
#[derive(Clone,
Debug,
PartialEq)]
#[derive(Clone,
Debug,
Eq,
PartialEq)]
pub
enum
Term
{
pub
enum
Term
{
Prop
,
Prop
,
Var
(
usize
),
Var
(
usize
),
...
@@ -10,15 +10,70 @@ pub enum Term {
...
@@ -10,15 +10,70 @@ pub enum Term {
Prod
(
Box
<
Term
>
,
Box
<
Term
>
),
Prod
(
Box
<
Term
>
,
Box
<
Term
>
),
}
}
use
Term
::
*
;
impl
Term
{
pub
fn
beta_reduction
(
self
)
->
Term
{
match
self
{
App
(
box
Abs
(
_
,
box
mut
t1
),
box
t2
)
=>
{
t1
.substitute
(
t2
,
1
);
t1
}
Abs
(
x
,
box
t
)
=>
Abs
(
x
,
box
t
.beta_reduction
()),
_
=>
self
,
}
}
fn
shift
(
&
mut
self
,
offset
:
usize
)
->
Term
{
match
self
{
Var
(
x
)
=>
Var
(
*
x
+
offset
),
App
(
box
t1
,
box
t2
)
=>
App
(
box
t1
.shift
(
offset
),
box
t2
.shift
(
offset
)),
Abs
(
x
,
box
t
)
=>
Abs
(
x
.clone
(),
box
t
.shift
(
offset
)),
Prod
(
x
,
box
t
)
=>
Prod
(
x
.clone
(),
box
t
.shift
(
offset
)),
_
=>
self
.clone
(),
}
}
fn
substitute
(
&
mut
self
,
rhs
:
Term
,
depth
:
usize
)
{
match
self
{
Var
(
i
)
if
*
i
==
depth
=>
*
self
=
rhs
.clone
()
.shift
(
depth
-
1
),
Var
(
i
)
if
*
i
!=
depth
=>
*
self
=
Var
(
*
i
-
1
),
App
(
l
,
r
)
=>
{
l
.substitute
(
rhs
.clone
(),
depth
);
r
.substitute
(
rhs
,
depth
);
}
Abs
(
_
,
t
)
|
Prod
(
_
,
t
)
=>
{
t
.substitute
(
rhs
,
depth
+
1
);
}
_
=>
{}
}
}
}
impl
Display
for
Term
{
impl
Display
for
Term
{
fn
fmt
(
&
self
,
f
:
&
mut
Formatter
)
->
std
::
fmt
::
Result
{
fn
fmt
(
&
self
,
f
:
&
mut
Formatter
)
->
std
::
fmt
::
Result
{
match
self
{
match
self
{
Term
::
Prop
=>
write!
(
f
,
"
\u{02119}
"
),
Prop
=>
write!
(
f
,
"
\u{02119}
"
),
Term
::
Var
(
i
)
=>
write!
(
f
,
"{}"
,
i
),
Var
(
i
)
=>
write!
(
f
,
"{}"
,
i
),
Term
::
Type
(
i
)
=>
write!
(
f
,
"
\u{1D54B}
({})"
,
i
),
Type
(
i
)
=>
write!
(
f
,
"
\u{1D54B}
({})"
,
i
),
Term
::
App
(
t1
,
t2
)
=>
write!
(
f
,
"{} {}"
,
t1
,
t2
),
App
(
t1
,
t2
)
=>
write!
(
f
,
"
(
{} {}
)
"
,
t1
,
t2
),
Term
::
Abs
(
t1
,
t2
)
=>
write!
(
f
,
"
\u{003BB}
{}
\u{02192}
{}"
,
t1
,
t2
),
Abs
(
t1
,
t2
)
=>
write!
(
f
,
"
\u{003BB}
{}
\u{02192}
{}"
,
t1
,
t2
),
Term
::
Prod
(
t1
,
t2
)
=>
write!
(
f
,
"
\u{02200}
{}
\u{02192}
{}"
,
t1
,
t2
),
Prod
(
t1
,
t2
)
=>
write!
(
f
,
"
\u{02200}
{}
\u{02192}
{}"
,
t1
,
t2
),
}
}
}
}
}
}
#[test]
fn
subst
()
{
let
term
=
Abs
(
box
Prop
,
box
App
(
box
Abs
(
box
Prop
,
box
App
(
box
Var
(
2
),
box
Var
(
1
))),
box
Var
(
1
),
),
);
let
reduced
=
Abs
(
box
Prop
,
box
App
(
box
Var
(
1
),
box
Var
(
1
)));
assert_eq!
(
term
.beta_reduction
(),
reduced
);
}
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