From b7eaab112da55f4521deeed8b70cb1b0ae8f08d0 Mon Sep 17 00:00:00 2001 From: Oskar Gewalli Date: Sat, 28 Jun 2014 14:15:56 +0200 Subject: [PATCH] Cons --- .gitattributes | 63 +++++++ UKanren/.gitignore | 187 +++++++++++++++++++++ UKanren/.nuget/packages.config | 4 + UKanren/Tests/Describe_Cons_ToString.cs | 53 ++++++ UKanren/Tests/Describe_Cons_ctor.cs | 28 +++ UKanren/Tests/Describe_cons_equals.cs | 19 +++ UKanren/Tests/Properties/AssemblyInfo.cs | 36 ++++ UKanren/Tests/TestExtensions.cs | 29 ++++ UKanren/Tests/TestHelper.cs | 13 ++ UKanren/Tests/Tests.csproj | 81 +++++++++ UKanren/Tests/app.config | 11 ++ UKanren/Tests/packages.config | 4 + UKanren/UKanren.sln | 33 ++++ UKanren/UKanren/Cons.cs | 139 +++++++++++++++ UKanren/UKanren/ObjectExtensions.cs | 25 +++ UKanren/UKanren/Properties/AssemblyInfo.cs | 36 ++++ UKanren/UKanren/TypeMatch.cs | 68 ++++++++ UKanren/UKanren/UKanren.csproj | 55 ++++++ lib/MiniKanren.cs | 58 ------- 19 files changed, 884 insertions(+), 58 deletions(-) create mode 100644 .gitattributes create mode 100644 UKanren/.gitignore create mode 100644 UKanren/.nuget/packages.config create mode 100644 UKanren/Tests/Describe_Cons_ToString.cs create mode 100644 UKanren/Tests/Describe_Cons_ctor.cs create mode 100644 UKanren/Tests/Describe_cons_equals.cs create mode 100644 UKanren/Tests/Properties/AssemblyInfo.cs create mode 100644 UKanren/Tests/TestExtensions.cs create mode 100644 UKanren/Tests/TestHelper.cs create mode 100644 UKanren/Tests/Tests.csproj create mode 100644 UKanren/Tests/app.config create mode 100644 UKanren/Tests/packages.config create mode 100644 UKanren/UKanren.sln create mode 100644 UKanren/UKanren/Cons.cs create mode 100644 UKanren/UKanren/ObjectExtensions.cs create mode 100644 UKanren/UKanren/Properties/AssemblyInfo.cs create mode 100644 UKanren/UKanren/TypeMatch.cs create mode 100644 UKanren/UKanren/UKanren.csproj delete mode 100644 lib/MiniKanren.cs diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/UKanren/.gitignore b/UKanren/.gitignore new file mode 100644 index 0000000..c37c7f2 --- /dev/null +++ b/UKanren/.gitignore @@ -0,0 +1,187 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ + +# Roslyn cache directories +*.ide/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +#NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding addin-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +## TODO: Comment the next line if you want to checkin your +## web deploy settings but do note that will include unencrypted +## passwords +*.pubxml + +# NuGet Packages +packages/* +*.nupkg +## TODO: If the tool you use requires repositories.config +## uncomment the next line +#!packages/repositories.config + +# Enable "build/" folder in the NuGet Packages folder since +# NuGet packages use it for MSBuild targets. +# This line needs to be after the ignore of the build folder +# (and the packages folder if the line above has been uncommented) +!packages/build/ + +# Windows Azure Build Output +csx/ +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.pfx +*.publishsettings +node_modules/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ diff --git a/UKanren/.nuget/packages.config b/UKanren/.nuget/packages.config new file mode 100644 index 0000000..7025a72 --- /dev/null +++ b/UKanren/.nuget/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/UKanren/Tests/Describe_Cons_ToString.cs b/UKanren/Tests/Describe_Cons_ToString.cs new file mode 100644 index 0000000..16dcf7d --- /dev/null +++ b/UKanren/Tests/Describe_Cons_ToString.cs @@ -0,0 +1,53 @@ +using NUnit.Framework; + +namespace Tests +{ + [TestFixture] + public class Describe_Cons_ToString : TestHelper + { + [Test] + public void It_prints_an_expression_correctly() + { + var c = cons(1, cons(2, cons(cons(3, cons(4, nil)), cons(5, nil)))); + c.ToString().must_equal("(1 2 (3 4) 5)"); + var d = cons(cons(2, 3), 1); + d.ToString().must_equal("((2 . 3) . 1)"); + var e = cons(1, nil); + e.ToString().must_equal("(1)"); + //# Is this an illogical list to be testing? + var f = cons(nil, 1); + f.ToString().must_equal("(nil . 1)"); + } + + [Test] + public void It_prints_a_cons_cell_representation_of_a_simple_cell() + { + cons('a', 'b').ToString().must_equal("(\"a\" . \"b\")"); + } + + [Test] + public void It_represents_Integers_and_Floats() + { + cons(1, 2).ToString().must_equal("(1 . 2)"); + } + + [Test] + public void It_prints_a_nested_expression() + { + cons('a', cons('b', 'c')).ToString().must_equal(@"(""a"" ""b"" . ""c"")"); + } + + [Test] + public void It_represents_Arrays_correctly_in_printed_form() + { + cons('a', new object[0]).ToString().must_equal(@"(""a"" . [])"); + } + + [Test] + public void It_represents_nil_elements() + { + cons('a', nil).ToString().must_equal(@"(""a"")"); + } + + } +} \ No newline at end of file diff --git a/UKanren/Tests/Describe_Cons_ctor.cs b/UKanren/Tests/Describe_Cons_ctor.cs new file mode 100644 index 0000000..1d50ba4 --- /dev/null +++ b/UKanren/Tests/Describe_Cons_ctor.cs @@ -0,0 +1,28 @@ +using System; +using MicroKanren; +using NUnit.Framework; + +namespace Tests +{ + [TestFixture] + public class Describe_Cons_ctor:TestHelper + { + [Test] + public void It_takes_two_arguments_for_car_and_cdr() + { + new Cons(1, 2).must_be_instance_of(typeof (Cons)); + } + + [Test] + public void It_cant_handle_null_due_to_csharp_typesystem() + { + Action ctor = () => { new Cons(1, null); }; ctor.must_raise(); + } + + [Test] + public void It_can_handle_null_if_using_cons_factory() + { + Cons.New(1, null).Cdr.must_equal(nil); + } + } +} \ No newline at end of file diff --git a/UKanren/Tests/Describe_cons_equals.cs b/UKanren/Tests/Describe_cons_equals.cs new file mode 100644 index 0000000..f8549ee --- /dev/null +++ b/UKanren/Tests/Describe_cons_equals.cs @@ -0,0 +1,19 @@ +using NUnit.Framework; + +namespace Tests +{ + [TestFixture] + public class Describe_cons_equals:TestHelper + { + [Test] + public void It_is_true_if_the_lists_are_equal() + { + cons(1, cons(2, nil)).must_equal(cons(1, cons(2, nil))); + } + [Test] + public void It_is_false_if_the_lists_contain_different_objects() + { + cons(1, cons(2, nil)).wont_equal(cons(1, nil)); + } + } +} \ No newline at end of file diff --git a/UKanren/Tests/Properties/AssemblyInfo.cs b/UKanren/Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..9401aa6 --- /dev/null +++ b/UKanren/Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Tests")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("5a650f6a-0dca-4581-881b-161ea9607852")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/UKanren/Tests/TestExtensions.cs b/UKanren/Tests/TestExtensions.cs new file mode 100644 index 0000000..a793a0c --- /dev/null +++ b/UKanren/Tests/TestExtensions.cs @@ -0,0 +1,29 @@ +using System; +using NUnit.Framework; + +namespace Tests +{ + internal static class TestExtensions + { + public static T must_be_instance_of(this T t, Type type) + { + Assert.That(t, Is.InstanceOf(type)); + return t; + } + public static T must_equal(this T t, T val) + { + Assert.That(t, Is.EqualTo(val)); + return t; + } + + public static T wont_equal(this T t, T val) + { + Assert.That(t, Is.Not.EqualTo(val)); + return t; + } + public static void must_raise(this Action a) where TException : System.Exception + { + Assert.Throws(() => a()); + } + } +} \ No newline at end of file diff --git a/UKanren/Tests/TestHelper.cs b/UKanren/Tests/TestHelper.cs new file mode 100644 index 0000000..463429a --- /dev/null +++ b/UKanren/Tests/TestHelper.cs @@ -0,0 +1,13 @@ +using MicroKanren; + +namespace Tests +{ + public class TestHelper + { + public Cons cons(T1 car, T2 cdr) + { + return new Cons(car, cdr); + } + public object nil { get { return new EmptyCons(); } } + } +} diff --git a/UKanren/Tests/Tests.csproj b/UKanren/Tests/Tests.csproj new file mode 100644 index 0000000..3e3ff55 --- /dev/null +++ b/UKanren/Tests/Tests.csproj @@ -0,0 +1,81 @@ + + + + + Debug + AnyCPU + {0776BA22-191E-4C15-AA59-F9077472CB82} + Library + Properties + Tests + Tests + v4.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + False + ..\packages\NUnit.2.6.3\lib\nunit.framework.dll + + + + + + + + + + + + + + + + + + + + + + + {56f93cdc-b1b4-416c-8880-c956676b281a} + UKanren + + + + + ..\packages\NUnit.Runners.2.6.3\tools\lib\nunit.core.dll + + + ..\packages\NUnit.Runners.2.6.3\tools\lib\nunit.core.interfaces.dll + + + + + \ No newline at end of file diff --git a/UKanren/Tests/app.config b/UKanren/Tests/app.config new file mode 100644 index 0000000..a051a21 --- /dev/null +++ b/UKanren/Tests/app.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/UKanren/Tests/packages.config b/UKanren/Tests/packages.config new file mode 100644 index 0000000..ad37a52 --- /dev/null +++ b/UKanren/Tests/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/UKanren/UKanren.sln b/UKanren/UKanren.sln new file mode 100644 index 0000000..ddbe07c --- /dev/null +++ b/UKanren/UKanren.sln @@ -0,0 +1,33 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.30501.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UKanren", "UKanren\UKanren.csproj", "{56F93CDC-B1B4-416C-8880-C956676B281A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{0776BA22-191E-4C15-AA59-F9077472CB82}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{F6A033E6-307F-47E0-B996-A8F083B49153}" + ProjectSection(SolutionItems) = preProject + .nuget\packages.config = .nuget\packages.config + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {56F93CDC-B1B4-416C-8880-C956676B281A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {56F93CDC-B1B4-416C-8880-C956676B281A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {56F93CDC-B1B4-416C-8880-C956676B281A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {56F93CDC-B1B4-416C-8880-C956676B281A}.Release|Any CPU.Build.0 = Release|Any CPU + {0776BA22-191E-4C15-AA59-F9077472CB82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0776BA22-191E-4C15-AA59-F9077472CB82}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0776BA22-191E-4C15-AA59-F9077472CB82}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0776BA22-191E-4C15-AA59-F9077472CB82}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/UKanren/UKanren/Cons.cs b/UKanren/UKanren/Cons.cs new file mode 100644 index 0000000..7d64f9a --- /dev/null +++ b/UKanren/UKanren/Cons.cs @@ -0,0 +1,139 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; + +namespace MicroKanren +{ + public abstract class Cons + { + + public static EmptyCons Nil + { + get { return new EmptyCons(); } + } + + public abstract String ToString(bool consInCdr); + + public static Cons New(object car, object cdr) + { + return new Cons(car ?? Nil, cdr ?? Nil); + } + public virtual object Car + { + get { throw new Exception(); } + } + + public virtual object Cdr + { + get { throw new Exception(); } + } + + public override bool Equals(object obj) + { + return obj is Cons ? Equals(obj as Cons) : false; + } + + public virtual bool Equals(Cons other) + { + if (other is EmptyCons && this is EmptyCons) + return true; + return Equals(Car, other.Car) && Equals(Cdr, other.Cdr); + } + + public override int GetHashCode() + { + unchecked + { + return (Car.GetHashCode() * 397) ^ Cdr.GetHashCode(); + } + } + + } + + public class EmptyCons : Cons + { + public override string ToString(bool consInCdr) + { + return "nil"; + } + + public override string ToString() + { + return ToString(false); + } + + public override bool Equals(object obj) + { + return obj is EmptyCons; + } + + public override int GetHashCode() + { + return 0; + } + } + + public class Cons : Cons + { + //attr_reader :car, :cdr + public override Object Car + { + get { return CarT; } + } + public override Object Cdr + { + get { return CdrT; } + } + + public T1 CarT { get; private set; } + public T2 CdrT { get; private set; } + //# Returns a Cons cell (read: instance) that is also marked as such for + //# later identification. + public Cons(T1 car, T2 cdr) + { + if (Equals(car , null)) + { + throw new ArgumentException("car"); + } + if (Equals(cdr, null)) + { + throw new ArgumentException("cdr"); + } + CarT = car; CdrT = cdr; + } + + //# Converts Lisp AST to a String. Algorithm is a recursive implementation of + //# http://www.mat.uc.pt/~pedro/cientificos/funcional/lisp/gcl_22.html#SEC1238. + public override String ToString() + { + return ToString(false); + } + + public override String ToString(bool consInCdr) + { + var str = new StringBuilder(consInCdr ? "" : "("); + str.Append((this.Car is Cons) ? this.Car.ToString() : AtomString(this.Car)); + + str.Append(TypeMatch.Case(this.Cdr, + (EmptyCons e) => string.Empty, + (Cons c) => " " + c.ToString(true), + o => " . " + AtomString(o))); + + return consInCdr ? str.ToString() : str.Append(")").ToString(); + } + + //private + + private static string AtomString(object car) + { + return TypeMatch.Case(car, + (EmptyCons e) => string.Empty, + (String s) => s.Inspect(), + (char c) => c.Inspect(), + (IEnumerable e) => e.Inspect(), + o => o.ToString() + ); + } + } +} diff --git a/UKanren/UKanren/ObjectExtensions.cs b/UKanren/UKanren/ObjectExtensions.cs new file mode 100644 index 0000000..2bf0cbd --- /dev/null +++ b/UKanren/UKanren/ObjectExtensions.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections; +using System.Globalization; +using System.Linq; + +namespace MicroKanren +{ + internal static class ObjectExtensions + { + public static string Inspect(this string s) + { + return "\"" + s.Replace("\"", @"\\""") + "\""; + } + + public static string Inspect(this char s) + { + return s.ToString(CultureInfo.InvariantCulture).Inspect(); + } + + public static string Inspect(this IEnumerable e) + { + return "["+String.Join(", ", e.Cast())+"]"; + } + } +} \ No newline at end of file diff --git a/UKanren/UKanren/Properties/AssemblyInfo.cs b/UKanren/UKanren/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..63d6f92 --- /dev/null +++ b/UKanren/UKanren/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("UKanren")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("UKanren")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("e60aa73b-b6c8-4070-a92d-5820d0259ac0")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/UKanren/UKanren/TypeMatch.cs b/UKanren/UKanren/TypeMatch.cs new file mode 100644 index 0000000..8a1482f --- /dev/null +++ b/UKanren/UKanren/TypeMatch.cs @@ -0,0 +1,68 @@ +using System; + +namespace MicroKanren +{ + internal class TypeMatch + { + public static TRet Case(object o, Func a1, Func _else) + { + if (o is T1) + { + return a1((T1)o); + } + if (_else != null) return _else(o); + return default(TRet); + } + public static TRet Case(object o, Func a1, Func a2, Func _else) + { + if (o is T1) + { + return a1((T1)o); + } + if (o is T2) + { + return a2((T2)o); + } + if (_else != null) return _else(o); + return default(TRet); + } + public static TRet Case(object o, Func a1, Func a2, Func a3, Func _else) + { + if (o is T1) + { + return a1((T1)o); + } + if (o is T2) + { + return a2((T2)o); + } + if (o is T3) + { + return a3((T3)o); + } + if (_else != null) return _else(o); + return default(TRet); + } + public static TRet Case(object o, Func a1, Func a2, Func a3,Func a4, Func _else) + { + if (o is T1) + { + return a1((T1)o); + } + if (o is T2) + { + return a2((T2)o); + } + if (o is T3) + { + return a3((T3)o); + } + if (o is T4) + { + return a4((T4)o); + } + if (_else != null) return _else(o); + return default(TRet); + } + } +} \ No newline at end of file diff --git a/UKanren/UKanren/UKanren.csproj b/UKanren/UKanren/UKanren.csproj new file mode 100644 index 0000000..4520980 --- /dev/null +++ b/UKanren/UKanren/UKanren.csproj @@ -0,0 +1,55 @@ + + + + + Debug + AnyCPU + {56F93CDC-B1B4-416C-8880-C956676B281A} + Library + Properties + MicroKanren + UKanren + v4.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/MiniKanren.cs b/lib/MiniKanren.cs deleted file mode 100644 index 8de232f..0000000 --- a/lib/MiniKanren.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; - -namespace MicroKanren{ - - public class Cons{ - //attr_reader :car, :cdr - public T1 Car{get;private set;} - public T2 Cdr{get;private set;} - //# Returns a Cons cell (read: instance) that is also marked as such for - //# later identification. - public Cons(T1 car,T2 cdr){ //def initialize(car, cdr) - Car=car; Cdr = cdr; - }//end - - //# Converts Lisp AST to a String. Algorithm is a recursive implementation of - //# http://www.mat.uc.pt/~pedro/cientificos/funcional/lisp/gcl_22.html#SEC1238. - public override String ToString(){ - return ToString(false); - } - public String ToString(bool consInCdr){ - var str = new StringBuilder(consInCdr ? "" : "("); - - - /*def to_s(cons_in_cdr = false) - str = cons_in_cdr ? '' : '(' - - str += self.car.is_a?(Cons) ? self.car.to_s : atom_string(self.car) - - str += case self.cdr - when Cons - ' ' + self.cdr.to_s(true) - when NilClass - '' - else - ' . ' + atom_string(self.cdr) - end - - cons_in_cdr ? str : str << ')' - end*/ - } - - /*def ==(other) - other.is_a?(Cons) ? self.car == other.car && self.cdr == other.cdr : false - end*/ - - //private - - /*def atom_string(node) - case node - when NilClass, Array, String - node.inspect - else - node.to_s - end - end*/ - -} -}