Thursday, September 02, 2004

lab 1 - postdate

NAME

lab 1 - implement ideas from postdate in inferno sh. write shell functions that are polymorphic along the valid timeline.

SETUP

Inferno 4th edition release 20040830. Using sh(1), and tickfs(1) and associated commands, rng(1), taq (1).

DESCRIPTION

Sh has command blocks which can be passed as params to commands, and executed. I want to exploit this to see if I can implement much of the flavor of Postdate in the shell. I already have tickfs in Inferno which supports bi-temporal binary relations. So the value for a tickfs entry could be the sh command block.

Create a new relation with entry of command block

% mount -c {tickfs} /n/tick
% touch /n/tick/sh.bt
% echo ${quote i . add {fc $1 $2 +}} > /n/tick/sh.bt
% rng , |taq -1rm /n/tick/sh.bt
1094173750 add '{fc $1 $2 +}'

I want shell function to return the command block from tickfs. It has to be a substitution function to keep it as a command block, because echo causes it to be a string.

% subfn pd {
 f = $1
 (a b c) = `{echo `{date -n} $f . |taq /n/tick/sh.bt}
 result = ${unquote $"c}
}
% echo ${pd add}
{fc $1 $2 +}

I can now call the function add; the code comes from a lookup in tickfs

% ${pd add} 1 1
2

I'll create a function I want to vary along the timeline. I can also define variables using the same method. I'll create a substition function that uses rng to give the date in epoch format. And then use that in ${pd} to select the effective fuction or variable

% subfn rng {
 r = $1
 (a b c) = `{rng $r}
 result =  $b
}

% date ${rng 20040901}
Wed Sep 01 00:00:00 EDT 2004

% subfn pd{
 (f r) = $*
 (a b c) =  `{echo ${rng $r} $f .  | taq /n/tick/sh.bt}
 result = ${unquote $"c}
}

Pdfn defines new functions in tickfs. It takes args rng name {cmd}

% fn pdfn {
 (r args) = $*
 echo ${quote i ${rng $r} $args} > /n/tick/sh.bt
}
% pdfn 20040101 rate {fc $1 2 x}
% pdfn 20040601 rate {fc $1 4 x}
% pdfn . rate {fc $1 8 x}
% pdfn 20040101 a 10

Now call these functions at different times

% ${pd rate 0401} 1
2
% ${pd rate 0701} 1
4
% ${pd rate 1201} ${pd a 1201}
80

In Postdate I had a dictionary stack. In Inferno the /n/tdb/sh.bt, or other .bt, file is the dictionary. I can push and pop easily from list in sh.

% pdstk = (n/tick/sh.bt)
% fn pdpush { pdstk = ($1 $pdstk)}
% fn pdpop {pdstk = ${tl $pdstk}}

I have to redefine pdfn and pd to use ${hd $pdstk} instead of hardcoded /n/tick/sh.bt.

The usual mode of processing tickfs result sets is in a pipeline. If I define a temporal package as a tickfs file with a main sh command block, the pdpkg command will call main on one triad at a time from stdin. It doesn't need to convert from YYYYMMDD format to epoch because triads always come in epoch format. We'll get around that by just defining another pd function, say epd, that takes seconds since the epoch.

% subfn epd{
 (f r) = $*
 (a b c) =  `{echo $r $f .  | taq ${hd $pdstk}}
 result = ${unquote $"c}
}

% fn pdpkg {
 pdpush $1
 getlines {
  (t k v) = ${unquote $line}
  ${epd main $t} $k $v
 }
 pdpop
}

% pdfn 20040201 main {fc $2 10 x}
% touch /n/tick/tick.bt
% echo i . a 1 > /n/tick/tick.bt
% echo i . b 2 > /n/tick/tick.bt
% echo i . c 3 > /n/tick/tick.bt
% rng , |taq -1rm /n/tick/tick.bt |pdpkg /n/tick/sh.bt
10
20
30

CONCLUSION

I have created shell functions and variables that can vary along the valid time line. I created packages, blocks of temporal shell code, that can be applied to tickfs result set. It is more featureful that Postdate, since we have the whole shell and inferno at our disposal. It is slow. I'm not concerned with the perfomance now. I want to find out if there's some interesting functions that I can implement that can vary along the timeline.

Postdate also has the effective valid time stack. We could implement the stack in the same way as pdstk but really the effective time is in the callstack since it is passed as a param to every call of pd.

No comments: