-
Notifications
You must be signed in to change notification settings - Fork 3
/
fix.pm
175 lines (133 loc) · 3.19 KB
/
fix.pm
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#
# Copyright (c) 2015 Christian Jaeger, copying@christianjaeger.ch
#
# This is free software, offered under either the same terms as perl 5
# or the terms of the Artistic License version 2 or the terms of the
# MIT License (Expat version). See the file COPYING.md that came
# bundled with this file.
#
=head1 NAME
FP::fix -- recurse with the fix point combinator
=head1 SYNOPSIS
use FP::fix;
sub foo {
my ($z)= @_;
my $local= fix sub {
my ($local, $x, $y)=@_;
$x > 0 ? &$local ($x-1, $x*$y) : $y
};
&$local ($z, 0)
}
=head1 DESCRIPTION
fix takes a function and returns another function that when called
calls the original function and gives it the fix'ed function as first
argument and then the original arguments.
This allows to write self-recursive local functions without having to
deal with the problem of reference cycles that self-referencing
closures would run into.
The example from the synopsis is equivalent to:
use Scalar::Util 'weaken';
sub foo {
my ($z)= @_;
my $local; $local= sub {
my ($x, $y)=@_;
$x > 0 ? &$local ($x-1, $x*$y) : $y
};
my $_local= $local; weaken $local;
&$local ($z, 0)
}
=cut
package FP::fix;
@ISA="Exporter"; require Exporter;
@EXPORT=qw(fix fixn);
@EXPORT_OK=qw();
%EXPORT_TAGS=(all=>[@EXPORT,@EXPORT_OK]);
use strict; use warnings; use warnings FATAL => 'uninitialized';
# Alternative implementations:
# Y combinator
*Y= do {
my $fix0= sub {
my ($fix0, $f)=@_;
sub {
@_=(&$fix0 ($fix0, $f), @_); goto &$f;
}
};
sub ($) {
my ($f)=@_;
&$fix0 ($fix0, $f)
}
};
# Haskell recursive let based implementation:
# XXX move this code to separate file to avoid dependencies
# fix f = let x = f x in x
use FP::TransparentLazy qw(lazy lazyLight);
# this variant is different since it requires $f to be curried
*haskell_curried= sub {
my ($f)= @_;
my $x; $x= &$f(lazy { $x }); # can't use lazyLight here, why?
$x
};
use Chj::TEST;
TEST {
my $f= haskell_curried (sub {
my ($self)= @_;
sub {
my ($x)=@_;
$x > 0 ? $x * &$self($x-1) : 1
}
});
[ &$f(0), &$f(3) ]
} [1, 6];
*haskell_uncurried= sub {
my ($f)= @_;
my $fc= sub {
my ($fc)=@_;
sub {
unshift @_, $fc; goto &$f;
};
};
my $x; $x= &$fc(lazy { $x }); # can't use lazyLight here, why?
$x
};
# indirectly self-referencing through package variable
*rec=
sub ($) {
my ($f)=@_;
sub {
#@_=(fix ($f), @_); goto &$f;
unshift @_, fix ($f); goto &$f;
}
};
# directly locally self-referencing
use Scalar::Util 'weaken';
*weakcycle=
sub ($) {
my ($f)=@_;
my $f2; $f2= sub {
unshift @_, $f2; goto &$f
};
my $f2_=$f2; weaken $f2; $f2_
};
# choose implementation:
sub fix ($);
*fix= *weakcycle;
# n-ary version:
sub fixn {
my (@f)=@_;
my @ff;
for (my $i=0; $i<@f; $i++) {
my $f= $f[$i];
$ff[$i]= sub {
unshift @_, @ff; goto &$f;
}
}
my @ff_= @ff;
# weaken $_ for @ff;
# ^ XXX: releases too early, same issue as
# mentioned in `intro/more_tailcalls`
wantarray ? @ff_ : do {
@ff==1 or die "fixn: got multiple arguments, but scalar context";
$ff_[0]
}
}
1