Systems and
Formalisms Lab

Week 9: Subset types

Author

Adam Chlipala, with modifications by CS-428 staff.

License

No redistribution allowed (usage by permission in CS-428).

So far, we have seen many examples of what we might call "classical program verification." We write programs, write their specifications, and then prove that the programs satisfy their specifications. The programs that we have written in Coq have been normal functional programs that we could just as well have written in Haskell or ML. In this lecture, we start investigating uses of dependent types to integrate programming, specification, and proving into a single phase.

A few programs to puzzle over

Inductive tree :=
| Leaf (* an empty tree *)
| Node (d : nat) (l r : tree).

Fixpoint bst (tr : tree) (s : nat -> Prop) : Prop :=
  match tr with
  | Leaf => forall x, not (s x)
  | Node d l r =>
      s d /\
        bst l (fun x => s x /\ x < d) /\
        bst r (fun x => s x /\ d < x)
  end.

Definition bool_tf_p :=
  forall b: bool, b = true \/ b = false.
bool_tf_p : Prop
Definition bool_if_s := forall b: bool, if b then nat else string.
bool_if_s : Set
Definition bool_if: bool_if_s := fun b: bool => match b with | true => 1 | false => "one" end.
bool_rec : forall P : bool -> Set, P true -> P false -> forall b : bool, P b
Definition bool_if_rect: bool_if_s := bool_rec (fun b => if b then nat else string) 1 "one". Definition read (mode: string): match mode with | "r" => string | "rb" => list Byte.byte | _ => unit end := match mode with | "r" => "Hi!" | "rb" => [Byte.x48; Byte.x69; Byte.x21] | _ => tt end. Module Heterogeneous. Inductive type : Set := | Nat : type | Bool : type | Prod : type -> type -> type. Inductive exp : type -> Set := | NConst : nat -> exp Nat | Plus : exp Nat -> exp Nat -> exp Nat | Eq : exp Nat -> exp Nat -> exp Bool | BConst : bool -> exp Bool | And : exp Bool -> exp Bool -> exp Bool | If : forall {t}, exp Bool -> exp t -> exp t -> exp t | Pair : forall {t1 t2}, exp t1 -> exp t2 -> exp (Prod t1 t2) | Fst : forall {t1 t2}, exp (Prod t1 t2) -> exp t1 | Snd : forall {t1 t2}, exp (Prod t1 t2) -> exp t2. Fixpoint typeDenote (t : type) : Set := match t with | Nat => nat | Bool => bool | Prod t1 t2 => typeDenote t1 * typeDenote t2 end%type.
= (nat * bool)%type : Set
Fixpoint expDenote {t} (e : exp t) : typeDenote t := match e with | NConst n => n | Plus e1 e2 => expDenote e1 + expDenote e2 | Eq e1 e2 => if PeanoNat.Nat.eq_dec (expDenote e1) (expDenote e2) then true else false | BConst b => b | And e1 e2 => expDenote e1 && expDenote e2 | If e' e1 e2 => if expDenote e' then expDenote e1 else expDenote e2 | Pair e1 e2 => (expDenote e1, expDenote e2) | Fst e' => fst (expDenote e') | Snd e' => snd (expDenote e') end.
= 3 : typeDenote Nat
= true : typeDenote Bool
= (1, false) : typeDenote (Prod Nat Bool)
End Heterogeneous.

2 <> 3

2 <> 3

2 = 3 -> False
H: 2 = 3

False
H: 2 = 3

match 2 with | 2 => False | _ => True end
(* pattern 2. set (fun _ => _) as f. *)
H: 2 = 3

True
(* subst f. cbv beta. cbv iota. *) auto. Qed.
False_rec : forall P : Set, False -> P
eq_rec : forall (A : Type) (x : A) (P : A -> Set), P x -> forall y : A, x = y -> P y

Introducing Subset Types

Let us consider several ways of implementing the natural-number-predecessor function. We start by displaying the definition from the standard library:

Nat.pred = fun n : nat => match n with | 0 => n | S u => u end : nat -> nat Arguments Nat.pred n%nat_scope

We can use a new command, Extraction, to produce an OCaml version of this function.

(** val pred : nat -> nat **) let pred n = match n with | O -> n | S u -> u

Returning 0 as the predecessor of 0 can come across as somewhat of a hack. In some situations, we might like to be sure that we never try to take the predecessor of 0. We can enforce this by giving pred a stronger, dependent type.


0 > 0 -> False

0 > 0 -> False
lia. Qed. Definition pred_strong1 {n : nat} : n > 0 -> nat := match n with | O => fun pf : 0 > 0 => match zgtz pf with end | S n' => fun _ => n' end.

We expand the type of pred to include a proof that its argument n is greater than 0. When n is 0, we use the proof to derive a contradiction, which we can use to build a value of any type via a vacuous pattern match. When n is a successor, we have no need for the proof and just return the answer. The proof argument can be said to have a dependent type, because its type depends on the value of the argument n.

Coq's Compute command can execute particular invocations of pred_strong1 just as easily as it can execute more traditional functional programs.


2 > 0

2 > 0
lia. Qed.
= 1 : nat

One aspect in particular of the definition of pred_strong1 may be surprising. We took advantage of Definition's syntactic sugar for defining function arguments in the case of n, but we bound the proofs later with explicit fun expressions. Let us see what happens if we write this function in the way that at first seems most natural.

The command has indeed failed with message: In environment n : nat pf : n > 0 The term "pf" has type "n > 0" while it is expected to have type "0 > 0".

The term zgtz pf fails to type-check. Somehow the type checker has failed to take into account information that follows from which match branch that term appears in. The problem is that, by default, match does not let us use such implied information. To get refined typing, we must always rely on match annotations, either written explicitly or inferred.

In this case, we must use a return annotation to declare the relationship between the value of the match discriminee and the type of the result. There is no annotation that lets us declare a relationship between the discriminee and the type of a variable that is already in scope; hence, we delay the binding of pf, so that we can use the return annotation to express the needed relationship.

We are lucky that Coq's heuristics infer the return clause (specifically, return n > 0 -> nat) for us in the definition of pred_strong1, leading to the following elaborated code:

Definition pred_strong1' (n : nat) : n > 0 -> nat :=
  match n return n > 0 -> nat with
  | O => fun pf : 0 > 0 => match zgtz pf with end
  | S n' => fun _ => n'
  end.

By making explicit the functional relationship between value n and the result type of the match, we guide Coq toward proper type checking. The clause for this example follows by simple copying of the original annotation on the definition. In general, however, the match annotation inference problem is undecidable. The known undecidable problem of higher-order unification reduces to the match type inference problem. Over time, Coq is enhanced with more and more heuristics to get around this problem, but there must always exist matches whose types Coq cannot infer without annotations.

Let us now take a look at the OCaml code Coq generates for pred_strong1.

(** val pred_strong1 : nat -> nat **) let pred_strong1 = function | O -> assert false (* absurd case *) | S n' -> n'

The proof argument has disappeared! We get exactly the OCaml code we would have written manually. This is our first demonstration of the main technically interesting feature of Coq program extraction: proofs are erased systematically.

We can reimplement our dependently typed pred based on subset types, defined in the standard library with the type family sig.

Inductive sig (A : Type) (P : A -> Prop) : Type := exist : forall x : A, P x -> {x : A | P x}. Arguments sig [A]%type_scope P%type_scope Arguments exist [A]%type_scope P%function_scope x _

We rewrite pred_strong1, using some syntactic sugar for subset types, after we deactivate some clashing notations for set literals.

Notation "{ x : A | P }" := (sig (fun x => P)) : type_scope (default interpretation)
Definition pred_strong2 (s : {n : nat | n > 0} ) : nat := match s with | exist _ O pf => match zgtz pf with end | exist _ (S n') _ => n' end.

To build a value of a subset type, we use the exist constructor, and the details of how to do that follow from the output of our earlier Print sig command, where we elided the extra information that parameter A is implicit.

= 1 : nat
(** val pred_strong2 : nat -> nat **) let pred_strong2 = function | O -> assert false (* absurd case *) | S n' -> n'

We arrive at the same OCaml code as was extracted from pred_strong1, which may seem surprising at first. The reason is that a value of sig is a pair of two pieces, a value and a proof about it. Extraction erases the proof, which reduces the constructor exist of sig to taking just a single argument. An optimization eliminates uses of datatypes with single constructors taking single arguments, and we arrive back where we started.

We can continue on in the process of refining pred's type. Let us change its result type to capture that the output is really the predecessor of the input.

Definition pred_strong3
  (s : {n : nat | n > 0}) : {m : nat | proj1_sig s = S m}
  :=
  match s return {m : nat | proj1_sig s = S m} with
  | exist _ 0 pf => match zgtz pf with end
  | exist _ (S n') pf => exist _ n' (eq_refl _)
  end.

= exist (fun m : nat => proj1_sig (exist (lt 0) 2 two_gt0) = S m) 1 eq_refl : {m : nat | proj1_sig (exist (lt 0) 2 two_gt0) = S m}

A value in a subset type can be thought of as a dependent pair (or sigma type) of a base value and a proof about it. The function proj1_sig extracts the first component of the pair. It turns out that we need to include an explicit return clause here, since Coq's heuristics are not smart enough to propagate the result type that we wrote earlier.

By now, the reader is probably ready to believe that the new pred_strong leads to the same OCaml code as we have seen several times so far, and Coq does not disappoint.

(** val pred_strong3 : nat -> nat **) let pred_strong3 = function | O -> assert false (* absurd case *) | S n' -> n'

We have managed to reach a type that is, in a formal sense, the most expressive possible for pred. Any other implementation of the same type must have the same input-output behavior. However, there is still room for improvement in making this kind of code easier to write. Here is a version that takes advantage of tactic-based theorem proving. We switch back to passing a separate proof argument instead of using a subset type for the function's input, because this leads to cleaner code. (False_rec is a library function that can be used to produce a value in any type given a proof of False. It's defined in terms of the vacuous pattern match we saw earlier.)


forall n : nat, n > 0 -> {m : nat | n = S m}

forall n : nat, n > 0 -> {m : nat | n = S m}
n: nat
g: 0 > 0

False
n, n': nat
g: S n' > 0
S n' = S n'

We build pred_strong4 using tactic-based proving, beginning with a Definition command that ends in a period before a definition is given. Such a command enters the interactive proving mode, with the type given for the new identifier as our proof goal.

We do most of the work with the refine tactic, to which we pass a partial "proof" of the type we are trying to prove. There may be some pieces left to fill in, indicated by underscores. Any underscore that Coq cannot reconstruct with type inference is added as a proof subgoal. In this case, we have two subgoals.

We can see that the first subgoal comes from the second underscore passed to False_rec, and the second subgoal comes from the second underscore passed to exist. In the first case, we see that, though we bound the proof variable with an underscore, it is still available in our proof context. Both subgoals are easy to discharge, so let us back up and ask to prove all subgoals automatically.

  

forall n : nat, n > 0 -> {m : nat | n = S m}
refine (fun n => match n with | O => fun _ => False_rec _ _ | S n' => fun _ => exist _ n' _ end); congruence || lia. Defined.

We end the "proof" with Defined instead of Qed, so that the definition we constructed remains visible. This contrasts to the case of ending a proof with Qed, where the details of the proof are hidden afterward. (More formally, Defined marks an identifier as transparent, allowing it to be unfolded; while Qed marks an identifier as opaque, preventing unfolding.) Let us see what our proof script constructed.

pred_strong4 = fun n : nat => match n as n0 return (n0 > 0 -> {m : nat | n0 = S m}) with | 0 => fun H : 0 > 0 => False_rec {m : nat | 0 = S m} (let g0 : BinInt.Z.gt BinNums.Z0 BinNums.Z0 := ZifyClasses.rew_iff (0 > 0) (BinInt.Z.gt BinNums.Z0 BinNums.Z0) (ZifyClasses.mkrel nat BinNums.Z gt BinInt.Z.of_nat BinInt.Z.gt Znat.Nat2Z.inj_gt 0 BinNums.Z0 eq_refl 0 BinNums.Z0 eq_refl) H in let __arith : forall __p1 : Prop, BinInt.Z.gt BinNums.Z0 BinNums.Z0 -> __p1 := fun __p1 : Prop => let __wit := [] in let __varmap := VarMap.Empty in let __ff := Tauto.IMPL (Tauto.A Tauto.isProp {| RingMicromega.Flhs := EnvRing.PEc BinNums.Z0; RingMicromega.Fop := RingMicromega.OpGt; RingMicromega.Frhs := EnvRing.PEc BinNums.Z0 |} tt) None (Tauto.X Tauto.isProp __p1) in ZMicromega.ZTautoChecker_sound __ff __wit (eq_refl <: ZMicromega.ZTautoChecker __ff __wit = true) (VarMap.find BinNums.Z0 __varmap) in __arith False g0) | S n' => fun _ : S n' > 0 => exist (fun m : nat => S n' = S m) n' eq_refl end : forall {n : nat}, n > 0 -> {m : nat | n = S m} Arguments pred_strong4 {n}%nat_scope _

We see the code we entered, with some (pretty long!) proofs filled in.

= exist (fun m : nat => 2 = S m) 1 eq_refl : {m : nat | 2 = S m}

We are almost done with the ideal implementation of dependent predecessor. We can use Coq's syntax-extension facility to arrive at code with almost no complexity beyond a Haskell or ML program with a complete specification in a comment. In this lecture, we will not dwell on the details of syntax extensions; the Coq manual gives a straightforward introduction to them.

Notation "!" := (False_rec _ _).
Notation "[ e ]" := (exist _ e _).


forall n : nat, n > 0 -> {m : nat | n = S m}

forall n : nat, n > 0 -> {m : nat | n = S m}
refine (fun n => match n with | O => fun _ => ! | S n' => fun _ => [n'] end); congruence || lia. Defined.

By default, notations are also used in pretty-printing terms, including results of evaluation.

= [1] : {m : nat | 2 = S m}

Decidable Proposition Types

There is another type in the standard library that captures the idea of a program value indicating which of two propositions is true.

Inductive sumbool (A B : Prop) : Set := left : A -> {A} + {B} | right : B -> {A} + {B}. Arguments sumbool (A B)%type_scope Arguments left {A B}%type_scope _, [_] _ _ Arguments right {A B}%type_scope _, _ [_] _

We have been using this type family behind the scenes for various equality checks, for instance:

Infix "==" := string_dec (at level 70).
"x" == "y" : {"x" = "y"} + {"x" <> "y"}

Here, the constructors of sumbool have types written in terms of a registered notation for sumbool, such that the result type of each constructor desugars to sumbool A B. We can define some notations of our own to make working with sumbool more convenient.

Notation "'Yes'" := (left _ _).
Notation "'No'" := (right _ _).
Identifier 'Reduce' now a keyword

The Reduce notation is notable because it demonstrates how if is overloaded in Coq. The if form actually works when the test expression has any two-constructor inductive type. Moreover, in the then and else branches, the appropriate constructor arguments are bound. This is important when working with sumbools, when we want to have the proof stored in the test expression available when proving the proof obligations generated in the appropriate branch.

Now we can write eq_nat_dec, which compares two natural numbers, returning either a proof of their equality or a proof of their inequality.


forall n m : nat, {n = m} + {n <> m}

forall n m : nat, {n = m} + {n <> m}
refine (fix f (n m : nat) : {n = m} + {n <> m} := match n, m with | O, O => Yes | S n', S m' => Reduce (f n' m') | _, _ => No end); congruence. Defined.
= Yes : {2 = 2} + {2 <> 2}
= No : {2 = 3} + {2 <> 3}

Note that the Yes and No notations are hiding proofs establishing the correctness of the outputs.

Our definition extracts to reasonable OCaml code.

(** val eq_nat_dec : nat -> nat -> sumbool **) let rec eq_nat_dec n m = match n with | O -> (match m with | O -> Left | S _ -> Right) | S n' -> (match m with | O -> Right | S m' -> eq_nat_dec n' m')

Proving this kind of decidable equality result is so common that Coq comes with a tactic for automating it.

n, m: nat

{n = m} + {n <> m}
decide equality. Defined.

Curious readers can verify that the decide equality version extracts to the same OCaml code as our more manual version does. That OCaml code had one undesirable property, which is that it uses Left and Right constructors instead of the Boolean values built into OCaml. We can fix this, by using Coq's facility for mapping Coq inductive types to OCaml variant types.

Extract Inductive sumbool => "bool" ["true" "false"].
(** val eq_nat_dec' : nat -> nat -> bool **) let rec eq_nat_dec' n x = match n with | O -> (match x with | O -> true | S _ -> false) | S n0 -> (match x with | O -> false | S n1 -> eq_nat_dec' n0 n1)

We can build "smart" versions of the usual Boolean operators and put them to good use in certified programming. For instance, here is a sumbool version of Boolean "or."

Notation "x || y" := (if x then Yes else Reduce y).

Let us use it for building a function that decides list membership. We need to assume the existence of an equality decision procedure for the type of list elements.

Section In_dec.
  Context {A : Set}.
  Variable A_eq_dec : forall x y : A, {x = y} + {x <> y}.

The final function is easy to write using the techniques we have developed so far.

  
A: Set
A_eq_dec: forall x y : A, {x = y} + {x <> y}

forall (x : A) (ls : list A), {In x ls} + {~ In x ls}
A: Set
A_eq_dec: forall x y : A, {x = y} + {x <> y}

forall (x : A) (ls : list A), {In x ls} + {~ In x ls}
refine (fix f (x : A) (ls : list A) : {In x ls} + {~ In x ls} := match ls with | nil => No | x' :: ls' => A_eq_dec x x' || f x ls' end); simpl; intuition congruence. Defined. End In_dec.
= Yes : {In 2 [1; 2]} + {~ In 2 [1; 2]}
= No : {In 3 [1; 2]} + {~ In 3 [1; 2]}

The In_dec function has a reasonable extraction to OCaml.

(** val in_dec : ('a1 -> 'a1 -> bool) -> 'a1 -> 'a1 list -> bool **) let rec in_dec a_eq_dec x = function | Nil -> false | Cons (x', ls') -> if a_eq_dec x x' then true else in_dec a_eq_dec x ls'

This is more or less the code for the corresponding function from the OCaml standard library.

Partial Subset Types

Our final implementation of dependent predecessor used a very specific argument type to ensure that execution could always complete normally. Sometimes we want to allow execution to fail, and we want a more principled way of signaling failure than returning a default value, as pred does for 0. One approach is to define this type family maybe, which is a version of sig that allows obligation-free failure.

Inductive maybe {A : Set} (P : A -> Prop) : Set :=
| Unknown : maybe P
| Found : forall x : A, P x -> maybe P.

We can define some new notations, analogous to those we defined for subset types.

Notation "{{ x | P }}" := (maybe (fun x => P)).
Notation "??" := (Unknown _).
Notation "[| x |]" := (Found _ x _).

Now our next version of pred is trivial to write.


forall n : nat, {{m | n = S m}}

forall n : nat, {{m | n = S m}}
refine (fun n => match n return {{m | n = S m}} with | O => ?? | S n' => [|n'|] end); trivial. Defined.
= [|1|] : {{m | 2 = S m}}
= ?? : {{m | 0 = S m}}

Because we used maybe, one valid implementation of the type we gave pred_strong7 would return ?? in every case. We can strengthen the type to rule out such vacuous implementations, and the type family sumor from the standard library provides the easiest starting point. For type A and proposition B, A + {B} desugars to sumor A B, whose values are either values of A or proofs of B.

Inductive sumor (A : Type) (B : Prop) : Type := inleft : A -> A + {B} | inright : B -> A + {B}. Arguments sumor (A B)%type_scope Arguments inleft {A B}%type_scope _, [_] _ _ Arguments inright {A B}%type_scope _, _ [_] _

We add notations for easy use of the sumor constructors. The second notation is specialized to sumor`s whose `A parameters are instantiated with regular subset types, since this is how we will use sumor below.

Notation "!!" := (inright _ _).
Notation "[|| x ||]" := (inleft _ [x]).

Now we are ready to give the final version of possibly failing predecessor. The sumor-based type that we use is maximally expressive; any implementation of the type has the same input-output behavior.


forall n : nat, {m : nat | n = S m} + {n = 0}

forall n : nat, {m : nat | n = S m} + {n = 0}
refine (fun n => match n with | O => !! | S n' => [||n'||] end); trivial. Defined.
= [||1||] : {m : nat | 2 = S m} + {2 = 0}
= !! : {m : nat | 0 = S m} + {0 = 0}

As with our other maximally expressive pred function, we arrive at quite simple output values, thanks to notations.

Monadic Notations

We can treat maybe like a monad, in the same way that the Haskell Maybe type is interpreted as a failure monad. Our maybe has the wrong type to be a literal monad, but a "bind"-like notation will still be helpful.

Notation "x <- e1 ; e2" := (match e1 with
                            | Unknown _ => ??
                            | Found _ x _ => e2
                            end)
(right associativity, at level 60).

The meaning of x <- e1; e2 is: First run e1. If it fails to find an answer, then announce failure for our derived computation, too. If e1 does find an answer, pass that answer on to e2 to find the final result. The variable x can be considered bound in e2.

This notation is very helpful for composing richly typed procedures. For instance, here is a very simple implementation of a function to take the predecessors of two naturals at once.


forall n1 n2 : nat, {{p | n1 = S (fst p) /\ n2 = S (snd p)}}

forall n1 n2 : nat, {{p | n1 = S (fst p) /\ n2 = S (snd p)}}
refine (fun n1 n2 => m1 <- pred_strong7 n1; m2 <- pred_strong7 n2; [|(m1, m2)|]); intuition. Defined.

We can build a sumor version of the "bind" notation and use it to write a similarly straightforward version of this function.

Notation "x <-- e1 ; e2" := (match e1 with
                             | inright _ _ => !!
                             | inleft (exist _ x _) => e2
                             end)
(right associativity, at level 60).


forall n1 n2 : nat, {p : nat * nat | n1 = S (fst p) /\ n2 = S (snd p)} + {n1 = 0 \/ n2 = 0}

forall n1 n2 : nat, {p : nat * nat | n1 = S (fst p) /\ n2 = S (snd p)} + {n1 = 0 \/ n2 = 0}
refine (fun n1 n2 => m1 <-- pred_strong8 n1; m2 <-- pred_strong8 n2; [||(m1, m2)||]); intuition. Defined.

This example demonstrates how judicious selection of notations can hide complexities in the rich types of programs.

A Type-Checking Example

We can apply these specification types to build a certified type checker for a simple expression language.

Inductive exp :=
| Nat (n : nat)
| Plus (e1 e2 : exp)
| Bool (b : bool)
| And (e1 e2 : exp).

We define a simple language of types and its typing rules.

Inductive type := TNat | TBool.

Inductive hasType : exp -> type -> Prop :=
| HtNat : forall n,
  hasType (Nat n) TNat
| HtPlus : forall e1 e2,
  hasType e1 TNat
  -> hasType e2 TNat
  -> hasType (Plus e1 e2) TNat
| HtBool : forall b,
  hasType (Bool b) TBool
| HtAnd : forall e1 e2,
  hasType e1 TBool
  -> hasType e2 TBool
  -> hasType (And e1 e2) TBool.

It will be helpful to have a function for comparing two types. We build one using decide equality.


forall t1 t2 : type, {t1 = t2} + {t1 <> t2}
decide equality. Defined.

Another notation complements the monadic notation for maybe that we defined earlier. Sometimes we want to include "assertions" in our procedures. That is, we want to run a decision procedure and fail if it fails; otherwise, we want to continue, with the proof that it produced made available to us. This infix notation captures that idea, for a procedure that returns an arbitrary two-constructor type.

Notation "e1 ;; e2" := (if e1 then e2 else ??)
  (right associativity, at level 60).

Despite manipulating proofs, our type checker is easy to run.

= [|TNat|] : {{t | hasType (Nat 0) t}}
= [|TNat|] : {{t | hasType (Plus (Nat 1) (Nat 2)) t}}
= ?? : {{t | hasType (Plus (Nat 1) (Bool false)) t}}

The type checker also extracts to some reasonable OCaml code.

(** val typeCheck : exp -> type0 maybe **) let rec typeCheck = function | Nat _ -> Found TNat | Plus (e1, e2) -> (match typeCheck e1 with | Unknown -> Unknown | Found t1 -> (match typeCheck e2 with | Unknown -> Unknown | Found t2 -> if eq_type_dec t1 TNat then if eq_type_dec t2 TNat then Found TNat else Unknown else Unknown)) | Bool _ -> Found TBool | And (e1, e2) -> (match typeCheck e1 with | Unknown -> Unknown | Found t1 -> (match typeCheck e2 with | Unknown -> Unknown | Found t2 -> if eq_type_dec t1 TBool then if eq_type_dec t2 TBool then Found TBool else Unknown else Unknown))

We can adapt this implementation to use sumor, so that we know our type-checker only fails on ill-typed inputs. First, we define an analogue to the "assertion" notation.

Notation "e1 ;;; e2" := (if e1 then e2 else !!)
  (right associativity, at level 60).

Our new function remains easy to test:

= [||TNat||] : {t : type | hasType (Nat 0) t} + {forall t : type, ~ hasType (Nat 0) t}
= [||TNat||] : {t : type | hasType (Plus (Nat 1) (Nat 2)) t} + {forall t : type, ~ hasType (Plus (Nat 1) (Nat 2)) t}
= !! : {t : type | hasType (Plus (Nat 1) (Bool false)) t} + {forall t : type, ~ hasType (Plus (Nat 1) (Bool false)) t}

The results of simplifying calls to typeCheck' look deceptively similar to the results for typeCheck, but now the types of the results provide more information.