TLghc is a proof-of-concept interpreter of the TransLucid language, written in Haskell. TransLucid is the language being developed to explore Cartesian Programming. Presentation and discussion of these ideas can be found at TransLucid and Cartesian Programming.
The interpreter is a standalone Haskell stack package, which can only be used if you have stack installed. If you do not have stack installed, follow the Stack Install/upgrade Instructions.
Once you have stack installed, git clone the repository, enter the directory, and type
stack build
to build a binary called TLghc-exe. See Usage below.
To run the TLghc test suite, type
stack test
The basic interface for TLghc-exe is as follows:
stack exec TLghc-exe file1 file2 ... fileN
will concatenate the N files file1
, ..., fileN
,
then parse them as a TransLucid program, then execute that program,
and output the result of the execution.
If N = 0, i.e., no files are specified, then TLghc-exe will read from the standard input.
There is also the possibility of adding options to TLghc-exe:
stack exec TLghc-exe -- options file1 file2 ... fileN
There are currently two options:
-
The option
--parseOnly
ensures that the input parses correctly, but does not execute it.
-
The option
--printAST
prints out the abstract syntax tree of the parsed input. If there are syntax errors in the input, then this option can be used to figure out where the syntax errors were detected by the parser.
A TransLucid program consists of a set of declarations for dimensions, variables and functions, and a set of demands for computation. These can be interleaved as the programmer wishes.
The dim
keyword declares a dimension identifier.
-
In the declaration
dim d <- expr
dimension
d
is declared, whose initial ordinate is set to be the result of evaluating expressionexpr
.
The var
keyword declares a variable identifier.
-
In the declaration
var x = expr
variable
x
is declared to be equal to expressionexpr
.
The fun
keyword declares a function identifier.
-
In the declaration
fun x.d1...dm = expr
function
x
, indexed by the m dimension formal parametersd1
throughdm
, is declared to be equal to expressionexpr
. -
In the declaration
fun x(x1,...,xn) = expr
function
x
is declared to take the n formal parametersx1
throughxn
and to haveexpr
as body. -
In the declaration
fun x.d1...dm(x1,...,xn) = expr
function
x
, indexed by the m dimension parametersd1
throughdm
, is declared to take the n formal parametersx1
throughxn
and to haveexpr
as body.
The evalExpr
keyword makes a demand for computation.
-
In the demand
evalExpr expr @ [d1 <- min1..max1, ..., dm <-minm, maxm]
expression expr
is to be evaluated in each of the contexts
in the hypercube defined by setting, respectively,
the ordinate for dimension i
to be between mini
and maxi
.
TransLucid is a functional language with where
clauses, that allows
the manipulation of a multidimensional context. The syntactic elements are:
-
variables
x
defined in one of thevar
declarations
-
constants
- integers
- characters in single quotes
- strings in double quotes
- Booleans
true
andfalse
-
unary operators
!
(Boolean negation)-
(arithmetic negation)
-
binary operators
*
,/
,%
(mod),%%
(rem)+
,-
<
,<=
,>=
,>
,==
,/=
&&
,||
-
conditional operator
if expr then expr else expr
-
arrays (1 to 5 dimensions)
[[(d,min,max)], [val, ..., val]]
(1 dimension,max-min+1
elements)[[(d1,min1,max1),(d2,min2,max2)], [val, ..., val]]
(2 dimensions)- ...
-
context query
#d
queries the current context for the ordinate associated with dimensiond
.
-
context perturbation
expr [d1 <- expr1, ..., dm <- exprm]
perturbs the current context by changing, respectively, the ordinate for dimensiondi
to the result of evaluatingexpri
.
-
lambda abstraction
fn.d1...dm(x1,...,xn) -> expr
defines a dimensionally abstract function.
-
function application
(expr).d1...dm(expr1,...,exprn)
evaluatesexpr
to produce a dimensionally abstract function, then applies it to dimension actual parametersd1
throughdm
, and actual parametersexpr
throughexprn
.
-
local dimension and variable declaration
expr where declarations end
, wheredeclarations
is a sequence ofdim
andvar
declarations, allows the declaration of identifiers local to expressionexpr
.
The file examples/simpleExamples
contains the following declarations:
fun index2.d1.d2 = #d1 + #d2
fun prev.d(X) = X [d <- #d - 1]
fun next.d(X) = X [d <- #d + 1]
fun fby.d(X,Y) = if #d < 1 then X else prev.d(Y)
fun ybf.d(X,Y) = if #d > -1 then Y else next.d(X)
dim d <- 0
dim d0 <- 0
dim d1 <- 0
var X = 0 fby.d X+1
var Y = Y-1 ybf.d 0
evalExpr X @ [d <- -2 .. 2]
evalExpr Y @ [d <- -2 .. 2]
evalExpr index2.d0.d1 @ [d0 <- 1 .. 3, d1 <- 2 .. 4]
The expected output from typing
stack exec TLghc-exe examples/simpleExamples
is as follows:
TLvar "X"
d <- -2: TLint 0
-1: TLint 0
0: TLint 0
1: TLint 1
2: TLint 2
TLvar "Y"
d <- -2: TLint -2
-1: TLint -1
0: TLint 0
1: TLint 0
2: TLint 0
TLapply (TLvar "index2") ["d0","d1"] []
d0 <- 1, d1 <- 2: TLint 3
3: TLint 4
4: TLint 5
2, d1 <- 2: TLint 4
3: TLint 5
4: TLint 6
3, d1 <- 2: TLint 5
3: TLint 6
4: TLint 7
The file examples/standardFunctions
contains the declarations for
a number of commonly used TransLucid functions.
The uses for these functions can be found in the test/tl
directory.
The expected output for, say, running
stack exec TLghc-exe test/tl/factorial.tl
can be found in file
test/out/factorial.out
The expected output for, say, running
stack exec TLghc-exe -- --printAST --parseOnly test/tl/factorial.tl
can be found in file
test/ast/factorial.ast