Skip to content

Instantly share code, notes, and snippets.

@larsr
larsr / haskell_to_llvm.sh
Created January 17, 2020 14:14
Example compiling haskell to llvm IR code and then linking it with the necessary libraries to run it. (I installed ghc and llvm with brew)
cat >demo.hs <<EOF
quicksort [] = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
where
lesser = filter (< p) xs
greater = filter (>= p) xs
main = print(quicksort([5,2,1,0,8,3]))
EOF
@larsr
larsr / dotnineninenine_is_one
Last active November 22, 2019 10:16
Coq proof that 0.9999999999 = 1
(* Proof that 0.9999999999... = 1 *)
Require Import Reals.
From Coquelicot Require Import Coquelicot.
Require Import Psatz.
Definition nine i := (9/10)*(1/10)^i.
Example e1: sum_n nine 3 = 9/10 + 9/100 + 9/1000 + 9/10000.
unfold nine.
@larsr
larsr / which_answer.v
Created November 21, 2019 12:01
Logic puzzle: which answer is correct?
(**
Which answer is correct?
1. All of the below.
2. None of the below.
3. All of the above.
4. At least one of the above.
5. None of the above.
6. None of the above.
@larsr
larsr / myparser2.hs
Created July 11, 2019 21:12
parser for binop expressions
import Data.Char
import Data.Functor
import Data.List
------------------------------------------------------------
import Control.Monad
import Control.Applicative
import Data.List
newtype Parser a = Parser { parse :: String -> [(a, String)]}
@larsr
larsr / hello.py
Last active March 16, 2019 10:46
local mDNS rendez-vous in python
""" Announcing and locating a service """
import socket
import atexit
import multiprocessing
import time
import pickle
import zeroconf
@larsr
larsr / sumn.v
Created February 27, 2019 06:44
Program Fixpoint example
Require Import Arith.
Require Import Program.
Program Fixpoint sumn (n:nat) {measure n} :=
if dec (n =? 0) then 0 else n + sumn (n-1).
Next Obligation.
destruct n; simpl in H.
congruence.
auto with arith.
Defined.
@larsr
larsr / shor.py
Last active November 24, 2023 17:25
part of shor's algorithm in python
# Lars.Rasmusson@gmail.com, 2019-01-12
from numpy import *
def gcd(n, m):
n, m = min(n, m), max(n, m)
while n != 0: n, m = m % n, n
return m
def experiment(N):
@larsr
larsr / miu.v
Last active December 17, 2018 16:05
Hofstadter's MU puzzle - can the string MU be generated from MI from the four rewrite rules?
(*
Coq proof of Hofstadter's MU puzzle (https://en.wikipedia.org/wiki/MU_puzzle)
To prove that "MU" cannot be generated,
show by induction on a valid string that the number of "I"
is never a multiple of three.
Then as a corollary we see that "MU" which has zero "I" cannot be
a valid string.
Lars.Rasmusson@gmail.com, 2018-12-17
@larsr
larsr / sin_cos_exp.v
Last active October 28, 2018 19:00
Unfinished attempt at formalizing Euler's formula.
From Coquelicot Require Import Coquelicot.
Import Even.
Require Import Reals.
Require Import Psatz.
Import Complex.
Notation "x !" := (fact x) (at level 30). (* tighter than mul and div *)
Lemma is_series_cos x: is_series (fun i : nat => (-1) ^ i / INR ((2 * i) !) * x ^ (2*i)) (cos x).
@larsr
larsr / exp_c.v
Last active October 9, 2018 23:01
Coq proof that complex version of exp matches exp on real numbers.
(* Definition of exponential for complex numbers
Lars.Rasmusson@gmail.com, 2018-10-06
*)
From Coquelicot Require Import Coquelicot.
Require Import Reals.
Require Import Psatz.
Import Complex.