From f1e9456e65008e23dd914af9dea0b314c724c040 Mon Sep 17 00:00:00 2001 From: dmitry Date: Thu, 29 Oct 2020 06:50:32 +0300 Subject: [PATCH 1/3] All files added --- semester4/Interpreter/Interpreter.sln | 22 ++++++++ .../Interpreter/Interpreter/Interpreter.fs | 54 +++++++++++++++++++ .../Interpreter/Interpreter.fsproj | 11 ++++ .../InterpreterTests/InterpreterTests.fsproj | 26 +++++++++ .../Interpreter/InterpreterTests/Program.fs | 1 + .../Interpreter/InterpreterTests/UnitTest1.fs | 44 +++++++++++++++ 6 files changed, 158 insertions(+) create mode 100644 semester4/Interpreter/Interpreter.sln create mode 100644 semester4/Interpreter/Interpreter/Interpreter.fs create mode 100644 semester4/Interpreter/Interpreter/Interpreter.fsproj create mode 100644 semester4/Interpreter/InterpreterTests/InterpreterTests.fsproj create mode 100644 semester4/Interpreter/InterpreterTests/Program.fs create mode 100644 semester4/Interpreter/InterpreterTests/UnitTest1.fs diff --git a/semester4/Interpreter/Interpreter.sln b/semester4/Interpreter/Interpreter.sln new file mode 100644 index 0000000..09b7c2f --- /dev/null +++ b/semester4/Interpreter/Interpreter.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Interpreter", "Interpreter\Interpreter.fsproj", "{F4AC6C26-010E-4A43-9A7E-DE86E607064B}" +EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "InterpreterTests", "InterpreterTests\InterpreterTests.fsproj", "{B0759521-85A7-4058-A22F-DAC2B0BC2EAE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F4AC6C26-010E-4A43-9A7E-DE86E607064B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F4AC6C26-010E-4A43-9A7E-DE86E607064B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F4AC6C26-010E-4A43-9A7E-DE86E607064B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F4AC6C26-010E-4A43-9A7E-DE86E607064B}.Release|Any CPU.Build.0 = Release|Any CPU + {B0759521-85A7-4058-A22F-DAC2B0BC2EAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B0759521-85A7-4058-A22F-DAC2B0BC2EAE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B0759521-85A7-4058-A22F-DAC2B0BC2EAE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B0759521-85A7-4058-A22F-DAC2B0BC2EAE}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/semester4/Interpreter/Interpreter/Interpreter.fs b/semester4/Interpreter/Interpreter/Interpreter.fs new file mode 100644 index 0000000..006f7a8 --- /dev/null +++ b/semester4/Interpreter/Interpreter/Interpreter.fs @@ -0,0 +1,54 @@ +module Interpreter + +/// Лямбда-терм, бывает трёх видов +type Term = + | Variable of char + | Abstraction of char * Term + | Application of Term * Term + +/// Свободна переменная в выражении или нет +let rec isFree expression variable = + match expression with + | Variable var -> var = variable + | Abstraction (var, term) -> var <> variable && isFree term variable + | Application (left, right) -> isFree left variable || isFree right variable + +/// Замена переменной в выражении +let rec substitution oldVariable newVariable expression = + let vars = [ 'a' .. 'z' ] + match expression with + | Variable var when var = oldVariable -> newVariable + | Variable _ -> expression + | Abstraction (var, _) when var = oldVariable -> expression + | Abstraction (var, term) when not <| isFree newVariable var -> + Abstraction(var, substitution oldVariable newVariable term) + | Abstraction (var, term) -> + let newSymbol = + vars + |> List.filter (not << isFree newVariable) + |> List.head + + Abstraction + (newSymbol, + substitution oldVariable newVariable + <| substitution var (Variable newSymbol) term) + | Application (left, right) -> + Application(substitution oldVariable newVariable left, substitution oldVariable newVariable right) + +/// Бета-редукция выражения +let betaReduction (term: Term) = + let rec betaReductionRec term = + match term with + | Variable var -> Variable(var) + | Abstraction (var, term) -> Abstraction(var, betaReductionRec <| term) + | Application (Abstraction (var, leftTerm), rightTerm) -> + leftTerm + |> substitution var rightTerm + |> betaReductionRec + | Application (left, right) -> + let leftReduce = betaReductionRec left + match leftReduce with + | Abstraction (_) -> Application(leftReduce, right) |> betaReductionRec + | _ -> Application(leftReduce, betaReductionRec right) + + betaReductionRec term diff --git a/semester4/Interpreter/Interpreter/Interpreter.fsproj b/semester4/Interpreter/Interpreter/Interpreter.fsproj new file mode 100644 index 0000000..877612d --- /dev/null +++ b/semester4/Interpreter/Interpreter/Interpreter.fsproj @@ -0,0 +1,11 @@ + + + + netcoreapp3.1 + + + + + + + diff --git a/semester4/Interpreter/InterpreterTests/InterpreterTests.fsproj b/semester4/Interpreter/InterpreterTests/InterpreterTests.fsproj new file mode 100644 index 0000000..8399cf4 --- /dev/null +++ b/semester4/Interpreter/InterpreterTests/InterpreterTests.fsproj @@ -0,0 +1,26 @@ + + + + netcoreapp3.1 + + false + false + + + + + + + + + + + + + + + + + + + diff --git a/semester4/Interpreter/InterpreterTests/Program.fs b/semester4/Interpreter/InterpreterTests/Program.fs new file mode 100644 index 0000000..0695f84 --- /dev/null +++ b/semester4/Interpreter/InterpreterTests/Program.fs @@ -0,0 +1 @@ +module Program = let [] main _ = 0 diff --git a/semester4/Interpreter/InterpreterTests/UnitTest1.fs b/semester4/Interpreter/InterpreterTests/UnitTest1.fs new file mode 100644 index 0000000..3b6c493 --- /dev/null +++ b/semester4/Interpreter/InterpreterTests/UnitTest1.fs @@ -0,0 +1,44 @@ +module InterpreterTests + +open NUnit.Framework +open FsUnit +open Interpreter + +[] +let ``simple variable test`` () = + Variable('x') + |> betaReduction + |> should equal (Variable('x')) + +[] +let ``simple abstraction test`` () = + Abstraction('x', Variable('x')) + |> betaReduction + |> should equal (Abstraction('x', Variable('x'))) + +[] +let ``simple application test`` () = + Application(Variable('x'), Variable('y')) + |> betaReduction + |> should equal (Application(Variable('x'), Variable('y'))) + +// (λx.x y) +[] +let ``simple test`` () = + Application(Abstraction('x', Variable('x')), Variable('y')) + |> betaReduction + |> should equal (Variable('y')) + +// λx.(x y) z +[] +let ``simple test two`` () = + Application(Abstraction('x', Application(Variable('x'), Variable('y'))), Variable('z')) + |> betaReduction + |> should equal (Application(Variable('z'), Variable('y'))) + +// (λx.y z) (λa.(a z) b) +[] +let ``simple test three`` () = + Application(Application(Abstraction('x', Variable('y')), Variable('z')), Application(Abstraction('a', Application(Variable('a'), Variable('z'))), Variable('b'))) + |> betaReduction + |> should equal (Application(Variable('y'), Application(Variable('b'), Variable('z')))) \ No newline at end of file From 58f4a4131e37a1d5bc39f89eb6325ba0d80b363d Mon Sep 17 00:00:00 2001 From: dmitry Date: Thu, 29 Oct 2020 07:15:29 +0300 Subject: [PATCH 2/3] Updated files --- .../InterpreterTests/{UnitTest1.fs => InterpreterTest.fs} | 0 .../Interpreter/InterpreterTests/InterpreterTests.fsproj | 7 +++---- semester4/Interpreter/InterpreterTests/Program.fs | 1 - 3 files changed, 3 insertions(+), 5 deletions(-) rename semester4/Interpreter/InterpreterTests/{UnitTest1.fs => InterpreterTest.fs} (100%) delete mode 100644 semester4/Interpreter/InterpreterTests/Program.fs diff --git a/semester4/Interpreter/InterpreterTests/UnitTest1.fs b/semester4/Interpreter/InterpreterTests/InterpreterTest.fs similarity index 100% rename from semester4/Interpreter/InterpreterTests/UnitTest1.fs rename to semester4/Interpreter/InterpreterTests/InterpreterTest.fs diff --git a/semester4/Interpreter/InterpreterTests/InterpreterTests.fsproj b/semester4/Interpreter/InterpreterTests/InterpreterTests.fsproj index 8399cf4..8011ef6 100644 --- a/semester4/Interpreter/InterpreterTests/InterpreterTests.fsproj +++ b/semester4/Interpreter/InterpreterTests/InterpreterTests.fsproj @@ -3,8 +3,8 @@ netcoreapp3.1 - false - false + true + true @@ -15,8 +15,7 @@ - - + diff --git a/semester4/Interpreter/InterpreterTests/Program.fs b/semester4/Interpreter/InterpreterTests/Program.fs deleted file mode 100644 index 0695f84..0000000 --- a/semester4/Interpreter/InterpreterTests/Program.fs +++ /dev/null @@ -1 +0,0 @@ -module Program = let [] main _ = 0 From 3330a7c68973eef766a6ed9be6bf7ca50fc7e5c8 Mon Sep 17 00:00:00 2001 From: dmitry Date: Thu, 29 Oct 2020 07:18:30 +0300 Subject: [PATCH 3/3] Changed appveyor.yml --- appveyor.yml | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 855f34f..787c08d 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,17 @@ -image: Visual Studio 2017 +image: Visual Studio 2019 + +init: + - git config --global core.autocrlf true + +environment: + matrix: + - solution: Semester4/Interpreter/Interpreter.sln before_build: - - nuget restore semester2/6.1/HW6T2.sln + - nuget restore %solution% build: - project: semester2/2.3/2.3.sln + project: $(solution) + +test_script: + - dotnet test %solution%