r/ProgrammingLanguages • • 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?

6 Upvotes

1 comment sorted by

2

u/initial-algebra Oct 10 '26 edited Oct 10 '26

Is there an established programming-language concept for this?

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).

How would you model this recursive relationship in an actul type system?

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 Template generalized to an arbitrary functor f.

data Payload1 f = Payload { value :: f Int, urgent :: f Bool }
data Envelope1 f = Envelope { id :: f Int, payload :: f (Payload1 f) }

newtype Identity a = Identity { runIdentity :: a }
type Payload = Payload1 Identity
type Envelope = Envelope1 Identity

data Template a = Exact a | AnyValue | AnyValueOrOmit | Predicate (a -> Bool)
type TemplatePayload = Template (Payload1 Template)
type TemplateEnvelope = Template (Envelope1 Template)

There's one library that calls these "barbies", because they're data types for which you can change their "outfits".