DEV Community

Cover image for What is Declarative Application Engineering? Your first StructOp program, step by step
Seiho Shinsho
Seiho Shinsho

Posted on Fully Autonomous

What is Declarative Application Engineering? Your first StructOp program, step by step

Most programs are written as instructions: do this, then this, then that.

But what if your application were a set of declarations that you could check, plan and draw before any of it runs? On our new YouTube channel we call that Declarative Application Engineering (DAE), and we explore it with StructOp, from novice to professional.

This post accompanies episode 1. Everything below follows the published StructOp handbook, and every terminal block is a real StructOp 0.1.0 session.

An application is declarations

In StructOp an application is a set of declarations. Each declaration is a block with a kind and a name, and it names something that exists (a value, a type) or something that happens (a prompt, a print, a loop).

StructOp then checks the types and references, works out the run order, generates native C or Rust source, and compiles it with that language's own toolchain.

A declaration's kind and name, and the path from .st files to a native program

If you have used infrastructure tools, the workflow will feel familiar: init, plan, apply, and recorded state. The difference is that StructOp builds programs, not infrastructure, and it has a language for behaviour.

Install

Install the command-line tool from Downloads and installation. On a Mac, download the disk image and its SHA256SUMS, verify, and copy the program onto your path:

shasum -a 256 -c SHA256SUMS
open structop-0.1.0-macos-universal.dmg
install -m 755 /Volumes/StructOp/structop "$HOME/.local/bin/structop"
structop version            # StructOp v0.1.0
Enter fullscreen mode Exit fullscreen mode

The first time, read the licence terms and accept them once:

structop licence accept
Enter fullscreen mode Exit fullscreen mode

Linux .deb/.rpm packages (ARM64 and x86-64) and a Homebrew cask are on the same page. The C examples need a C11 compiler; for Rust, install Cargo.

Three files

Our first project asks for your name and greets you.

intent.st, vars.st and main.st, and how the prompt writes the value that print reads

intent.st chooses the build target:

structop {
    type_app {
        exec = { lang = "C" }
    }
}
Enter fullscreen mode Exit fullscreen mode

vars.st declares a value that holds the name, and a data block that reads it:

ability "value" "name" {
    description = "Who to greet"
    type        = String
    initial     = "world"
}
data "value" "name" {
    of = ability.value.name
}
Enter fullscreen mode Exit fullscreen mode

Data that changes lives in a value. Whatever writes it must name it; everything else reads it through data. So every write and every read is visible.

main.st declares two abilities: a prompt named Ask that writes the name, and a print named Greet. depends_on makes Greet wait for Ask.

ability "prompt" "Ask" {
    prompt   = "What is your name"
    response = ability.value.name
}
ability "print" "Greet" {
    message    = "Hello, ${data.value.name.current}!"
    depends_on = [ability.prompt.Ask]
}
Enter fullscreen mode Exit fullscreen mode

init, plan, apply

$ structop init
Initializing provider plugins...
- Provider index StructOp: serial 7 (verified)
- Installed StructOp/c v2.3.0
- Installed StructOp/rust v2.3.0

StructOp has been successfully initialized!
$ structop plan
  # program.app will be created
  + program.app (.structop/build/app)
      + ability.prompt.Ask
      + ability.print.Greet
      + ability.value.name

Plan: 1 to add, 0 to change, 0 to destroy.
$ structop apply
  ...
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

What is your name: Ada
Hello, Ada!
$ structop plan
No changes. Your application matches the configuration.
Enter fullscreen mode Exit fullscreen mode

init fetches the providers the project names from a verified index. plan compares the declarations with the recorded state and shows what apply would do. apply generates the source, compiles it, records the new state and runs the program.

The real init, plan and apply session

Draw it

structop graph draws what we declared: Greet depends on Ask, and reads the name through its data block.

structop graph, redrawn: prompt.Ask, print.Greet, value.name and data.value.name

Change one word

Change Hello to Welcome and plan shows exactly which ability changed, and how:

$ structop plan
  # program.app will be replaced
  ~ program.app (.structop/build/app)
      ~ ability.print.Greet
          message: "Hello, ${data.value.name.current}!" -> "Welcome, ${data.value.name.current}!"

Plan: 0 to add, 1 to change, 0 to destroy.
Enter fullscreen mode Exit fullscreen mode

Want Rust instead of C? Change one word in intent.st, lang = "Rust", and the same declarations are built with Cargo.

One-word change, and the same declarations rebuilt for Rust

What the checks prove, and what they don't

Before any code is generated, StructOp checks that every attribute is one the provider allows, that every expression is well typed, that every address names a declaration, and that a value is written only by abilities that name it.

Those checks find errors in the declarations. They don't prove the program does what you meant. That's what tests are for.

Next

That's DAE: describe the application as declarations, let the tool check, plan and draw them, then build native code. Next episode we grow this into a guest list with types, a loop, a function and a switch, following the handbook's guided tour.

StructOp is created and owned by Sibusiso Nxumalo.

Top comments (0)