Skip to content

Instantly share code, notes, and snippets.

function power(x: int, y: int) : int
requires y >= 0
decreases y
{
if y == 0 then 1
else if y % 2 == 0 then power(x * x, y / 2)
else x * power(x * x, (y - 1) / 2)
}
lemma PowerOddLemma(x: int, y: int)
@rdivyanshu
rdivyanshu / Nats.dfy
Last active August 7, 2024 17:40
Streams & Unique Fixed Points
codatatype Stream = Cons(head: nat, tail: Stream)
function Upwards(n: nat): Stream {
Cons(n, Upwards (n + 1))
}
function Nats() : Stream {
Upwards(0)
}
@rdivyanshu
rdivyanshu / OC588.lean
Last active May 24, 2024 04:55
OC588.lean
import Mathlib.Algebra.Ring.Defs
import Mathlib.Data.Finite.Defs
import Mathlib.Data.Set.Finite
import Mathlib.Data.Nat.Lattice
import Mathlib.Tactic
theorem crux_oc588 {R: Type*} [Ring R] [Finite R]
(a b : R) (h : (a * b - 1) * b = 0) : b * (a * b - 1) = 0 := by
let s := { b ^ k | k : ℕ }
From RecordUpdate Require Import RecordUpdate.
From stdpp Require Import gmap.
From TLA Require Import logic.
Module OneBit.
Inductive pc := ncs | wait | w2 | w3 | w4 | cs | exit.
Inductive threads := t0 | t1.
@rdivyanshu
rdivyanshu / timer.dfy
Last active January 31, 2024 09:48
Timer
datatype State = State(min: int)
predicate Init(s: State){
s.min == 5
}
predicate EnabledDecreaseMin(s: State){
s.min > 0
}
@rdivyanshu
rdivyanshu / instance.lp
Last active November 29, 2023 11:20
Haunted puzzles (by KrazyDad) solved in ASP
#const n = 4.
% count(X, Y, N) - Number of ghoulish fiends (N) visible from X, Y.
% Grid is extended to include 0th and (n + 1)th row as well as 0th and (n + 1)th column
count(0, 1, 0). count(0, 2, 2). count(0, 3, 2). count(0, 4, 3).
count(1, 0, 2). count(2, 0, 1). count(3, 0, 2). count(4, 0, 0).
count(5, 1, 1). count(5, 2, 1). count(5, 3, 1). count(5, 4, 2).
count(1, 5, 1). count(2, 5, 1). count(3, 5, 2). count(4, 5, 1).
% mirror(X, Y, T) - There is mirror of type T on X, Y.
@rdivyanshu
rdivyanshu / bm.rkt
Created October 16, 2023 13:00
Bounded Model Checking
#lang rosette/safe
(output-smt "/tmp/rosette")
(define-symbolic x (bitvector 4))
(define (range x)
(if (zero? x)
(list)
(append (range (sub1 x)) (list x))))
@rdivyanshu
rdivyanshu / hat.rkt
Last active May 16, 2024 08:15
Hat tiling
#lang racket
(require metapict)
(require metapict/pict)
(struct M-edge (turn) #:transparent)
(struct X+ M-edge () #:transparent)
(struct X- M-edge () #:transparent)
(struct A+ M-edge () #:transparent)
@rdivyanshu
rdivyanshu / cubes.jpeg
Last active August 6, 2023 05:48
Generative Art
cubes.jpeg
% square size
#const n = 5.
% number of partition
#const m = 5.
% sum of each partition
#const sm = (n * (n + 1) * (2 * n + 1)) / (6 * m).
% given(N, X, Y) - there is N at (X, Y).
given(4, 1, 2). given(3, 1, 5).
given(5, 5, 1). given(5, 5, 4).