Skip to content

Instantly share code, notes, and snippets.

View tmoux's full-sized avatar

Timothy Mou tmoux

View GitHub Profile
import Mathlib.Util.AtomM
import Mathlib.Data.Nat.Basic
open Lean Elab Meta Tactic Mathlib.Tactic
open Lean.Expr
namespace Monoid
structure Context where
α : Expr -- The type of the expression
univ : Level -- The universe level of α
import Mathlib.Util.AtomM
import Mathlib.Data.Nat.Basic
open Lean Elab Meta Tactic Mathlib.Tactic
open Lean.Expr
-- Monoid simplifier tactic
-- Normalize expressions by flattening to a list of atoms and eliminating identity elements.
namespace Monoid
@tmoux
tmoux / Main.hs
Created February 4, 2024 01:40
Template Haskell STLC
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Term
import TH
idf :: Term Ty
idf = $$(typecheck (lam "x" Base (var "x")))
-- Invalid: unbound variable
@tmoux
tmoux / P.hs
Created January 26, 2024 04:23
Typesafe rewriting patterns example
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Data.Rewriting.P where
(*|
==========================================
Verifying Programming Contest Problems
==========================================
In programming contests, one of the most frustrating (and common!) verdicts to receive is Wrong Answer (WA).
One of the reasons behind this is that there are often many potential sources of WA that a programmer has to hunt down.
A WA could come from a typo in the implementation, or it could come from the use of a greedy algorithm which is not always correct, or many other reasons.
In this tutorial, I will try to give a brief overview of how you can *prove your programs correct* by writing specifications and proofs in the Coq proof assistant.
@tmoux
tmoux / stlc.v
Last active July 10, 2023 00:40
Practicing logical relations type safety proof
Require Import Unicode.Utf8.
Require Import Relations.Relation_Operators.
Require Import List.
Require Import Autosubst.Autosubst.
(* We present a simple proof of type safety for STLC using logical relations.
We use the Autosubst library to handle binding/substitutions.
It also makes it easy to state and prove details about substitutions in the fundamental lemma. *)
Inductive type : Type :=
module Mwe where
open import Relation.Binary.PropositionalEquality as Eq using (_≡_; refl; cong)
data Type : Set where
⊤ : Type
_⇒_ : Type → Type → Type
infix 30 _⇒_
data Context : Set where
@tmoux
tmoux / red-blue-beans.ml
Created December 7, 2022 02:20
Extracted solution from Coq for the problem https://codeforces.com/contest/1519/problem/A
(** val add : int64 -> int64 -> int64 **)
let rec add = Int64.add
(** val mul : int64 -> int64 -> int64 **)
let rec mul = Int64.mul
module Nat =
struct
@tmoux
tmoux / testcut.wyv
Created August 9, 2021 23:55
expressiveness of cut rule
name C {z =>
type R <= Int
}
name D {z =>
type R <= Top
}
subtype D {type R <= Int} <: C
//D is some conditional subtype of C