-
Notifications
You must be signed in to change notification settings - Fork 0
Lamba interpratator #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MikePuzanov
wants to merge
3
commits into
master
Choose a base branch
from
LambaInterpratator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
LambdaInterpreter/LambdaInterpreter.Test/LambdaInterpreter.Test.fsproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| <GenerateProgramFile>false</GenerateProgramFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="LambdaInterpreterTest1.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="FsUnit" Version="4.2.0" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> | ||
| <PackageReference Include="NUnit" Version="3.13.2" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="4.0.0" /> | ||
| <PackageReference Include="coverlet.collector" Version="3.1.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\LambdaInterpreter\LambdaInterpreter.fsproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
29 changes: 29 additions & 0 deletions
29
LambdaInterpreter/LambdaInterpreter.Test/LambdaInterpreterTest1.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| module LambdaInterpreter.Test | ||
|
|
||
| open NUnit.Framework | ||
| open FsUnit | ||
| open Program | ||
|
|
||
| [<Test>] | ||
| let TestAppWithVarTerm () = | ||
| let term = Term.Application(LambdaAbstraction("y", Variable("y")), Variable("z")) | ||
|
|
||
| let result = betaReduction term | ||
|
|
||
| result |> should equal (Variable("z")) | ||
|
|
||
| [<Test>] | ||
| let TestAppWithTwoTerm () = | ||
| let term = Term.Application(LambdaAbstraction("y", Variable("y")), LambdaAbstraction("z", Variable("z"))) | ||
|
|
||
| let result = term |> betaReduction | ||
|
|
||
| result |> should equal (LambdaAbstraction("z", Variable("z"))) | ||
|
|
||
| [<Test>] | ||
| let TestLambdaAbs () = | ||
| let term = Term.LambdaAbstraction("x", LambdaAbstraction("z", Variable("z"))) | ||
|
|
||
| let result = term |> betaReduction | ||
|
|
||
| result |> should equal (LambdaAbstraction("x", LambdaAbstraction("z", Variable("z")))) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "LambdaInterpreter", "LambdaInterpreter\LambdaInterpreter.fsproj", "{81D87A95-7386-4009-9E71-1E58B5966D23}" | ||
| EndProject | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "LambdaInterpreter.Test", "LambdaInterpreter.Test\LambdaInterpreter.Test.fsproj", "{0D2BE233-2834-448C-A50E-4644123FA525}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {81D87A95-7386-4009-9E71-1E58B5966D23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {81D87A95-7386-4009-9E71-1E58B5966D23}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {81D87A95-7386-4009-9E71-1E58B5966D23}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {81D87A95-7386-4009-9E71-1E58B5966D23}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {0D2BE233-2834-448C-A50E-4644123FA525}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {0D2BE233-2834-448C-A50E-4644123FA525}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {0D2BE233-2834-448C-A50E-4644123FA525}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {0D2BE233-2834-448C-A50E-4644123FA525}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| EndGlobal |
13 changes: 13 additions & 0 deletions
13
LambdaInterpreter/LambdaInterpreter/LambdaInterpreter.fsproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| <DockerDefaultTargetOS>Windows</DockerDefaultTargetOS> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="Program.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| open System | ||
| open System.Collections.Generic | ||
| open System.Security.Principal | ||
|
|
||
| type Term = | ||
| | Variable of string | ||
| | Application of Term * Term | ||
| | LambdaAbstraction of string * Term | ||
|
|
||
| let getVar term = | ||
| let rec execute term list = | ||
| match term with | ||
| | Variable(v) -> v :: list | ||
| | Application(term1, term2) -> List.append (execute term1 list) (execute term2 list) | ||
| | LambdaAbstraction(v, term1) -> execute term1 (List.filter(fun x -> x <> v) list) | ||
| execute term [] | ||
| let checkFreeVar var term = term |> getVar |> List.exists(fun x -> x = var) | ||
|
|
||
| let rec substitution var termOut termIn = | ||
| match termOut with | ||
| | Variable(v) -> if v = var then termIn else Variable(var) | ||
| | Application(termApp1, termApp2) -> Application(substitution var termApp1 termIn, substitution var termApp2 termIn) | ||
| | LambdaAbstraction(varInAbs, termInAbs) -> | ||
| match varInAbs with | ||
| | v when var = v -> LambdaAbstraction(varInAbs, termInAbs) | ||
| | v when (checkFreeVar varInAbs termIn) || (checkFreeVar var termInAbs) -> LambdaAbstraction(varInAbs, (substitution var termInAbs termIn)) | ||
| | _ -> | ||
| let newVar = (((getVar termInAbs) @ (getVar termIn)) |> List.toArray |> Array.max) + "1" | ||
| let newTerm = newVar |> Variable | ||
| let termWithNewVar = substitution var termInAbs newTerm | ||
| LambdaAbstraction(newVar, substitution var termWithNewVar termIn ) | ||
|
|
||
|
|
||
| let rec betaReduction term = | ||
| match term with | ||
| | Variable(v) -> Variable(v) | ||
| | Application(LambdaAbstraction(v, term), termForSubstitution) -> betaReduction (substitution v term termForSubstitution) | ||
| | Application(term1, term2) -> Application(betaReduction term1, betaReduction term2) | ||
| | LambdaAbstraction(v, term) -> LambdaAbstraction(v, betaReduction term) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
через
TestCaseData(*case*).Returns(*returns*)и[ < TestCaseSource > ]было бы проще (: