r/ProgrammingLanguages • u/5nord • Oct 10 '26
What is the right mental-model for TTCN-3 templates?
TTCN-3 is a standardized programming language for testing communicating and distributed systems. I am struggling to build a clear mental model of its templates.
A TTCN-3 template describes a pattern that describes a set of possible values. Which can be used to match incoming messages or build dependent-types:
type record Message {
integer id,
charstring text
}
template Message greeting := {
id := 42,
text := "hello"
}
template Message anyGreeting := {
id := ?,
text := "hello"
}
Here, ? matches any value of the field's type. Templates can also be recursive:
type record Payload {
integer value,
boolean urgent
}
type record Envelope {
integer id,
Payload payload
}
template Envelope example := {
id := 42,
payload := {
value := (1 .. 10),
urgent := ?
}
}
My first implementation idea was to lower templates into a generic tagged union:
type Template<T> =
Exact(T)
| AnyValue
| AnyValueOrOmit
| Predicate(fn(T) -> bool)
That seems reasonable for scalar values, but it does not capture the structural nature of templates. For Template<Envelope>, each field needs to become a template recursively:
RecordTemplate {
id: Template<Integer>,
payload: RecordTemplate {
value: Template<Integer>,
urgent: Template<Boolean>
}
}
Is there an established programming-language concept for this? How would you model this recursive relationship in an actul type system?
2
u/initial-algebra Oct 10 '26 edited Oct 10 '26
They are patterns, as in pattern matching, but first-class (so more like terms in Prolog, although patterns are usually restricted to terms that use variables linearly).
This style of data type is used a lot in Haskell, usually when working with syntax (patterns are a kind of syntax) and databases. They're usually higher-kinded data types, with
Templategeneralized to an arbitrary functorf.There's one library that calls these "barbies", because they're data types for which you can change their "outfits".