Last active
August 28, 2024 14:11
-
-
Save jmitchell/b245155ea9e2dca8e00d890f1357143c to your computer and use it in GitHub Desktop.
Basic tuples in Coq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Require Import Vector. | |
Import VectorNotations. | |
Set Implicit Arguments. | |
Inductive tuple : forall {n : nat}, Vector.t Set n -> Type := | |
| unit : tuple [] | |
| push : forall {T : Set} (x : T) | |
{n : nat} {ts : Vector.t Set n}, | |
tuple ts -> tuple (T :: ts). | |
Check unit. | |
Check push true unit. | |
Check push 5 (push true unit). | |
Module TupleNotations. | |
Notation "(| |)" := unit (format "(| |)") : tuple_scope. | |
Notation "(| x |)" := (push x unit) : tuple_scope. | |
Notation "(| x1 , .. , xn |)" := | |
(push x1 .. (push xn unit) .. ) : tuple_scope. | |
End TupleNotations. | |
Import TupleNotations. | |
Open Scope tuple_scope. | |
Example unitTupleNotation: (||) = unit. | |
Proof. reflexivity. Qed. | |
Example oneElementTupleNotation: (| false |) = push false unit. | |
Proof. reflexivity. Qed. | |
Example manyElementTupleNotation: (| 5, true |) = push 5 (push true unit). | |
Proof. reflexivity. Qed. | |
Definition length : forall {n:nat} {ts:Vector.t Set n}, tuple ts -> nat := | |
fun n _ _ => n. | |
Example lengthOfUnitIsZero: length (||) = 0. | |
Proof. reflexivity. Qed. | |
Example lengthOfTwoElementTupleIsTwo: length (| 5, true |) = 2. | |
Proof. reflexivity. Qed. | |
Definition typeSig : forall {n:nat} {ts:Vector.t Set n}, | |
tuple ts -> Vector.t Set n := | |
fun _ ts _ => ts. | |
Example complexTypeSig: typeSig (|true, Some false, 5|) = | |
[bool; option bool; nat]. | |
Proof. reflexivity. Qed. | |
Close Scope tuple_scope. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
do these not come built in?