Title: | Continuation Passing Style Development |
---|---|
Description: | Provides basic functions for Continuation-Passing Style development. |
Authors: | Matteo Provenzano |
Maintainer: | Matteo Provenzano <[email protected]> |
License: | BSD_3_clause + file LICENSE |
Version: | 0.1 |
Built: | 2024-12-04 07:25:38 UTC |
Source: | CRAN |
It allows to compose two functions of the form f(x, ret)
and g(x, ret)
returning a function h(x,ret)
which is the composition .
It implements the composition operator of the Continuation category.
The the composition has the following properties:
Associativity:
Unity:
In order for these relations to hold, the function f
and g
must not deal with global mutable states.
compose(f, g)
compose(f, g)
f |
The first function that must be composed |
g |
The first function that must be composed |
Rerturns the composite function of f
and g
The composition is performed from left to right i.e. such that the first function executed is f
.
Matteo Provenzano
http://www.alephdue.com
# Example 1 # define an arrow in the Continuation category. # this function applies the continuation to the # increment of its argument and then decrements it. one <- function(x, ret) { return(ret(x+1) - 1) } # define another arrow in the Continuation category. # this function doubles its argument. two <- function(x, ret) { return(ret(2*x)) } # create the composition # this is exactly the same as one %.% two composite <- compose(one, two) # build the function (forget the continuation) execute1 <- forget(composite) execute1(1) # returns 3 # Example 2 # compose the function further to loop over an array of elements # lapply and sapply are already arrow in the Continuation category loop <- compose(lapply, composite) # build the function execute2 <- forget(loop) execute2(1:10)
# Example 1 # define an arrow in the Continuation category. # this function applies the continuation to the # increment of its argument and then decrements it. one <- function(x, ret) { return(ret(x+1) - 1) } # define another arrow in the Continuation category. # this function doubles its argument. two <- function(x, ret) { return(ret(2*x)) } # create the composition # this is exactly the same as one %.% two composite <- compose(one, two) # build the function (forget the continuation) execute1 <- forget(composite) execute1(1) # returns 3 # Example 2 # compose the function further to loop over an array of elements # lapply and sapply are already arrow in the Continuation category loop <- compose(lapply, composite) # build the function execute2 <- forget(loop) execute2(1:10)
do
allows to specify the list of function directly as its arguments.
It return a function which is the composition of every argument with the continuation already forgotten.
do(...)
do(...)
... |
The functions that must be composed together. |
A function of the type g(x)
which can be directly used on the input.
Matteo Provenzano
http://www.alephdue.com
# define a function that doubles its argument times.two <- function(x, ret) { ret(x*2) } # define a function that loops over a list of list and double every element loop <- do(lapply, lapply, times.two) #returns list(list(2, 4, 6), list(8,10,12)) loop(list(list(1,2,3),list(4,5,6)))
# define a function that doubles its argument times.two <- function(x, ret) { ret(x*2) } # define a function that loops over a list of list and double every element loop <- do(lapply, lapply, times.two) #returns list(list(2, 4, 6), list(8,10,12)) loop(list(list(1,2,3),list(4,5,6)))
This function takes a function of the form f(x, ret)
and forgets the ret
part returning a function of the form g(x)
.
forget(f)
forget(f)
f |
a function of the form |
a function of the form f(x)
.
Matteo Provenzano
http://www.alephdue.com
# forget the FUN part in lapply to.list <- forget(lapply) # returns the list of the natural numbers from 1 to 10 to.list(1:10)
# forget the FUN part in lapply to.list <- forget(lapply) # returns the list of the natural numbers from 1 to 10 to.list(1:10)
The identity arrow for the Continuation category for which holds: f %.% identity2 = f = identity2 %.% f
identity2(x, ret)
identity2(x, ret)
x |
The value on which the function operates |
ret |
The following computation |
This function always returns the original arrow.
Matteo Provenzano
http://www.alephdue.com
Creates the monoid binary operator for a monoid in the Continuation category.
monoid(op)
monoid(op)
op |
The binary operator to be be insert in the monoid (multiplication). |
It returns a function of the type h(f, g)
where f
and g
must be elements of the monoid and objects in the Continuation category. The function h
will return a function of the type t(x, ret)
which can be used in the Continuation category.
The developer must make sure that the function f
and g
are elements of a monoid and of the Continuation category. The developer must also ensure that the operator op
is the monoid's binary operator.
Matteo Provenzano
http://www.alephdue.com
https://en.wikipedia.org/wiki/Monoid_(category_theory)
# A list is a monoid replicate.10 <- function(x, ret) { ret(rep(x, 10)) } # concatenation is the binary operator for the list monoid # the empty list is the unit `%et%` <- monoid(c) replicate.20 <- do(replicate.10 %et% replicate.10) # returns a list of 20 "a"s replicate.20("a")
# A list is a monoid replicate.10 <- function(x, ret) { ret(rep(x, 10)) } # concatenation is the binary operator for the list monoid # the empty list is the unit `%et%` <- monoid(c) replicate.20 <- do(replicate.10 %et% replicate.10) # returns a list of 20 "a"s replicate.20("a")
It applies the compose
opertor recursively on all the elements of the list provided as argument
path(fs)
path(fs)
fs |
The list of the functions that must be composed together (e.g: list(f1, f2, f3, ...)). |
A function of the type g(x, ret)
result of the pairwise composition of each element in the list.
Matteo Provenzano
http://www.alephdue.com
# define a function that doubles its argument times.two <- function(x, ret) { ret(x*2) } # define a function that loops over a list of list and double every element loop <- forget(path(list(lapply, lapply, times.two))) #returns list(list(2, 4, 6), list(8,10,12)) loop(list(list(1,2,3),list(4,5,6)))
# define a function that doubles its argument times.two <- function(x, ret) { ret(x*2) } # define a function that loops over a list of list and double every element loop <- forget(path(list(lapply, lapply, times.two))) #returns list(list(2, 4, 6), list(8,10,12)) loop(list(list(1,2,3),list(4,5,6)))