Schrift
Wiki:Tipp zum Debugging: use Data::Dumper; local $Data::Dumper::Useqq = 1; print Dumper \@var;
[thread]11322[/thread]

Gültigkeit von Variablen... oder so etwas. (Seite 4)

Leser: 13


<< |< 1 2 3 4 >| >> 31 Einträge, 4 Seiten
murphy
 2008-02-23 22:17
#106307 #106307
User since
2004-07-19
1776 Artikel
HausmeisterIn
[Homepage]
user image
So, ich habe mal eine kleine CPS-Demonstration gebastelt, die ein Logikrätsel löst:
Code (perl): (dl )
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/perl
#
# A demonstration of continuation passing style. Implements primitives
# for ambivalence and backtracking which are used to solve a logic
# problem.
#
use 5.008;
use strict;
use warnings;


# This package could be used as a standalone module offering CPS
# ambivalence utilities.
package Amb;

# This variable holds the current failure continuation, which is
# called when amb has no more values to return or Amb::assert fails.
# The default value of this variable signals an error, because it
# should really by initialized by Amb::find or Amb::collect.
our $Failure = sub {
    die 'Unexpected ambivalence failure'
};

# The universal backtracking ambivalence operator. Calling amb with an
# empty list of alternatives thus results in immediate invocation of
# the failure continuation while calling amb with several alternatives
# results in a CPS return value selected from that list as determined
# by the further flow of the program.
#
# This is achieved as follows: Given a success continuation and a list
# of alternatives, amb sets up the global failure continuation to
# shift a value off that list unless empty and to invoke the success
# continuation; iff the list is empty, though, the outer failure
# continuation is restored and invoked. amb then invokes the new
# failure continuation immediately.
#
# Params:
#   $Success      = The continuation invoked upon success.
#   @alternatives = The possible values that may be fed to $Success.
#
# Returns (CPS):
#   Any value from @alternatives.
sub some(&@) {
    my ($Success, @alternatives) = @_;
    my $OuterFailure = $Failure;

    $Failure = sub {
        if (@alternatives > 0) {
            @_ = (shift @alternatives); goto &$Success;
        }
        else {
            $Failure = $OuterFailure;
            @_ = (); goto &$Failure;
        }
    };

    @_ = (); goto &$Failure;
}

# The ambivalence assertion operator. Given a success continuation and
# a condition, it invokes the success continuation iff the condition
# is true; otherwise the failure continuation is invoked.
#
# Params:
#   $Success   = The continuation invoked upon success.
#   $condition = The condition to check.
#
# Returns (CPS):
#   nothing.
sub assert(&$) {
    my ($Success, $condition) = @_;

    @_ = ();
    if ($condition) {
        goto &$Success;
    }
    else {
        goto &$Failure;
    }
}

# The single value ambivalence CPS entry point. Sets up initial
# success and failure continuations for use by the CPS method passed
# as the first argument and runs it, collecting its return value.
#
# Params:
#   &proc     = The CPS procedure to call.
#   @defaults = The default set of values to return if the CPS
#               procedure fails.
#
# Returns:
#   The values passed to the success continuation by the given CPS
#   procedure or the set of default values if the CPS procedure
#   invokes the initial failure continuation.
sub find(&@) {
    my ($proc, @defaults) = @_;

    my @results = ();
    local $Failure = sub {
        @results = @defaults;
    };

    my $Success = sub {
        @results = @_;
    };

    $proc->($Success);

    return @results;
}

# The collecting ambivalence CPS entry point. Sets up initial success
# and failure continuations for use by the CPS method passed as the
# argument and runs it, collecting all its possible return values.
#
# Params:
#    &proc = The CPS procedure to call.
#    $wrap = If true, return value sets from the CPS procedure are
#            wrapped into array references before they are
#            collected. If false, return value sets are simply
#            concatenated.
#
# Returns:
#    All possible return value sets from the CPS procedure as array
#    references.
sub collect(&$) {
    my ($proc, $wrap) = @_;

    my $done = 0;
    local $Failure = sub {
        $done = 1;
    };

    my @results = ();
    my $Success = sub {
        if ($wrap) {
            push @results, [ @_ ];
        }
        else {
            push @results, @_;
        }
    };

    $proc->($Success);
    $Failure->() while (!$done);

    return @results;
}

1;


# The following program uses the ambivalence operators and
# Amb::collect to find all possible solutions of the following puzzle:
#
#   The Kalotans are a tribe with a peculiar quirk. Their males always
#   tell the truth. Their females never make two consecutive true
#   statements, or two consecutive untrue statements.
#
#   An anthropologist (let's call him Worf) has begun to study
#   them. Worf does not yet know the Kalotan language. One day, he
#   meets a Kalotan (heterosexual) couple and their child Kibi. Worf
#   asks Kibi: "Are you a boy?" Kibi answers in Kalotan, which of
#   course Worf doesn't understand.
#
#   Worf turns to the parents (who know English) for explanation. One
#   of them says: "Kibi said: 'I am a boy.'" The other adds: "Kibi is
#   a girl. Kibi lied."
#
#   Solve for the sex of the parents and Kibi.
package main;

my @mf = qw/male female/;

my @solutions = Amb::collect {
    my ($Success) = @_;

    Amb::some {
        my ($parent1) = @_;
        Amb::some {
            my ($parent2) = @_;
            Amb::some {
                my ($kibi) = @_;
                Amb::some {
                    my ($kibi_self) = @_;
                    Amb::some {
                        my ($kibi_lied) = @_;
                        Amb::assert {
                            my $KibiSuccess = sub {
                                my $Parent1Success = sub {
                                    @_ = ({
                                        parent1 => $parent1,
                                        parent2 => $parent2,
                                        kibi => $kibi
                                    });
                                    goto &$Success;
                                };

                                if ($parent1 eq 'male') {
                                    Amb::assert
                                        \&$Parent1Success,
                                        (($kibi_self eq 'male') &&
                                         ((($kibi eq 'female') && !$kibi_lied) ^
                                          (($kibi eq 'male') && $kibi_lied)))
                                }
                                else {
                                    Amb::assert
                                        \&$Parent1Success,
                                        (($kibi eq 'female') && $kibi_lied)
                                }
                            };

                            if ($kibi_lied) {
                                Amb::assert
                                    \&$KibiSuccess,
                                    ((($kibi_self eq 'male') &&
                                      ($kibi eq 'female')) ^
                                     (($kibi_self eq 'female') &&
                                      ($kibi eq 'male')))
                            }
                            else {
                                Amb::assert
                                    \&$KibiSuccess,
                                    ((($kibi_self eq 'male') &&
                                      ($kibi eq 'male')) ^
                                     (($kibi_self eq 'female') &&
                                      ($kibi eq 'female')))
                            }
                        } ($parent1 ne $parent2);
                    } 1, 0;
                } @mf;
            } @mf;
        } @mf;
    } @mf;
} 0;

print <<EOD foreach (0 .. $#solutions);
Solution $_:
  First parent is  $solutions[$_]->{parent1}
  Second parent is $solutions[$_]->{parent2}
  Kibi is          $solutions[$_]->{kibi}
EOD
When C++ is your hammer, every problem looks like your thumb.
<< |< 1 2 3 4 >| >> 31 Einträge, 4 Seiten



View all threads created 2008-02-18 13:44.