HaloO,

On Monday, 16. June 2008 10:03:13 Ovid wrote:
> --- TSa <[EMAIL PROTECTED]> wrote:
> ... why do you think that
> > the way to get at the constraint programming paradigm are the subset
> > type definitions?
>
> Because I can't think of any other way to do it :)

So I´ll try to come up with some creative thoughts =8()


> Now it's my turn to say that I can't follow that statement.

I hope the asymmetry in standard Perl 6 ´$x = $y´ is obvious.
By contrast in constraint programming (CP) ala Oz translated to Perl 6
we have

   $x = $y; # 1
   42 = $y; # 2
   say $x;  # 3 (prints 42)

where line 1 just stores the equality of $x and $y in the constraint
store and goes ahead. Line 2 binds 42 to $y directly and by constraint
solving to $x as well. That is what I call symmetric and which causes
fundamental troubles in implanting CP into imperative Perl 6. Thanks, btw
for the ref to the Kaleidoscope paper, it always amazing how many languages
I haven´t heard about ;)

Now we need a way to express constraint enforcement---called CIP in the
paper. My idea is that it naturally belongs to the variable subsystem. I.e.
we need declarations that declare constraints between variables. My first
attempt is simply

    sub move_vertical_line (Line $line, Event $event)
    {
       constrain $start := $line.start.x, $end := $line.end.x to
       {
          always $start == $end; # constraint
          SOLVE # enforcer
          {
              $end = $start; # normal assignment through bound vars
          }
       }
       my Event $motion;
       while ($motion = get_next_event()) ~~ ButtonMotion
       {
          $line.start.x = $motion.x; # $line.end.x is enforced
       }
       if $motion ~~ ButtonUp  # last event application
       {
          $line.start.x = $motion.x; # $line.end.x is enforced
       }
    }

This is the value constraint from the paper. I would call the other
types almost as in the paper: identity, type and structure constraints.
A type constraint comes closest to the subset type constraints that
are not enforced and hopefully side-effect free. So a global definition
might look like

   constrain PosInt of Int to
   {
      always $_ > 0;
      SOLVE { $_ = 1 }
   }

   my PosInt $x;

   $x = -17; # invokes SOLVE block
   say $x; # prints 1
 
The SOLVE closure needs beefing up, like e.g. if you have more than
one constraint it has to topicalize the constraint somehow:

   constrain $x, $y, $z to
   {
       always $x + $y == $z;
       SOLVE
       {
          when $x changed { $y = $z - $x; }
          when $y changed { $x = $z - $y; } 
          when $z changed { $x = $Z / 2; } # calls first when
       }
   }


Comments?
-- 
"The unavoidable price of reliability is simplicity" -- C.A.R. Hoare
"Simplicity does not precede complexity, but follows it." -- A.J. Perlis
1 + 2 + 3 + 4 + ... = -1/12  -- Srinivasa Ramanujan

Reply via email to