Home /Modules/churn/Constraints.java

Constraints.java

churn/2/Constraints.java · 157 line(s) · java

package com.mearvk.sleela.churn;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * The Constitution's congrains / limits applied to a churn relevance set.
 *
 * <p>Shared constraint/theory layer carried by every churn set (/1 Longs,
 * /2 Structures, /3 Facets, /4 Areas). It encodes the governing constraints
 * from the repository's Constitution (top-level README, "The Constitution",
 * and SHEET.sheet's {@code congrains} / {@code limits} / {@code system}
 * sections) as a small, dependency-free Java class, so the same theory can be
 * checked from the JVM side as from Sleela.
 *
 * <p>System invariants always in force: {@code DEPTH_LIMIT} (&le; 3024) and
 * {@code DEGREE_MAX} (&le; 4). A limit (Bound / Threshold / Capacity) checks a
 * set's magnitude — here, a Structure's value. Failing a limit records that a
 * value falls outside a bound; it is a modelling condition, not a claim about
 * reality.
 *
 * <p>Running {@code main} prints the report to stdout and also writes a
 * well-formed Markdown report to a local {@code DD-MM-YYYY/} folder (named for
 * the run date), as {@code churn-2-structures.md}.
 */
public final class Constraints {

    public static final String SET_ID = "2";
    public static final String SET_TITLE = "Structures";
    public static final String REPORT_FILE = "churn-2-structures.md";

    public static final int DEPTH_LIMIT = 3024;
    public static final int DEGREE_MAX = 4;

    public static boolean connectPermitted(int n, int degree) {
        return n >= 0 && n <= DEPTH_LIMIT && degree <= DEGREE_MAX;
    }

    public static boolean withinBound(double value, double lo, double hi) {
        return value >= lo && value <= hi;
    }

    public static boolean belowThreshold(double value, double threshold) {
        return value < threshold;
    }

    public static boolean withinCapacity(double used, double capacity) {
        return capacity >= 0.0 && used <= capacity;
    }

    private record BoundCheck(String name, double value, double lo, double hi) {
        String verdict() {
            return withinBound(value, lo, hi) ? "HOLDS" : "VIOLATES";
        }
    }

    private Constraints() {
    }

    public static void main(String[] args) {
        List<BoundCheck> bounds = new ArrayList<>();
        bounds.add(new BoundCheck("labor force participation rate (US 2024)", 62.5, 0.0, 100.0));
        bounds.add(new BoundCheck("employment-population ratio (US 2024)", 59.9, 0.0, 100.0));
        bounds.add(new BoundCheck("speed of light c", 299_792_458.0, 0.0, 299_792_458.0));

        System.out.println("=== /" + SET_ID + " constraints (Java) -- the Constitution applied to "
                + SET_TITLE + " ===");
        System.out.println("System invariants: DEPTH_LIMIT=" + DEPTH_LIMIT
                + ", DEGREE_MAX=" + DEGREE_MAX + ".");
        System.out.println("[limits] Structure values must hold within their stated bounds");
        for (BoundCheck b : bounds) {
            System.out.println("  Bound[" + b.lo() + ", " + b.hi() + "] on " + b.name()
                    + " (value=" + b.value() + "): " + b.verdict());
        }
        System.out.println("[invariant] the connect rule (n<=3024, degree<=4)");
        System.out.println("  connect(3024, 4) permitted : " + connectPermitted(3024, 4));
        System.out.println("  connect(3025, 4) permitted : " + connectPermitted(3025, 4));
        System.out.println("  connect(100, 5) permitted  : " + connectPermitted(100, 5));

        String capacityNote = "Structure bounds: social rates within [0, 100] percent; "
                + "a physical constant is a positive magnitude (c bounded by its own extent)";
        try {
            Path out = writeReport(bounds, capacityNote);
            System.out.println("wrote report: " + out);
        } catch (IOException e) {
            System.err.println("error: could not write Markdown report: " + e.getMessage());
        }
    }

    static Path writeReport(List<BoundCheck> bounds, String capacityNote) throws IOException {
        LocalDate today = LocalDate.now();
        String folder = today.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
        Path dir = Path.of(folder);
        Files.createDirectories(dir);
        Path file = dir.resolve(REPORT_FILE);
        Files.writeString(file, buildMarkdown(bounds, capacityNote, today), StandardCharsets.UTF_8);
        return file;
    }

    static String buildMarkdown(List<BoundCheck> bounds, String capacityNote, LocalDate date) {
        StringBuilder md = new StringBuilder();
        md.append("# churn/").append(SET_ID).append(" — ").append(SET_TITLE)
          .append(": Constitution constraints report\n\n");
        md.append("_Generated ").append(date.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")))
          .append(" by `com.mearvk.sleela.churn.Constraints` (churn/").append(SET_ID)
          .append(")._\n\n");

        md.append("## System invariants\n\n");
        md.append("| Invariant | Value |\n");
        md.append("|---|---|\n");
        md.append("| DepthLimit (max system depth) | ").append(DEPTH_LIMIT).append(" |\n");
        md.append("| ComplexityDegree (max) | ").append(DEGREE_MAX).append(" |\n\n");

        md.append("## Limits — Bound checks\n\n");
        md.append("Each **Structure** value must hold within its stated bound.\n\n");
        md.append("| Subject | Value | Bound | Verdict |\n");
        md.append("|---|---:|---|:--:|\n");
        for (BoundCheck b : bounds) {
            md.append("| ").append(b.name())
              .append(" | ").append(fmt(b.value()))
              .append(" | [").append(fmt(b.lo())).append(", ").append(fmt(b.hi())).append("]")
              .append(" | ").append(b.verdict()).append(" |\n");
        }
        md.append('\n');

        md.append("## Notes on the bounds\n\n");
        md.append("- ").append(capacityNote).append("\n\n");

        md.append("## Invariant — the connect rule\n\n");
        md.append("`connect(n)` is permitted iff `0 <= n <= ").append(DEPTH_LIMIT)
          .append("` and `degree <= ").append(DEGREE_MAX).append("`.\n\n");
        md.append("| Call | Permitted |\n");
        md.append("|---|:--:|\n");
        md.append("| connect(3024, 4) | ").append(connectPermitted(3024, 4)).append(" |\n");
        md.append("| connect(3025, 4) | ").append(connectPermitted(3025, 4)).append(" |\n");
        md.append("| connect(100, 5) | ").append(connectPermitted(100, 5)).append(" |\n\n");

        md.append("---\n\n");
        md.append("_A constraint is a modelling condition to be checked and argued with, ")
          .append("not a claim about reality. `ASSOCIATION != CAUSATION`._\n");
        return md.toString();
    }

    private static String fmt(double v) {
        if (v == Math.floor(v) && !Double.isInfinite(v)) {
            return String.format("%,d", (long) v);
        }
        return String.valueOf(v);
    }
}