raskell-0.0.0.2: RASP-L in Haskell
Safe HaskellSafe-Inferred
LanguageHaskell2010

RaskellLib

Description

This module provides convenience functions built from the core of the RASP-L language.

It is based on Listing 3 of "What Algorithms Can Transformers Learn", https://arxiv.org/abs/2310.16028, by Zhou et al.

Synopsis

Logical Operations

(?) :: [Bool] -> (Sequence, Sequence) -> Sequence Source #

Use a boolean sequence to select between two sequences. Also known in Python RASP-L as "where", see where'.

shiftRight Source #

Arguments

:: Token

Filler Token

-> Int8

Number of positions to shift

-> Sequence

Input Sequence

-> Sequence 

Shift a sequence to the right by a given number of elements, filling the vacated positions with the provided Token.

toBool :: Token -> Bool Source #

Maps tokens onto bools using Python's "truthiness" rules.

mask :: Token -> [Bool] -> Sequence -> Sequence Source #

Masks a Sequence with a boolean sequence, using the provided Token as the mask.

Running Aggregations

cumSum :: [Bool] -> Sequence Source #

Computes the cumulative sum of a boolean sequence.

maximum' :: Sequence -> Sequence Source #

Computes the running maximum of a Sequence.

minimum' :: Sequence -> Sequence Source #

Computes the running minimum of a Sequence.

argmax :: Sequence -> Sequence Source #

Computes the indices of the running maximum values in a Sequence.

argmin :: Sequence -> Sequence Source #

Computes the indices of the running minimum values in a Sequence.

Aggregations with Queries

numPrev :: Sequence -> Queries -> Sequence Source #

Computes the number of previous tokens in a Sequence that are equal to each Token from Queries.

hasSeen :: Sequence -> Queries -> Sequence Source #

Returns 1s where the Token from the Queries has been seen before in the Sequence.

Indexing with Queries

firsts :: Token -> Sequence -> Queries -> Sequence Source #

Finds the first occurrence of each query token in a Sequence.

lasts :: Token -> Sequence -> Queries -> Sequence Source #

Finds the last occurrence of each query token in a Sequence.

indexSelect :: Token -> Sequence -> Sequence -> Sequence Source #

Selects the tokens from a Sequence at the indices provided by another sequence.

Token Comparisons

Sampling

sample Source #

Arguments

:: Token

End of sequence token

-> (Sequence -> Sequence)

RASP-L program to extend the sequence

-> Sequence

Initial/prompt sequence

-> Word8

Number of steps to decode

-> Sequence

Output (including prompt)

Greedily and autoregressively sample the output of a RASP-L program on a sequence.

Compatibility

where' :: [Bool] -> Sequence -> Sequence -> Sequence Source #

Use a boolean sequence to select between two sequences. Provided for compatibility with Listing 3, but with an apostrophe to avoid a name clash with the "where" keyword.

sampleAutoregressive :: Token -> (Sequence -> Sequence) -> Sequence -> Word8 -> Sequence Source #

Greedily and autoregressively sample the output of a RASP-L program on a sequence.

Provided for compatibility with Listing 3.