interactive-lp
interactive-lp copied to clipboard
Support for string literals and a 'write' builtin predicate
I'm working on generative storytelling and I needed to output text from Ceptre because it was to cumbersome to parse the output with a different program. This introduces side effects and the need for string literals.
When declaring a predicate with the WRITE
builtin it is checked to be an action predicate having one or more arguments of any type. Multiple write predicates can be declared, this is needed to write values of different types. Action predicates are not really used in the code, I don't know what was the intention behind them but the fact they can only appear on the RHS of a rule means side effect to me. When a rule a * b -o write a b
is applied, it will print ab
with no newline and the write a b
atom won't be added to the context as a special case of write. Maybe this case should be extended to all action predicates but for now only write has side effect. Or maybe it should be added to the context except when some special operator is used to flag an atom not to be added.
Next, to be able to print any text without having to declare all words as predicates, string literals were needed.
There was a string type in the core engine (SLit
terms) but unlike integers it wasn't implemented in the parser and in the typechecker so there was no way to obtain a string term.
Finally the --quiet
CLI flag was added so only the output of write is displayed.
Example:
nat : type. #builtin NAT nat.
str : type. #builtin STRING str.
write/str str : action. #builtin WRITE write/str.
write/nat nat : action. #builtin WRITE write/nat.
write/sentence str str str nat str : action. #builtin WRITE write/sentence.
go str nat : pred.
stage rules {
print1: go Name Age -o write/sentence "My name is " Name ", I am " Age " years old.\n".
print2: go Name Age -o
write/str "My name is "
* write/str Name
* write/str ", I am "
* write/nat Age
* write/str " years old.\n".
}
context init = { go "Bob" 25 }.
#trace _ rules init.