Skip to content
Volume P · The TensorCore.si Library

Pascal

The classic language for learning to program.

Pascal was designed by Niklaus Wirth to teach structured programming clearly. For decades it was the first language in schools and universities, and through Delphi and Free Pascal it still builds fast desktop applications today.

Prologue

Why learn Pascal?

Pascal's clear structure makes it one of the best languages for understanding how programs are built step by step.

  • Readable: begin ... end blocks and clear keywords.
  • Strict types: catches mistakes before the program runs.
  • Fast native apps: Lazarus and Delphi build GUI apps quickly.
Prologue

Set up and run your first program

Install Free Pascal (fpc). Lazarus adds a visual IDE for desktop apps.

terminal
sudo apt install fpc
fpc hello.pas
./hello
hello.pas
program Hello;
var
  name: string = 'Ada';
begin
  writeln('Hello, AI! Welcome to Pascal, ', name, '.');
end.
TipThe main program ends with end. (a full stop).
Chapter 1

Variables and types

Variables are declared in a var section before begin. Assignment uses :=.

variables.pas
program Variables;
var
  layers: integer;
  params, gb: real;
  model: string;
begin
  layers := 32;
  params := 8.03e9;
  model := 'llama3.1';
  gb := params * 2 / 1e9;
  writeln(model, ': ', layers, ' layers, ', gb:0:1, ' GB');
end.
Chapter 2

Control flow

Pascal has if, case, for, while and repeat ... until. Use begin ... end to group several statements.

control.pas
program Control;
var epoch, bits: integer;
begin
  for epoch := 1 to 3 do
    writeln('epoch ', epoch);
  bits := 8;
  case bits of
    32: writeln('FP32');
    16: writeln('FP16');
    1..8: writeln('low precision');
  else
    writeln('other');
  end;
end.
Chapter 3

Procedures and functions

Procedures do work; functions return a value through Result. var parameters let a procedure change its argument.

procs.pas
program Procs;
{$mode objfpc}
function Params(layers, width: Int64): Int64;
begin
  Result := 12 * layers * width * width;
end;

procedure Double(var x: integer);
begin
  x := x * 2;
end;

var n: integer = 21;
begin
  writeln(Params(32, 4096));
  Double(n);
  writeln(n);
end.
Chapter 4

Arrays and records

Arrays have declared index ranges. Records group related fields into one type.

records.pas
program Records;
type
  TGpu = record
    name: string;
    vram: integer;
  end;
var
  gpus: array[1..3] of TGpu;
  i, total: integer;
begin
  gpus[1].name := 'H100'; gpus[1].vram := 80;
  gpus[2].name := 'H200'; gpus[2].vram := 141;
  gpus[3].name := 'RTX 5090'; gpus[3].vram := 32;
  total := 0;
  for i := 1 to 3 do
  begin
    if gpus[i].vram >= 80 then writeln(gpus[i].name, ' is a data center GPU');
    total := total + gpus[i].vram;
  end;
  writeln('Total VRAM: ', total, ' GB');
end.
Chapter 5

Classes (Object Pascal)

Free Pascal and Delphi add classes with constructors, methods and properties.

classes.pas
program Classes;
{$mode objfpc}{$H+}
type
  TGpu = class
    Name: string;
    Vram: integer;
    constructor Create(AName: string; AVram: integer);
    function Fits(gb: integer): boolean;
  end;

constructor TGpu.Create(AName: string; AVram: integer);
begin Name := AName; Vram := AVram; end;

function TGpu.Fits(gb: integer): boolean;
begin Result := gb <= Vram; end;

var g: TGpu;
begin
  g := TGpu.Create('RTX 5090', 32);
  writeln(g.Name, ' fits 16 GB: ', g.Fits(16));
  g.Free;
end.
Part II · AI with Pascal

AI with Pascal

Pascal is not a common AI language, but it is a great way to learn the maths behind AI without hidden magic. Free Pascal has neural network libraries such as CAI Neural API, and Delphi can call Python AI libraries through Python4Delphi or LLM APIs over HTTP.

The essential libraries

CAI Neural APINeural networks in Free Pascal and Delphi.
fphttpclientHTTP client in Free Pascal.
fpjsonJSON parsing in Free Pascal.
Python4DelphiUse Python AI libraries from Delphi.

Gradient descent from scratch

Fit y = 3x + 0.5 with plain loops.

gradient.pas
program Gradient;
const N = 100;
var
  xs, ys: array[1..N] of real;
  w, b, gw, gb, err: real;
  i, step: integer;
begin
  for i := 1 to N do
  begin
    xs[i] := -1 + 2 * (i - 1) / (N - 1);
    ys[i] := 3 * xs[i] + 0.5;
  end;
  w := 0; b := 0;
  for step := 1 to 500 do
  begin
    gw := 0; gb := 0;
    for i := 1 to N do
    begin
      err := w * xs[i] + b - ys[i];
      gw := gw + 2 * err * xs[i] / N;
      gb := gb + 2 * err / N;
    end;
    w := w - 0.1 * gw;
    b := b - 0.1 * gb;
  end;
  writeln('w = ', w:0:2, '  b = ', b:0:2);
end.
Part III · Build an AI agent

Call an LLM from Pascal

Free Pascal's fphttpclient and fpjson units can send a chat request to a local model and read the reply. A tool-calling loop follows the same steps as in the other volumes.

Your goal→Model plans→Tool call→Your code runs the tool→Result back to model→Final answer
chat.pas
program Chat;
{$mode objfpc}{$H+}
uses SysUtils, Classes, fphttpclient, fpjson, jsonparser;
var
  client: TFPHTTPClient;
  body, reply: string;
  data: TJSONData;
begin
  body := '{"model":"llama3.1","messages":[{"role":"user","content":"Explain tensor cores in one sentence."}]}';
  client := TFPHTTPClient.Create(nil);
  try
    client.AddHeader('Content-Type', 'application/json');
    client.RequestBody := TStringStream.Create(body);
    reply := client.Post('http://localhost:11434/v1/chat/completions');
    data := GetJSON(reply);
    writeln(data.FindPath('choices[0].message.content').AsString);
    data.Free;
  finally
    client.RequestBody.Free;
    client.Free;
  end;
end.

To add tools, include a tools array in the body and check choices[0].message.tool_calls in the reply.

Run it locallyInstall Ollama, run ollama pull llama3.1, and keep it running. The code talks to its OpenAI-compatible API at http://localhost:11434/v1, so the same program also works with vLLM, llama.cpp server or a cloud API by changing the URL, model name and adding an API key.
Part IV · Your path

Pascal learning roadmap

Tick items off as you go. Your progress is saved in this browser.

Beginner

Intermediate

Advanced

Part IV · Your path

Projects to build

Grade calculatorRead scores and print averages.
Address bookStore records in a file.
AI desktop appA Lazarus window that chats with a local LLM.
Reference

Pascal cheat sheet

TaskSyntax
Variablevar x: integer;
Assignx := 5;
Loopfor i := 1 to 10 do
Blockbegin ... end;
Functionfunction F(x: integer): integer;
Printwriteln('Hi ', x);
Reference

Common mistakes

  • Use := to assign and = to compare.
  • No semicolon before else in an if statement.
  • Strings are 1-indexed: the first character is s[1].
Reference

Where to go next

On TensorCore.si: C volume, Ada volume.

Keyboard: [ previous language · ] next language · / search the library