User loginNavigation |
archivesType-safe solution to the expression problem in C#?Posted this on Channel9 (http://channel9.msdn.com/ShowPost.aspx?PostID=386812), but didn't get much feedback, so I thought I'd repost here. Basically, I've been looking at a type-safe approach for the expression problem in C#, taking advantage of the lambda expression runtime compilation command Expression.Compile() in C#'s Language Integrated Query (LINQ). The biggest drawback to the solution is that the compiled lambda functions take about 2x longer to execute on primitive expressions compared to non-generic solutions. I haven't yet found an EvalStream implementation that can avoid this overhead of lambda evaluation. I'm pretty new to functional programming, but I'd love to hear anyone's thoughts on the approach I'm trying here (see LinFu author Philip Laureano's comments (thanks Philip!): http://plaureano.blogspot.com/2008/02/linfudynamicobject-and-dynamic-object.html#c4431642200009960939) using System; namespace TPLTest Array array1 = new Array(1000).InitializeTo(10); Console.Read(); public struct Number where T : struct public static implicit operator Number(T myValue) { return new Number() { numValue = myValue }; } public class Array where T : struct public Array() { } public static implicit operator T[](Array myArray) { return myArray.arrayInstance; } public static Array operator +(Array a, Array b) { return GADT.AddFunc.EvalStream(a, b); } public T[] ToArray() { return arrayInstance; } public static class ArrayExtensions public static class GADT internal static class GADTHelper public static double Subtract(double a, double b) { return a - b; } public static double Multiply(double a, double b) { return a * b; } public static double Divide(double a, double b) { return a / b; } public static class DynamicExtensions Uniqueness Typing SimplifiedUniqueness Typing Simplified, by Edsko de Vries, Rinus Plasmeijer, and David M. Abrahamson.
Uniqueness typing is related to linear typing, and their differences have been discussed here before. Linear types have many applications. This paper describes the difference between linear and unique types:
In contrast to other papers on substructural typing, such as Fluet's thesis Monadic and Substructural Type Systems for Region-Based Memory Management, this paper classifies uniqueness attributes by a kind system. This possibility was mentioned in Fluet's thesis as well, Section 4.2, footnote 8, though the technique used here seems somewhat different. Uniqueness typing is generally used to tame side-effects in purely functional languages. Uniqueness types have also been contrasted with monads on LTU, which are also used to tame effects, among other things. One point not discussed in that thread, is how straightforward it is to compile each approach into efficient code. It seems clear that uniqueness types have a straightforward, efficient compilation, but it's not clear to me how efficient monads can be, and how much work is required to make them efficient. This may make uniqueness or substructural types more suitable for just-in-time compilers than monads. |
Browse archivesActive forum topics |
Recent comments
22 weeks 1 day ago
22 weeks 2 days ago
22 weeks 2 days ago
44 weeks 3 days ago
48 weeks 5 days ago
50 weeks 2 days ago
50 weeks 2 days ago
1 year 6 days ago
1 year 5 weeks ago
1 year 5 weeks ago