Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions docs/stlc-progress-example.hazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
type Ty = +TB+TArr(Ty, Ty) in
type Term = +TmVar(Int)+TmLam(Ty, Term)+TmAp(Term, Term) in
type MaybeTy = +NoTy+SomeTy(Ty) in
type MaybeTm = +NoTm+SomeTm(Term) in
let ty_eq = fun p -> case p | (TB, TB) => true | (TB, TArr(x, y)) => false | (TArr(x, y), TB) => false | (TArr(x1, y1), TArr(x2, y2)) => ty_eq((x1, x2)) && ty_eq((y1, y2)) end in
let nth_ty = fun p -> case p | ([], n) => NoTy | (t :: rest, n) => if n == 0 then SomeTy(t) else nth_ty((rest, n - 1)) end in
let lam_result = fun p -> case p | (t, NoTy) => NoTy | (t, SomeTy(t2)) => SomeTy(TArr(t, t2)) end in
let ap_result = fun p -> case p | (NoTy, m) => NoTy | (SomeTy(TB), m) => NoTy | (SomeTy(TArr(ta, tb)), NoTy) => NoTy | (SomeTy(TArr(ta, tb)), SomeTy(s)) => (if ty_eq((ta, s)) then SomeTy(tb) else NoTy) end in
let infer = fun p -> case p | (ctx, TmVar(n)) => nth_ty((ctx, n)) | (ctx, TmLam(t, b)) => lam_result((t, infer((t :: ctx, b)))) | (ctx, TmAp(e1, e2)) => ap_result((infer((ctx, e1)), infer((ctx, e2)))) end in
let is_value = fun e -> case e | TmVar(n) => false | TmLam(t, b) => true | TmAp(x, y) => false end in
let is_lam = fun e -> case e | TmLam(t, b) => true | TmVar(n) => false | TmAp(x, y) => false end in
let is_some_tm = fun m -> case m | NoTm => false | SomeTm(x) => true end in
let shift = fun p -> case p | (d, c, TmVar(n)) => (if n < c then TmVar(n) else TmVar(n + d)) | (d, c, TmLam(t, b)) => TmLam(t, shift((d, c + 1, b))) | (d, c, TmAp(x, y)) => TmAp(shift((d, c, x)), shift((d, c, y))) end in
let subst = fun p -> case p | (j, v, TmVar(n)) => (if n == j then v else TmVar(n)) | (j, v, TmLam(t, b)) => TmLam(t, subst((j + 1, shift((1, 0, v)), b))) | (j, v, TmAp(x, y)) => TmAp(subst((j, v, x)), subst((j, v, y))) end in
let step = fun e -> case e | TmVar(n) => NoTm | TmLam(t, b) => NoTm | TmAp(x, y) => (if is_value(x) then (if is_value(y) then case x | TmVar(n) => NoTm | TmLam(t, b) => SomeTm(subst((0, y, b))) | TmAp(f, a) => NoTm end else case step(y) | NoTm => NoTm | SomeTm(yp) => SomeTm(TmAp(x, yp)) end) else case step(x) | NoTm => NoTm | SomeTm(xp) => SomeTm(TmAp(xp, y)) end) end in
theorem step_ap_val1 = forall u1: Term -> forall u2: Term -> is_value(u1) == false ==> step(TmAp(u1, u2)) == (case step(u1) | NoTm => NoTm | SomeTm(xp) => SomeTm(TmAp(xp, u2)) end) proof assume is_value(u1) == false => from step(TmAp(u1, u2)) do step end; from is_value(u1) do rewrite assume end end; ? in
theorem step_lam_ap2 = forall w1: Ty -> forall q1: Term -> forall u2: Term -> is_value(u2) == false ==> step(TmAp(TmLam(w1, q1), u2)) == (case step(u2) | NoTm => NoTm | SomeTm(yp) => SomeTm(TmAp(TmLam(w1, q1), yp)) end) proof assume is_value(u2) == false => from step(TmAp(TmLam(w1, q1), u2)) do step end; from is_value(TmLam(w1, q1)) do step end; from is_value(u2) do rewrite assume end end; ? in
theorem step_beta = forall w1: Ty -> forall q1: Term -> forall u2: Term -> is_value(u2) == true ==> step(TmAp(TmLam(w1, q1), u2)) == SomeTm(subst((0, u2, q1))) proof assume is_value(u2) == true => from step(TmAp(TmLam(w1, q1), u2)) do step end; from is_value(TmLam(w1, q1)) do step end; from is_value(u2) do rewrite assume end end; ? in
theorem some_tm_is_some = forall u1: Term -> is_some_tm(SomeTm(u1)) == true proof from is_some_tm(SomeTm(u1)) do step end; from true == true do rewrite refl end end in
theorem progress = forall e0: Term -> forall t0: Ty -> infer(([], e0)) == SomeTy(t0) ==> is_value(e0) || is_some_tm(step(e0)) proof generalize t0 => induction e0
| TmVar(m0) => forall t0 => from infer(([], TmVar(m0))) do step end; from nth_ty(([], m0)) do step end; from NoTy == SomeTy(t0) do step end; from false ==> $e do step end
| TmLam(w0, q0) => forall t0 => assume infer(([], TmLam(w0, q0))) == SomeTy(t0) => from is_value(TmLam(w0, q0)) do step end; from true || $e do step end
| TmAp(x0, y0) => forall t0 =>
alias ihx = ih_x0 =>
alias ihy = ih_y0 =>
from infer(([], TmAp(x0, y0))) do step end; cases infer(([], x0)) as tx
| NoTy => cases infer(([], y0)) as ty
| NoTy => from ap_result((NoTy, NoTy)) do step end; from NoTy == SomeTy(t0) do step end; from false ==> $e do step end
| SomeTy(s0) => from ap_result((NoTy, SomeTy(s0))) do step end; from NoTy == SomeTy(t0) do step end; from false ==> $e do step end
end
| SomeTy(tf0) => cases tf0
| TB => cases infer(([], y0)) as ty
| NoTy => from ap_result((SomeTy(TB), NoTy)) do step end; from NoTy == SomeTy(t0) do step end; from false ==> $e do step end
| SomeTy(s0) => from ap_result((SomeTy(TB), SomeTy(s0))) do step end; from NoTy == SomeTy(t0) do step end; from false ==> $e do step end
end
| TArr(ta0, tb0) => cases infer(([], y0)) as ty
| NoTy => from ap_result((SomeTy(TArr(ta0, tb0)), NoTy)) do step end; from NoTy == SomeTy(t0) do step end; from false ==> $e do step end
| SomeTy(s0) => from ap_result((SomeTy(TArr(ta0, tb0)), SomeTy(s0))) do step end; cases ty_eq((ta0, s0)) as teq
| false => from if false then SomeTy(tb0) else NoTy do step end; from NoTy == SomeTy(t0) do step end; from false ==> $e do step end
| true => from if true then SomeTy(tb0) else NoTy do step end; assume SomeTy(tb0) == SomeTy(t0) => cases is_value(x0) as vx
| false => from is_value(TmAp(x0, y0)) do step end; from false || $e do step end; from step(TmAp(x0, y0)) do rewrite step_ap_val1 end end; cases step(x0) as sx
| NoTm => revert ihx with t0 = tf0 =>
from infer(([], x0)) do rewrite tx end end;
from SomeTy(tf0) == SomeTy(tf0) do rewrite refl end end;
from true ==> is_value(x0) || is_some_tm(step(x0)) do step end;
from is_value(x0) do rewrite vx end end;
from false || is_some_tm(step(x0)) do step end;
from step(x0) do rewrite sx end end;
from is_some_tm(NoTm) do step end;
from false ==> $e do step end
| SomeTm(u0) => from is_some_tm(case SomeTm(u0) | NoTm => NoTm | SomeTm(xp) => SomeTm(TmAp(xp, y0)) end) do step end
end
| true => cases x0
| TmVar(m0) => revert is_value(x0) == true => from x0 do rewrite case_eq end end; from is_value(TmVar(m0)) do step end; from false == true do step end; from false ==> $e do step end
| TmLam(w0, q0) => from is_value(TmAp(TmLam(w0, q0), y0)) do step end; from false || $e do step end; cases is_value(y0) as vy
| true => from step(TmAp(TmLam(w0, q0), y0)) do rewrite step_beta end end; from is_some_tm(SomeTm(subst((0, y0, q0)))) do rewrite some_tm_is_some end end
| false => from step(TmAp(TmLam(w0, q0), y0)) do rewrite step_lam_ap2 end end; cases step(y0) as sy
| NoTm => revert ihy with t0 = s0 =>
from infer(([], y0)) do rewrite ty end end;
from SomeTy(s0) == SomeTy(s0) do rewrite refl end end;
from true ==> is_value(y0) || is_some_tm(step(y0)) do step end;
from is_value(y0) do rewrite vy end end;
from false || is_some_tm(step(y0)) do step end;
from step(y0) do rewrite sy end end;
from is_some_tm(NoTm) do step end;
from false ==> $e do step end
| SomeTm(u0) => from is_some_tm(case SomeTm(u0) | NoTm => NoTm | SomeTm(yp) => SomeTm(TmAp(TmLam(w0, q0), yp)) end) do step end
end
end
| TmAp(f0, a0) => revert is_value(x0) == true => from x0 do rewrite case_eq end end; from is_value(TmAp(f0, a0)) do step end; from false == true do step end; from false ==> $e do step end
end
end
end
end
end
end
end in 0
6 changes: 3 additions & 3 deletions hazel-programs/config/colors.hz
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ let theme_of(p : ColorPalette, role : ColorOverrides): ColorScheme = ^^fold(let
let text_l = color_lightness(p.`ink`) in
let toward(base, c, t) =
color_with_lightness(c, base +. (text_l -. base) *. t) in
# at(c, t) places c on this scheme's OWN contrast axis: t = 0. is the #
# on_axis(c, t) places c on this scheme's OWN contrast axis: t = 0. is the #
# page background's lightness, t = 1. the body text's. That is why one #
# definition is correct in all four schemes, where a fixed lightness is #
# right in light and invisible in dark. #
let at(c, t) = toward(bg_l, c, t) in
let on_axis(c, t) = toward(bg_l, c, t) in
# wash scales chroma too: a fill wants a wash of its hue, an accent #
# the whole thing. #
let wash(c, t, sat) = at(color_saturate(c, sat), t) in
let wash(c, t, sat) = on_axis(color_saturate(c, sat), t) in
# a fill must be placed against the surface it actually sits ON, and #
# not every surface is the page: table rows sit on the code background, #
# ten lightness points off the page in dark. Same span, same ink. #
Expand Down
23 changes: 23 additions & 0 deletions hazel-programs/docs/reference/prover-induction.hz
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# CASES AND INDUCTION #

# A goal stuck on a variable can be split into cases, one per #
# pattern the variable can match. #
# cases e each case knows e == pattern, as the fact case_eq #
# induction e each case instead knows the statement for the #
# smaller values inside it: ih_t for a tail t #
# Use induction on a recursive type such as a list, and cases for #
# anything else, such as a Bool test. Facts about e leave scope in #
# an induction on e; revert a fact first to keep it. #

theorem app_nil = forall xs: [Int] -> xs @ [] == xs proof
induction xs
| [] =>
from [] @ [] do step end;
rewrite refl end
| x :: t =>
from (x :: t) @ [] do step end;
from t @ [] do rewrite ih_t end end;
rewrite refl end
end
in
¿
32 changes: 32 additions & 0 deletions hazel-programs/docs/reference/prover-lemmas.hz
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# VARIABLES AND LEMMAS #

# forall x: T -> P states P for every x of type T. In the proof, x #
# is an unknown value: evaluation can step around it, but not into #
# it. Goals keep definitions by name, so rev stays rev until a step #
# needs to look inside it. #

let rev = fun xs -> case xs | [] => [] | y :: t => rev(t) @ [y] end in

theorem rev_one = forall x: Int -> rev([x]) == [x] proof
from rev([x]) do step end;
from rev([]) do step end;
from [] @ [x] do step end;
rewrite refl end
in

# A proven theorem is a rule. On a goal that matches the rule's left #
# side, rewrite name end replaces it with the right side; inside from #
# p do ... end, it rewrites just p. rewrite <- name end goes right to #
# left, and the builtin rule refl closes e == e. Typing a rule's name #
# in the write box lists the places it applies. #
theorem rev_two = forall x: Int -> forall y: Int -> rev([x, y]) == [y, x] proof
from rev([x, y]) do step end;
from rev([y]) do rewrite rev_one end end;
from [y] @ [x] do step end;
rewrite refl end
in

# A proof may use a theorem that isn't finished: it then reads #
# proven*, assuming that theorem, and the star names it. Using a #
# refuted one is an error. #
¿
75 changes: 75 additions & 0 deletions hazel-programs/docs/reference/prover-reference.hz
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# EVERY STEP #

# Click any step in these proofs to read about it in the docs panel. #

let sum = fun xs -> case xs | [] => 0 | y :: t => y + sum(t) end in
let sum_acc = fun (xs, a) -> case xs | [] => a | y :: t => sum_acc((t, a + y)) end in
let sign = fun n -> if n > 0 then 1 else 0 in

theorem app_nil = forall xs: [Int] -> xs @ [] == xs proof
induction xs
| [] => from [] @ [] do step end; rewrite refl end
| x :: t => from (x :: t) @ [] do step end; from t @ [] do rewrite ih_t end end; rewrite refl end
end
in

theorem app_nil_fun = (fun ys: [Int] -> ys @ []) == (fun ys: [Int] -> ys) proof
funext ys =>
revert app_nil with xs = ys =>
assume ys @ [] == ys =>
from ys at 2 do rewrite <- assume end end;
rewrite refl end
in

theorem shuffle = forall s: Int -> forall a: Int -> forall y: Int -> (y + s) + a == s + (a + y) proof
from (y + s) + a do algebra s + (a + y) end end; rewrite refl end
in

theorem acc = forall xs: [Int] -> forall a: Int -> sum_acc((xs, a)) == sum(xs) + a proof
generalize a =>
induction xs
| [] => forall a =>
from sum_acc(([], a)) do step end; from sum([]) do step end;
from 0 + a do algebra a end end; rewrite refl end
| y :: t => forall a =>
from sum_acc((y :: t, a)) do step end; from sum(y :: t) do step end;
from (y + sum(t)) + a do rewrite shuffle end end;
from sum(t) + (a + y) do rewrite <- ih_t with a = a + y end end; rewrite refl end
end
in

theorem div_pos = forall z: Int -> z > 0 ==> 8 / z == 8 / z proof
assume pos = z > 0 =>
have z != 0 proof rewrite nonzero_of_pos with a = z end =>
rewrite refl end
in

theorem div_neg = forall z: Int -> z < 0 ==> 8 / z == 8 / z proof
assume z < 0 =>
have nz = z != 0 proof rewrite nonzero_of_neg end =>
rewrite refl end
in

theorem sum_three = forall x: Int -> forall y: Int -> x == 1 ==> y == 2 ==> x + y == 3 proof
assume x == 1 => alias hx = assume => assume y == 2 =>
from x do rewrite hx end end; from y do rewrite assume end end;
from 1 + 2 do step end; step
in

theorem sign_pos = forall n: Int -> n > 0 ==> sign(n) == 1 proof
assume n > 0 =>
from sign(n) do step end;
cases n > 0
| true => from if true then 1 else 0 do step end; step
| false => revert case_eq => from n > 0 do rewrite assume end end; from true == false do step end; rewrite false_impl end
end
in

theorem sign_le = forall n: Int -> sign(n) <= 1 proof
from sign(n) do step end;
cases n > 0
| true => from if true then 1 else 0 do step end; step
| false => from if false then 1 else 0 do step end; step
end
in
¿
19 changes: 19 additions & 0 deletions hazel-programs/docs/reference/prover-side-conditions.hz
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SIDE CONDITIONS #

# Some steps hold only under a condition: 8 / z needs z != 0. The #
# checker records each condition, and discharges it from the facts #
# in scope or by evaluation. One it can't discharge stays open: the #
# theorem reads incomplete, 1 open, and the step's row has a red #
# circle. Click the circle to see the condition and ways to add it. #
theorem div_open = forall z: Int -> 8 / z == 8 / z proof rewrite refl end in

# For a statement P ==> Q, assume h = P => makes P a fact named h #
# for the rest of the proof. have h = P proof ... => proves a fact #
# on the way, here with the builtin rule nonzero_of_pos. Without a #
# name, the facts are called assume and have. #
theorem div_ok = forall z: Int -> z > 0 ==> 8 / z == 8 / z proof
assume pos = z > 0 =>
have nz = z != 0 proof rewrite nonzero_of_pos end =>
rewrite refl end
in
¿
24 changes: 24 additions & 0 deletions hazel-programs/docs/reference/prover-stepper.hz
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# THE STEPPER #

# A proof is a chain of goals. It starts at the statement, and each #
# step replaces the goal with one that is enough to prove it, until #
# the goal is true. The stepper draws the chain, one row per goal, #
# and underlines the part of each goal that the next step acts on. #

# The stepper and the proof text are two views of one proof: editing #
# either changes the other. You can write a proof in the stepper and #
# only read the text it writes. #

# An unfinished proof ends in a write box. Click the box in the #
# stepper for try_it below, then: #
# Tab takes the next evaluation step, shown in grey #
# type the goal you want next, such as 21 == 21, and press #
# Enter: a search finds the steps that get there #
# select part of the goal, such as 3 + 4, to work on just that #
# part #
# The tabs above the box offer more: Step marks each part of the #
# goal that can be evaluated, and Strategy writes the structured #
# steps of the later slides. #

theorem try_it = (1 + 2) * (3 + 4) == 21 proof ¿ in
¿
30 changes: 30 additions & 0 deletions hazel-programs/docs/reference/prover-theorems.hz
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# THEOREMS #

# A theorem states a Bool and proves it: #
# theorem name = statement proof steps in body #
# Proofs are checked as the program runs. Below the program, a #
# stepper shows each theorem's proof, with its status in the header: #
# proven the goal reached true #
# refuted the goal reached false, so the statement is wrong #
# incomplete the proof stops before either #

# from 2 * 3 do step end evaluates the 2 * 3 inside the goal. The #
# last step evaluates 6 == 6 to true. #
theorem six = 2 * 3 == 6 proof
from 2 * 3 do step end;
step
in

# The same steps show that this statement is false. #
theorem seven = 2 * 3 == 7 proof
from 2 * 3 do step end;
step
in

# An empty proof, drawn as a small circle, is one still to write. The #
# next slide fills one in. #
theorem eight = 2 * 4 == 8 proof ¿ in

# Proofs in a theorem's body can use it by name, as the slide on #
# lemmas shows. #
¿
2 changes: 1 addition & 1 deletion src/CLI/Cli.re
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ let probe_hazel = (auto: bool, many: bool, path: string): unit => {
root_id,
syntax.term_data,
syntax.terms,
syntax.measured,
Haz3lcore.CachedSyntax.measured(syntax),
info_map,
)
) {
Expand Down
1 change: 1 addition & 0 deletions src/CLI/Diagnostic.re
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ let warning_string = (item: Language.Warning.list_item): string =>
| TPat(NonContractive(name)) => "type " ++ name ++ " is defined as itself"
| Typ(NonContractive(name)) =>
"recursive type rec " ++ name ++ " is defined as itself"
| Exp(ForallOutsideTheorem) => "forall outside a theorem has no value"
};

/* Format a single diagnostic (error or warning) in Rust-style with source
Expand Down
3 changes: 2 additions & 1 deletion src/CLI/Run.re
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ let statics_and_elab = (exp: Exp.t): (Statics.Map.t, Exp.t) =>
let elaborate = (exp: Exp.t): Exp.t => snd(statics_and_elab(exp));

let evaluate = (exp: Exp.t): Exp.t => {
let (statics, elab) = statics_and_elab(exp);
let (result, _) =
Evaluator.evaluate(~env=Builtins.env_init, elaborate(exp));
Evaluator.evaluate(~statics, ~env=Builtins.env_init, elab);
result;
};

Expand Down
6 changes: 6 additions & 0 deletions src/docslides/Slides.re
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ let all_slides: list((string, Haz3lcore.PersistentZipper.t)) =
("Cards", [%blob "cards.hz"]),
("Probes", [%blob "probes.hz"]),
("Livelits / Builtins", [%blob "livelits-builtins.hz"]),
("Prover / Theorems", [%blob "prover-theorems.hz"]),
("Prover / The Stepper", [%blob "prover-stepper.hz"]),
("Prover / Variables and Lemmas", [%blob "prover-lemmas.hz"]),
("Prover / Cases and Induction", [%blob "prover-induction.hz"]),
("Prover / Side Conditions", [%blob "prover-side-conditions.hz"]),
("Prover / Every Step", [%blob "prover-reference.hz"]),
]
|> List.map(~f=((name, text)) =>
(name, Haz3lcore.PersistentZipper.of_slide_text(text))
Expand Down
11 changes: 11 additions & 0 deletions src/haz3lcore/CachedSyntax.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/* A segment laid out on its own: what a sub-editor renders of a region
* of the main editor's segment. */
type splice = {
segment: Segment.t,
measured: Measured.t,
projector_list: list(Id.t),
};

type t = {
old: bool,
segment: Segment.t,
Expand Down Expand Up @@ -214,3 +222,6 @@ let calculate = (z: Zipper.t, info_map, dyn_map, ~elaborated=None, old: t) => {
};
};
};

let measured = (syntax: t) => syntax.measured;
let segment = (syntax: t) => syntax.segment;
3 changes: 1 addition & 2 deletions src/haz3lcore/CompositionCore/HighLevelNodeMap.re
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ module Utils = {
| LivelitName(_)
| TyAlias(_)
| ExplicitNonlabel
| Theorem(_, _, _)
| ProofObject(_)
| Theorem(_, _, _, _)
| Forall(_, _)
| Projector(_, _)
| Var(_)
Expand Down
Loading
Loading