r/ProgrammingLanguages • • 25d ago

Discussion Closures in PostScript

It appears that r/PostScript is truly dead. But I just have to talk about this PostScript stuff I've been playing with.

So, a closure is of course a function plus an environment containing the bindings of its free variables. PostScript has all that! A procedure is just an array with the executable flag set, so it can carry its own storage. It doesn't even strictly need variables because you can embed a circular reference so a procedure can access its own array as storage.

But then you can't print them out. If the procedure contains a circular reference to itself, then calling == or pstack will give you an execstackoverflow error.

So, instead we can pack the variables in a dictionary. This example generates a pseudorandom number generator using an environment dictionary.

%!
/start { 1 dict begin } def
/args { { exch def } forall } def
/clone { dup xcheck exch dup length array copy exch {cvx} if } def
/prng { {seed} start args
  /env currentdict def
  /func ({//env/seed get
          dup -7 bitshift xor
          dup 9 bitshift xor
          dup -13 bitshift xor
          //env/seed 2 index put}) cvx exec def
  /func load end
} def

Poof! a closure. seed prng generates a generator starting from that seed. You can just straight up make a copy of it at any point to have 2 synchronized generators if you wanted that. You can even see the environment and how it "captures" arguments (collect them in a dict, duh. then save the dict). It all seems so simple now. But I don't recall seeing anybody else do it.

9 Upvotes

5 comments sorted by

View all comments

1

u/Reasonable-Pay-8771 25d ago

Here's another example, in the more dangerous style of just using the array itself as storage. It takes a string and produces a stream function that yields one white-space delimited word at a time.

%!
/start { 1 dict begin } def
/args { { exch def } forall } def
/amap { % array proc . array'  % in-place map mutates the array
  exch 0 1 2 index length 1 sub { % p a i
    3 copy get exch exec % p a i p(a[i])
    3 copy put pop pop % p a'
  } for exch pop % a'
} def
/selfie { % proc . proc'  % replace occurrences of SELF with proc itself
  mark 1 index
  { dup /SELF eq {pop counttomark 1 add index} if }
  amap pop pop
} def
/by-word { % string . stream
  {src} start args
  ({SELF //src ( ) search {
      %discard space  save prefix  store remainder
      exch pop exch 3 2 roll 1 3 2 roll put
    }{
      exch 1 () put  % store remainder
    } ifelse
  }) cvx exec selfie end
} def
/push-back { % stream str . stream
  {str stream} start args
  /stream-copy /stream load dup length array copy cvx def
  /stream load 0
  ({//str 0 pop //stream 0 //stream-copy putinterval}) cvx exec
  putinterval
} def

Way too fiddly. And of course you can't print them. But it kinda works. Since the by-word stream is exactly 7 elements long, we can copy a new 7-element-long procedure over the top of it and then copy the old one back as the last step.