This tutorial uses function name only for for readability. Again, function is anonymous by default in lambda calculus. In the above lambda function and C# function, a function name False is referenced. At run time, a(b)(False) always return Boolean, and converting Boolean to Boolean always succeeds, so And works smoothly without any exception. ![]() In contrast, when Boolean is defined as dynamic –> dynamic -> dynamic, a(b)(False) returns dynamic, which is viewed as supporting any operation at compile time, including implicitly conversion to Boolean, so the explicit type conversion is not required. Since b and False are both of type Boolean, so here it is safe to convert a(b)(False) to Boolean. Here a is either True or False, according to the definition of True and False, a(b)(False) returns either b or False. If Boolean is defined as object –> object -> object: public delegate Func Boolean ( object static partial class ChurchBooleanĪnd must return Boolean, but a(b)(False) returns object, so a type conversion is required. This also demonstrates that dynamic type simplifies type conversion. ![]() Without this alias, the type of And becomes (dynamic –> dynamic –> dynamic) –> (dynamic –> dynamic –> dynamic) –> (dynamic –> dynamic –> dynamic), which is Func>, Func>, Func> in C#. This demonstrates that the Boolean alias improves the readability. In C#, And can be viewed as a => b => a(b)(False), it is of curried function type Boolean –> Boolean -> Boolean: public static partial class ChurchBoolean In C#, this can be viewed as false & b is always false. When a is False, the application is beta reduced to False b False, which applies False function with b and False, and the second argument False is returned.In C#, this can be viewed that true & b is the same as b. When a is True, the application is beta reduced to True b False, which applies True function with b and False, and the first argument b is returned.And can be defined by the following function: And := λa.λb.a b FalseĪpplying function True with Boolean a and b: Logical operatorsĪfter defining Boolean values True and False with functions, now the Boolean logics can be represented by functions too. Actually this F# code is compiled to CIL code similar to above C# structure (static member of a type). There is no noise and the function currying is default. In other functional languages like F#, functions can directly defined: let True t f = t Public delegate Func Boolean ( dynamic that True and False can be defined with lambda expression: public static partial class ChurchBooleanįalse => => does not support defining function directly in the global scope, so True and False are defined as static filed member of a type. Boolean is the alias of dynamic -> dynamic -> dynamic. For convenience, an alias Boolean can be defined for such function type: // Curried from (dynamic, dynamic) -> dynamic. So, the function type of t => f => t and t => f => f is dynamic –> dynamic –> dynamic, which is represented as Func> in C#. In C#, at compile time dynamic is viewed as object and also supports any operation at runtime if the operation is actually not supported, an exception is thrown. ![]() Here t and f can be of any type, so leave their types as dynamic for convenience. In C#, they can be viewed as t => f => t and t => f => f, which are curried from (t, f) => t and (t, f) => f. ![]() In this tutorial, for consistency and intuition, function definition with multiple variables is always represented in the latter curried form. True function simply output the first parameter, and False function output the second parameter: True := λtf.tĪs fore mentioned, λtf.E is just the abbreviation of λt.λf.E, so these definitions actually are: True := λt.λf.t Church Booleanīoolean values True and False can be both represented by anonymous function with 2 parameters. This part discusses Church Boolean - modeling Boolean values and logic operators with functions. Church encoding is named after Alonzo Church, who first discovered this approach. data and operation can be modeled by higher-order anonymous functions and their application. Anonymous function is actually very powerful. Lambda calculus is a formal system for function definition and function application, so in lambda calculus, the only primitive is anonymous function. NET Lambda Calculus Functional Programming Church Encoding Church Booleans
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |