fbpx
Wikipedia

Mogensen–Scott encoding

In computer science, Scott encoding is a way to represent (recursive) data types in the lambda calculus. Church encoding performs a similar function. The data and operators form a mathematical structure which is embedded in the lambda calculus.

Whereas Church encoding starts with representations of the basic data types, and builds up from it, Scott encoding starts from the simplest method to compose algebraic data types.

Mogensen–Scott encoding extends and slightly modifies Scott encoding by applying the encoding to Metaprogramming[citation needed]. This encoding allows the representation of lambda calculus terms, as data, to be operated on by a meta program.

History edit

Scott encoding appears first in a set of unpublished lecture notes by Dana Scott[1] whose first citation occurs in the book Combinatorial Logic, Volume II.[2] Michel Parigot gave a logical interpretation of and strongly normalizing recursor for Scott-encoded numerals,[3] referring to them as the "Stack type" representation of numbers. Torben Mogensen later extended Scott encoding for the encoding of Lambda terms as data.[4]

Discussion edit

Lambda calculus allows data to be stored as parameters to a function that does not yet have all the parameters required for application. For example,

 

May be thought of as a record or struct where the fields   have been initialized with the values  . These values may then be accessed by applying the term to a function f. This reduces to,

 

c may represent a constructor for an algebraic data type in functional languages such as Haskell. Now suppose there are N constructors, each with   arguments;

 

Each constructor selects a different function from the function parameters  . This provides branching in the process flow, based on the constructor. Each constructor may have a different arity (number of parameters). If the constructors have no parameters then the set of constructors acts like an enum; a type with a fixed number of values. If the constructors have parameters, recursive data structures may be constructed.

Definition edit

Let D be a datatype with N constructors,  , such that constructor   has arity  .

Scott encoding edit

The Scott encoding of constructor   of the data type D is

 

Mogensen–Scott encoding edit

Mogensen extends Scott encoding to encode any untyped lambda term as data. This allows a lambda term to be represented as data, within a Lambda calculus meta program. The meta function mse converts a lambda term into the corresponding data representation of the lambda term;

 

The "lambda term" is represented as a tagged union with three cases:

  • Constructor a - a variable (arity 1, not recursive)
  • Constructor b - function application (arity 2, recursive in both arguments),
  • Constructor c - lambda-abstraction (arity 1, recursive).

For example,

 

Comparison to the Church encoding edit

The Scott encoding coincides with the Church encoding for booleans. Church encoding of pairs may be generalized to arbitrary data types by encoding   of D above as[citation needed]

 

compare this to the Mogensen Scott encoding,

 

With this generalization, the Scott and Church encodings coincide on all enumerated datatypes (such as the boolean datatype) because each constructor is a constant (no parameters).

Concerning the practicality of using either the Church or Scott encoding for programming, there is a symmetric trade-off:[5] Church-encoded numerals support a constant-time addition operation and have no better than a linear-time predecessor operation; Scott-encoded numerals support a constant-time predecessor operation and have no better than a linear-time addition operation.

Type definitions edit

Church-encoded data and operations on them are typable in system F, as are Scott-encoded data and operations. However, the encoding is significantly more complicated.[6]

The type of the Scott encoding of the natural numbers is the positive recursive type:

 

Full recursive types are not part of System F, but positive recursive types are expressible in System F via the encoding:

 

Combining these two facts yields the System F type of the Scott encoding:

 

This can be contrasted with the type of the Church encoding:

 

The Church encoding is a second-order type, but the Scott encoding is fourth-order!


See also edit

Notes edit

  1. ^ Scott, Dana (1968) [1962]. A system of functional abstraction. Lectures delivered at University of California, Berkeley.
  2. ^ Curry, Haskell (1972). Combinatorial Logic, Volume II. North-Holland Publishing Company. ISBN 0-7204-2208-6.
  3. ^ Parigot, Michel (1988). "Programming with proofs: A second order type theory". In H. Ganzinger (ed.). European Symposium on Programming: ESOP '88. 2nd European Symposium on Programming. Nancy, France, March 21–24, 1988. Lecture Notes in Computer Science. Vol. 300. Springer. pp. 145–159. doi:10.1007/3-540-19027-9_10. ISBN 978-3-540-19027-1.
  4. ^ Mogensen, Torben (1994). "Efficient Self-Interpretation in Lambda Calculus". Journal of Functional Programming. 2 (3): 345–364. doi:10.1017/S0956796800000423. S2CID 8736707.
  5. ^ Parigot, Michel (1990). "On the representation of data in lambda-calculus". In Egon Börger; Hans Kleine Büning; Michael M. Richter (eds.). International Workshop on Computer Science Logic: CSL '89. 3rd Workshop on Computer Science Logic. Kaiserslautern, FRG, October 2-6, 1989. Lecture Notes in Computer Science. Vol. 440. Springer. pp. 209–321. doi:10.1007/3-540-52753-2_47. ISBN 978-3-540-52753-4.
  6. ^ See the note "Types for the Scott numerals" by Martín Abadi, Luca Cardelli and Gordon Plotkin (February 18, 1993).

References edit

  • Stump, A. (2009). Directly reflective meta-programming. Higher-Order and Symbolic Computation, 22, 115-144.
  • Mogensen, T.Æ. (1992). Efficient Self-Interpretations in lambda Calculus. J. Funct. Program., 2, 345-363.

mogensen, scott, encoding, computer, science, scott, encoding, represent, recursive, data, types, lambda, calculus, church, encoding, performs, similar, function, data, operators, form, mathematical, structure, which, embedded, lambda, calculus, whereas, churc. In computer science Scott encoding is a way to represent recursive data types in the lambda calculus Church encoding performs a similar function The data and operators form a mathematical structure which is embedded in the lambda calculus Whereas Church encoding starts with representations of the basic data types and builds up from it Scott encoding starts from the simplest method to compose algebraic data types Mogensen Scott encoding extends and slightly modifies Scott encoding by applying the encoding to Metaprogramming citation needed This encoding allows the representation of lambda calculus terms as data to be operated on by a meta program Contents 1 History 2 Discussion 3 Definition 3 1 Scott encoding 3 2 Mogensen Scott encoding 4 Comparison to the Church encoding 5 Type definitions 6 See also 7 Notes 8 ReferencesHistory editScott encoding appears first in a set of unpublished lecture notes by Dana Scott 1 whose first citation occurs in the book Combinatorial Logic Volume II 2 Michel Parigot gave a logical interpretation of and strongly normalizing recursor for Scott encoded numerals 3 referring to them as the Stack type representation of numbers Torben Mogensen later extended Scott encoding for the encoding of Lambda terms as data 4 Discussion editLambda calculus allows data to be stored as parameters to a function that does not yet have all the parameters required for application For example l x 1 x n l c c x 1 x n v 1 v n f displaystyle lambda x 1 ldots x n lambda c c x 1 ldots x n v 1 ldots v n f nbsp May be thought of as a record or struct where the fields x 1 x n displaystyle x 1 ldots x n nbsp have been initialized with the values v 1 v n displaystyle v 1 ldots v n nbsp These values may then be accessed by applying the term to a function f This reduces to f v 1 v n displaystyle f v 1 ldots v n nbsp c may represent a constructor for an algebraic data type in functional languages such as Haskell Now suppose there are N constructors each with A i displaystyle A i nbsp arguments Constructor Given arguments Result l x 1 x A 1 l c 1 c N c 1 x 1 x A 1 v 1 v A 1 f 1 f N f 1 v 1 v A 1 l x 1 x A 2 l c 1 c N c 2 x 1 x A 2 v 1 v A 2 f 1 f N f 2 v 1 v A 2 l x 1 x A N l c 1 c N c N x 1 x A N v 1 v A N f 1 f N f N v 1 v A N displaystyle begin array c c c text Constructor amp text Given arguments amp text Result hline lambda x 1 ldots x A 1 lambda c 1 ldots c N c 1 x 1 ldots x A 1 v 1 ldots v A 1 amp f 1 ldots f N amp f 1 v 1 ldots v A 1 lambda x 1 ldots x A 2 lambda c 1 ldots c N c 2 x 1 ldots x A 2 v 1 ldots v A 2 amp f 1 ldots f N amp f 2 v 1 ldots v A 2 vdots amp vdots amp vdots lambda x 1 ldots x A N lambda c 1 ldots c N c N x 1 ldots x A N v 1 ldots v A N amp f 1 ldots f N amp f N v 1 ldots v A N end array nbsp Each constructor selects a different function from the function parameters f 1 f N displaystyle f 1 ldots f N nbsp This provides branching in the process flow based on the constructor Each constructor may have a different arity number of parameters If the constructors have no parameters then the set of constructors acts like an enum a type with a fixed number of values If the constructors have parameters recursive data structures may be constructed Definition editLet D be a datatype with N constructors c i i 1 N displaystyle c i i 1 N nbsp such that constructor c i displaystyle c i nbsp has arity A i displaystyle A i nbsp Scott encoding edit The Scott encoding of constructor c i displaystyle c i nbsp of the data type D is l x 1 x A i l c 1 c N c i x 1 x A i displaystyle lambda x 1 ldots x A i lambda c 1 ldots c N c i x 1 ldots x A i nbsp Mogensen Scott encoding edit Mogensen extends Scott encoding to encode any untyped lambda term as data This allows a lambda term to be represented as data within a Lambda calculus meta program The meta function mse converts a lambda term into the corresponding data representation of the lambda term mse x l a b c a x mse M N l a b c b mse M mse N mse l x M l a b c c l x mse M displaystyle begin aligned operatorname mse x amp lambda a b c a x operatorname mse M N amp lambda a b c b operatorname mse M operatorname mse N operatorname mse lambda x M amp lambda a b c c lambda x operatorname mse M end aligned nbsp The lambda term is represented as a tagged union with three cases Constructor a a variable arity 1 not recursive Constructor b function application arity 2 recursive in both arguments Constructor c lambda abstraction arity 1 recursive For example mse l x f x x l a b c c l x mse f x x l a b c c l x l a b c b mse f mse x x l a b c c l x l a b c b l a b c a f mse x x l a b c c l x l a b c b l a b c a f l a b c b mse x mse x l a b c c l x l a b c b l a b c a f l a b c b l a b c a x l a b c a x displaystyle begin array l operatorname mse lambda x f x x lambda a b c c lambda x operatorname mse f x x lambda a b c c lambda x lambda a b c b operatorname mse f operatorname mse x x lambda a b c c lambda x lambda a b c b lambda a b c a f operatorname mse x x lambda a b c c lambda x lambda a b c b lambda a b c a f lambda a b c b operatorname mse x operatorname mse x lambda a b c c lambda x lambda a b c b lambda a b c a f lambda a b c b lambda a b c a x lambda a b c a x end array nbsp Comparison to the Church encoding editThe Scott encoding coincides with the Church encoding for booleans Church encoding of pairs may be generalized to arbitrary data types by encoding c i displaystyle c i nbsp of D above as citation needed l x 1 x A i l c 1 c N c i x 1 c 1 c N x A i c 1 c N displaystyle lambda x 1 ldots x A i lambda c 1 ldots c N c i x 1 c 1 ldots c N ldots x A i c 1 ldots c N nbsp compare this to the Mogensen Scott encoding l x 1 x A i l c 1 c N c i x 1 x A i displaystyle lambda x 1 ldots x A i lambda c 1 ldots c N c i x 1 ldots x A i nbsp With this generalization the Scott and Church encodings coincide on all enumerated datatypes such as the boolean datatype because each constructor is a constant no parameters Concerning the practicality of using either the Church or Scott encoding for programming there is a symmetric trade off 5 Church encoded numerals support a constant time addition operation and have no better than a linear time predecessor operation Scott encoded numerals support a constant time predecessor operation and have no better than a linear time addition operation Type definitions editChurch encoded data and operations on them are typable in system F as are Scott encoded data and operations However the encoding is significantly more complicated 6 The type of the Scott encoding of the natural numbers is the positive recursive type m X R R X R R displaystyle mu X forall R R to X to R to R nbsp Full recursive types are not part of System F but positive recursive types are expressible in System F via the encoding m X G X X G X X X displaystyle mu X G X forall X G X to X to X nbsp Combining these two facts yields the System F type of the Scott encoding X R R X R R X X displaystyle forall X forall R R to X to R to R to X to X nbsp This can be contrasted with the type of the Church encoding X X X X X displaystyle forall X X to X to X to X nbsp The Church encoding is a second order type but the Scott encoding is fourth order See also editChurch encoding System F Lambda cubeNotes edit Scott Dana 1968 1962 A system of functional abstraction Lectures delivered at University of California Berkeley Curry Haskell 1972 Combinatorial Logic Volume II North Holland Publishing Company ISBN 0 7204 2208 6 Parigot Michel 1988 Programming with proofs A second order type theory In H Ganzinger ed European Symposium on Programming ESOP 88 2nd European Symposium on Programming Nancy France March 21 24 1988 Lecture Notes in Computer Science Vol 300 Springer pp 145 159 doi 10 1007 3 540 19027 9 10 ISBN 978 3 540 19027 1 Mogensen Torben 1994 Efficient Self Interpretation in Lambda Calculus Journal of Functional Programming 2 3 345 364 doi 10 1017 S0956796800000423 S2CID 8736707 Parigot Michel 1990 On the representation of data in lambda calculus In Egon Borger Hans Kleine Buning Michael M Richter eds International Workshop on Computer Science Logic CSL 89 3rd Workshop on Computer Science Logic Kaiserslautern FRG October 2 6 1989 Lecture Notes in Computer Science Vol 440 Springer pp 209 321 doi 10 1007 3 540 52753 2 47 ISBN 978 3 540 52753 4 See the note Types for the Scott numerals by Martin Abadi Luca Cardelli and Gordon Plotkin February 18 1993 References editStump A 2009 Directly reflective meta programming Higher Order and Symbolic Computation 22 115 144 Mogensen T AE 1992 Efficient Self Interpretations in lambda Calculus J Funct Program 2 345 363 Retrieved from https en wikipedia org w index php title Mogensen Scott encoding amp oldid 1186952131, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.