From: Mike Rotondo Date: 2006-07-17T04:01:50+09:00 Subject: Re: [QUIZ] Negative Sleep (#87) --Apple-Mail-2-823126856 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8; delsp=yes; format=flowed "Let's suppose there existed a sleep which accepted negative =20 arguments. Would it actually imply time travel?" ...Well of course! I created the SuperSleep class, which when given a =20= negative argument jumps backwards into the history of execution by =20 the amount of time specified and resumes execution from that point. So, the program that looks like this: ------------------------------------------------------------------------=20= --- tta =3D TwistyTimeApp.new([Computation.new("x =3D 0 "), Computation.new("p x "), Computation.new("x +=3D = 1 "), Computation.new("p x "), Computation.new("x +=3D = 1 "), Computation.new("p x "), Computation.new("x +=3D = 1 "), Computation.new("p x "), SuperSleep.new(-0.015), SuperSleep.new(-0.015)]) tta.run ------------------------------------------------------------------------=20= --- Would have output that might look like this (comments added for =20 clarification of flow): =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 0 1 2 3 #reaches the first SuperSleep, jumps back .015 seconds 4 #gets to the third Computation, starts flowing again 5 6 #gets to the second SuperSleep, jumps back .015 seconds 0 #reaches the first Computation (resetting x), and starts flowing again 1 2 3 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Wacky, huh?! Caveats: - When a SuperSleep is executed (possibly jumping back in time) it =20 is also destroyed, so that no infinite loops are created (jump back, =20 flow forward, jump back, etc. etc.) - The time at which each Computation is executed is set once, the =20 first time it happens. This might change later, if I want to make =20 things really confusing. - SuperSleeps with positive delta just sleep for that long, instead =20= of jumping forward into the future. This is definitely going to =20 change, so that they actually jump forward in the execution by the =20 amount of time specified, skipping certain Computations :). - If a Negative Sleep goes past the beginning of the program, it =20 simply starts over without accounting for the extra time by which it =20 surpassed the beginning of execution. THE FUTURE: - I'm hoping to make this program continuations/generator based. =20 Expect that later tonight I guess. - Jumping into the future with SuperSleeps given positive numbers, =20 instead of just sleeping! :) Attached is my code, check it out and let me know what you think! =20 (Run it a few times to get an idea of how the output can change, and =20 adjust the sleeps for the speed of your computer :P) =EF=BF=BC= --Apple-Mail-2-823126856 Content-Type: multipart/mixed; boundary=Apple-Mail-3-823126856 --Apple-Mail-3-823126856 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1 "Let's suppose there existed a = sleep which accepted negative arguments. Would it actually imply time = travel?"

...Well = of course! I created the SuperSleep class, which when given a negative = argument jumps backwards into the history of execution by the amount of = time specified and resumes execution from that point.

So, the program that looks = like = this:
----------------------------------------------------------= -----------------
tta =3D = TwistyTimeApp.new([Computation.new("x =3D 0 "),
=A0 =A0 =A0=A0 = =A0=A0 =A0=A0 =A0=A0 =A0=A0 =A0=A0 =A0=A0 =A0 = Computation.new("p x "),
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 = Computation.new("x +=3D 1 "),
=A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 = Computation.new("p x "),
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 = Computation.new("x +=3D 1 "),
=A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 = Computation.new("p x "),
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 = Computation.new("x +=3D 1 "),
=A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 = Computation.new("p x "),
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 = SuperSleep.new(-0.015),
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 = SuperSleep.new(-0.015)])
tta.run
--------------= -------------------------------------------------------------
Wo= uld have output that might look like this (comments added for = clarification of = flow):
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
0
1
2
3 #reaches the first SuperSleep, jumps back = .015 seconds
4 #gets to the third Computation, starts = flowing again
5
6 #gets to the = second SuperSleep, jumps back .015 seconds
0 #reaches the = first Computation (resetting x), and starts flowing = again
1
2
3
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D

Wacky, huh?!

Caveats:
=A0- = When a SuperSleep is executed (possibly jumping back in time) it is also = destroyed, so that no infinite loops are created (jump back, flow = forward, jump back, etc. etc.)
=A0- The time at which each = Computation is executed is set once, the first time it happens. This = might change later, if I want to make things really = confusing.
=A0- SuperSleeps with positive delta just sleep for = that long, instead of jumping forward into the future. This is = definitely going to change, so that they actually jump forward in the = execution by the amount of time specified, skipping certain Computations = :).
=A0- If a Negative Sleep goes past the beginning of the = program, it simply starts over without accounting for the extra time by = which it surpassed the beginning of execution.

THE FUTURE:
=A0- = I'm hoping to make this program continuations/generator based. Expect = that later tonight I guess.
=A0- Jumping into the future with = SuperSleeps given positive numbers, instead of just sleeping! = :)

Attached = is my code, check it out and let me know what you think! (Run it a few = times to get an idea of how the output can change, and adjust the sleeps = for the speed of your computer = :P)
= --Apple-Mail-3-823126856 Content-Transfer-Encoding: 7bit Content-Type: text/x-ruby-script; x-unix-mode=0644; name=NegativeSleep.rb Content-Disposition: attachment; filename=NegativeSleep.rb class Computation attr_accessor :statement def initialize(statement) @statement = statement end def print p @statement end def evaluate() eval(@statement) end end class SuperSleep attr_accessor :delta def initialize(delta) @delta = delta end end class TwistyTimeApp def initialize(computations_and_sleeps) @program = computations_and_sleeps @exec_times = [] end def run @current_index = 0 @previous_time = Time.now.to_f @current_time = @previous_time while @current_index < @program.size @current_time += Time.now.to_f - @previous_time @previous_time = Time.now.to_f statement_object = @program[@current_index] if statement_object.class == Computation eval statement_object.statement #p "ci = #{@current_index}" @exec_times[@current_index] = @exec_times[@current_index] || @current_time @current_index += 1 elsif statement_object.class == SuperSleep #jump back and delete the SuperSleep statement so it only happens once #otherwise you get infinite loops #p "jumping #{statement_object.delta}" @current_time += statement_object.delta if statement_object.delta < 0 while @exec_times[@current_index-1] > @current_time @current_index -= 1 break if @current_index == 0 end @program.delete(statement_object) else sleep(statement_object.delta) @program.delete(statement_object) end end end #p @exec_times.inspect end end tta = TwistyTimeApp.new([Computation.new("p 'a'"), Computation.new("p 'b'"), Computation.new("p 'c'"), Computation.new("p 'd'"), Computation.new("p 'e'"), Computation.new("p 'f'"), Computation.new("p 'g'"), Computation.new("p 'h'"), Computation.new("p 'i'"), Computation.new("p 'j'"), Computation.new("p 'k'"), SuperSleep.new(-0.02)]) tta.run tta2 = TwistyTimeApp.new([Computation.new("x = 0 "), Computation.new("p x "), Computation.new("x += 1 "), Computation.new("p x "), Computation.new("x += 1 "), Computation.new("p x "), Computation.new("x += 1 "), Computation.new("p x "), SuperSleep.new(-0.015), SuperSleep.new(-0.015)]) tta2.run --Apple-Mail-3-823126856 Content-Transfer-Encoding: 7bit Content-Type: text/html; charset=US-ASCII
--Apple-Mail-3-823126856-- --Apple-Mail-2-823126856--