/prism:prism-programmingfrom prism
Teaches PRISM probabilistic Prolog programming: writing .psm files, using msw/2, learning parameters via EM, and converting ProbLog/Prolog models. Essential before writing any PRISM code to avoid silent errors from shared random variables and negation.
Ships in prism. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
PRISM is a generative probabilistic logic language on top of B-Prolog (not
SWI-Prolog). You write a Prolog program that simulates how data is
generated, drawing every random choice from a named "random switch" with
msw/2. From that same program, the system derives probabilities, Viterbi
paths, hindsight probabilities and EM-learned parameters by tabled explanation
search.
doc/manual/manual.tex, also as HTML on the GitHub Pages siteexs/../tprism-programming/references/Dockerfile). upprism file.psm with a prism_main is the easiest way to script checks.| If you think in ProbLog or Prolog | In PRISM |
|---|---|
0.3::rain.: a probabilistic fact with a number in the program | No numbers in the model. Declare an outcome space, values(rain,[yes,no])., and draw with msw(rain,X). Numbers are parameters held by the system: set_sw/2, values/3, or learned by learn/1. The default is uniform. |
| A probabilistic fact used twice is one shared random variable | Every msw call is a fresh independent draw, even with the same switch name. To share a value, bind one variable and pass it around. |
| Overlapping proofs are fine | Explanations of a goal must be mutually exclusive. PRISM just adds their probabilities, so overlaps give silently wrong numbers (gotcha G2). |
query(g). and evidence(e,true). | Use prob/2, viterbif/3, hindsight/3 and chindsight_agg/2. Evidence goes into the arguments of the observed goal. |
0.9::a :- b.: dependent choice | Dependence is expressed by different switch names per context: msw(tr(State),Next), msw(al(Fire,Tamper),Alarm). |
\+, findall or forall around any goal | Never around probabilistic goals. A \+ of a probabilistic goal makes plain upprism run nothing, with no error (verified; G7). Negate only deterministic facts, or draw and then test. |
assert/retract freely | Never inside probabilistic predicates. Use them only in utility code. values/2 bodies must be free of side effects. |
SWI-Prolog libraries and directives (use_module, strings, forall/2, maplist/3 with a closure) | B-Prolog dialect (G14). Data files written for SWI need a loader (G19). |
A .psm file has three parts:
values/2);msw directly or indirectly;prism_main.%% declarations (outcome spaces)
values(init,[s0,s1]).
values(out(_),[a,b]). % family of switches out(s0), out(s1), ...
values(tr(_),[s0,s1]).
%% modeling part: generative, probabilistic predicates
hmm(L):- msw(init,S), hmm(1,S,L).
hmm(T,_,[]):- T>10, !.
hmm(T,S,[Ob|Y]):-
msw(out(S),Ob), % emission depends on S -> switch named by S
msw(tr(S),Next), % transition depends on S
T1 is T+1,
hmm(T1,Next,Y).
%% utility part: ordinary Prolog, never called during explanation search
prism_main([]):-
random_set_seed(1234),
get_samples(50,hmm(_),Gs),
learn(Gs),
show_sw.
Rules of thumb that keep you inside the modeling assumptions:
hmm([a,b,b])).values/2 head.
_ for families.msw is called. The outcome may be unbound, and explanation search then enumerates it.probf/1.| Task | How |
|---|---|
| Interactive | Run prism, then ?- prism(model)., which loads model.psm. prism([consult],model) is for tracing only, because nothing is tabled then. |
| Batch | Define prism_main/1 and run upprism model arg1 arg2. The arguments arrive as atoms (parse_atom/2). If both /0 and /1 exist, only /1 runs. Details: §6. |
Failure programs (failure/0, negation) | upprism prismn:model or prismn/1 (G7). |
| Multiple files | Probabilistic predicates in other files need :- include('other.psm'). |
| From Python | Use pyprism; see the programming-pyprism skill. |
A typical session:
?- prism(bloodtype).
?- set_sw(gene,[0.3,0.2,0.5]). % optional; default uniform
?- sample(bloodtype(X)). % forward sampling
?- prob(bloodtype(a),P). % exact probability (fails for an impossible goal)
?- learn([count(bloodtype(a),40),count(bloodtype(b),20),count(bloodtype(o),30),count(bloodtype(ab),10)]).
?- show_sw. % learned parameters
?- save_sw('params.sw'). % ... and restore_sw('params.sw') in a later run
?- viterbif(hmm([a,a,b])). % most probable explanation, printed
?- probf(hmm([a,a,b])). % the explanation graph (best debugging tool)
?- hindsight(hmm([a,b,a,b]),hmm(2,_,_,_),Ps). % P(subgoal, goal)
?- chindsight_agg(world(_,_,_,yes,_,no),world(_,_,query,yes,_,no)). % P(Alarm | evidence)
About learning:
learn/1 takes goals or count(Goal,N). Every goal must be an instance of a probabilistic predicate and must have an explanation; otherwise the whole call aborts (G18).set_prism_flag/2):
init: none starts EM from the current parameters (G11).restart, max_iterate, epsilondefault_sw_d: pseudo counts, for MAP estimationlearn_mode (ml, vb, ...) and log_scale (G16)error_on_cyclelearn_statistics/2 gives log_likelihood, bic, free_energy and more after learning. See references/builtins.md.0.3::a. becomes values(a,[t,f]) with msw(a,X).msw call. If the model needs one shared variable, draw it once at the top and pass the value down (mind §2 rule 4).msw(alarm(B,E),A).world(Sm,Re).probf(G) on a tiny ground goal and read the <=> lines.prob/2 sums to 1 over the observable goals.A side-by-side example is §7 of references/examples.md.
The easiest way to design a PRISM model is often to write, or reuse, the deterministic Prolog rules first, and then turn each hard-coded mapping into a switch. The default behaviour then reproduces the rules, and EM refines it from data.
foo_default/N. They are the prior.foo/N with the same name and arity as msw: foo(K,V) :- msw(foo_sw(K),V).. Callers stay unchanged.\+ foo(...) would now negate a probabilistic goal, which is not allowed (G7). Guard on a deterministic fact instead. This usually also keeps the rules exclusive.set_sw. Switches without a default stay uniform.set_prism_flag(init,none), filter out goals without an explanation (G18), then learn, and finally save_sw.The full recipe is in references/deterministic-to-probabilistic.md. A
verified, runnable example with three CLI modes is in examples/activity.psm
(§8 of references/examples.md).
prism_main([learn, Params | Data]), prism_main([infer, Params, Data | Query]) and prism_main([Data | Query]) (defaults only). Add a usage clause as the last one.DIST:Key:Value:Prob. Never print raw terms that the caller has to parse as Prolog.save_sw / restore_sw, one parameter file per data split. Mask held-out labels with retractall/1 before querying.Aborted by exception -- ....prism_main gives exit status 0 with the last line no.Details and a CLI skeleton: references/batch-and-pipelines.md.
probf(Goal) on the smallest ground goal and read the <=> equations.
sample(Goal) a few times, and check that the story generates sensible data.prob/2 of all the observable goals of a tiny instance should sum to 1. If not, see G2, G7 and G8.learn, look at show_sw for zero or unused switches. They often come from typos in the switch names.prism_main output? Look for \+ or not around a probabilistic goal (G7).| File | Read it when |
|---|---|
references/gotchas.md | A result looks off, or before writing anything non-trivial. G1–G19, each with wrong and right code. |
references/examples.md | You want a complete, idiomatic program: coin, blood type, HMM, PCFG, Bayesian network with evidence, failure program, ProbLog translation, deterministic-to-probabilistic. |
references/builtins.md | You need a built-in or a flag: sampling, learning, switches, B-Prolog list and loop constructs. |
references/deterministic-to-probabilistic.md | You are upgrading existing Prolog rules (§5). |
references/batch-and-pipelines.md | The program is called from a shell script, CI job or another language (§6). |
examples/activity.psm (+ data.pl, goals.pl) | You need a runnable template for §5 and §6. |
Authoritative details are in the repository's exs/base/*.psm, in
doc/manual/manual.tex and in testing/programs/. For tensors, embeddings
or neural networks, use tprism-programming.
examples/activity.psm (opens in a new tab)examples/data.pl (opens in a new tab)examples/goals.pl (opens in a new tab)references/batch-and-pipelines.md (opens in a new tab)references/builtins.md (opens in a new tab)references/deterministic-to-probabilistic.md (opens in a new tab)references/examples.md (opens in a new tab)references/gotchas.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub prismplp/prism --plugin prism6 skills in this plugin
/install-linuxBuild PRISM and T-PRISM from source and set up their environment on Linux (Ubuntu 22.04, 24.04, 26.04). Covers the apt packages, the C and Prolog build with the USE_NPY, USE_H5, USE_PB and PROCTYPE=mp options, PATH, regression tests, the T-PRISM Python environment (venv, PyTorch, protobuf), the runtime libraries of the prebuilt packages, and fixes for common build and run errors. Use this skill whenever someone wants to install, compile, build, rebuild or set up PRISM or T-PRISM on Linux or Ubuntu (e.g. "コンパイル", "ビルド", "環境構築", "インストール"). Also use it when someone hits an error from make, upprism, prism or tprism on Linux, or prepares a Linux machine, container or CI job for PRISM, even if they don't say "install"./install-mingwBuild and verify PRISM and bp4prism on Windows with native MinGW-w64, including optional 32bit and 64bit MS-MPI builds, without tools/mingw helper scripts./programming-prism-dsOther plugins' skills for the same job