Welcome to mirror list, hosted at ThFree Co, Russian Federation.

function_arrow.py « arrows « core « pypeline « src - github.com/ianj-als/pypeline.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 17e282d8f9541b85242c367aa9dccffa6e64b637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#
# Copyright Applied Language Solutions 2012
#
# This file is part of Pypeline.
#
# Pypeline is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pypeline is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pypeline.  If not, see <http://www.gnu.org/licenses/>.
#
import types

from pypeline.core.arrows.arrow import Arrow


#
# An implementation of Arrows with functions. The composition
# operator (>>>) is implemented as the right shift
# operator (>>). Tuple input parallel computation (***) is the
# Python (**) operator. And Parallel computation with a scalar
# input (&&&) is the bitwise and operator (&).
# WATCH OUT FOR OPERATOR PRECEDENCE!!!
#
# ** is higher than >> is higher than than &
#
#
# See:
#  Publications:
#    http://www.cse.chalmers.se/~rjmh/Papers/arrows.pdf
#    http://www.soi.city.ac.uk/~ross/papers/notation.pdf
#
#  Tutorials:
#    http://www.haskell.org/arrows/
#    http://www.haskell.org/haskellwiki/Arrow_tutorial
#    http://en.wikibooks.org/wiki/Haskell/Understanding_arrows
#
class FunctionArrow(Arrow):
    def __init__(self, func):
        if type(func) is not types.FunctionType and \
           type(func) is not types.MethodType:
            raise ValueError("Must be a function or method")

        Arrow.__init__(self)
        self._func = func

    #
    # (>>>) composition
    #
    #       +---------+           +---------+
    # b --->+--- f ---+--- c ---->+--- g ---+---> d
    #       +---------+           +---------+
    def __rshift__(self, other):
        if not isinstance(other, Arrow):
            raise ValueError("Must be an arrow")

        return FunctionArrow(lambda b: other._func(self._func(b)))

    #
    # (***) parallel computation with input of type tuple
    #
    #       +---------+           +---------+
    # b --->+--- f ---+---> c --->+---------+---> c
    #       |         |           |         |
    # d --->+---------+---> d --->+--- g ---+---> e
    #       +---------+           +---------+
    def __pow__(self, other):
        if not isinstance(other, Arrow):
            raise ValueError("Must be an arrow")

        return self.first() >> other.second()

    #
    # (&&&) parallel computation with scalar input
    #
    #       +---------+           +---------+           +---------+
    #       |    /----+---> b --->+--- f ---+---> c --->+---------+---> c
    # b --->+----     |           |         |           |         |
    #       |    \----+---> b --->+---------+---> b --->+--- g ---+---> d
    #       +---------+           +---------+           +---------+
    def __and__(self, other):
        if not isinstance(other, Arrow):
            raise ValueError("Must be an arrow")

        return split() >> self.first() >> other.second()

    #
    # First
    #
    #       +---------+
    # b --->+--- f ---+---> c
    #       |         |
    # d --->+---------+---> d
    #       +---------+
    def first(self):
        def composition(a_tuple):
            if type(a_tuple) is not types.TupleType:
                raise ValueError("Must be a tuple type")

            return (self._func(a_tuple[0]), a_tuple[1])

        return FunctionArrow(composition)

    #
    # Second
    #
    #       +---------+
    # d --->+---------+---> d
    #       |         |
    # b --->+--- f ---+---> c
    #       +---------+
    def second(self):
        def composition(a_tuple):
            if type(a_tuple) is not types.TupleType:
                raise ValueError("Must be a tuple type")

            return (a_tuple[0], self._func(a_tuple[1]))

        return FunctionArrow(composition)

    #
    # Apply a value, of appropriate type, to the arrow
    #
    def __call__(self, value):
        return self._func(value)


#
# Split
#
#       +-------+
#       |   /---+---> b
# b --->+---    |
#       |   \---+---> b
#       +-------+
def split():
    return FunctionArrow(lambda b: (b, b))


#
# Unsplit
#
#       +---------+
# b --->+---\     |
#       |  (op)---+---> d
# c --->+---/     |
#       +---------+
def unsplit(op_func):
    return FunctionArrow(lambda t: op_func(t[0], t[1]))