Skip to content

Instantly share code, notes, and snippets.

View barrucadu's full-sized avatar

Michael Walker barrucadu

View GitHub Profile
@barrucadu
barrucadu / stack.ll
Created January 13, 2014 12:00
Stack implementation in LLVM IR
; The stack pointer is going to be an index into the stack, and the
; stack is an array of words. The alternative would be to have the
; stack pointer me a pointer to memory, but this is perhaps a bit
; nicer, as where the stack actually lives is totally irrelevant.
@stack = global [1000 x i64] undef
@sp = global i64 undef;
; Now we have the basic stack operations: push, pop, and peek. As can
; be seen from the definitions, LLVM is typed, which is really nice as
@barrucadu
barrucadu / coapplicative.hs
Created March 24, 2015 23:38
Coapplicative functors in Haskell
-- Because of the type of 'from', 'Coaplicative' must correspond to "non-empty containers", for the usual hand-wavy definition of "container".
class Functor f => Coapplicative f where
from :: f a -> a
separate :: f (Either a b) -> Either (f a) (f b)
instance Coapplicative Identity where
from (Identity a) = a
separate (Identity (Left a)) = Left (Identity a)
separate (Identity (Right b)) = Right (Identity b)

Your good friend Jackson Elias calls for your help with a telegram scarce on details. Something about the ill-fated Carlyle Expedition of 1920, where everyone ended up dead. He said he's found something. You arrive at his hotel room and knock on the door, but get no response. The door is ajar. You push it open and see a man in a dark suit standing there.

"I'm here to help you," says the stranger. "You're not going to believe what I've found."

He leads you into an empty office and closes the door behind him.

You say "Where's Jackson Elias?"

"Jackson? What happened to him?"

data ThreadId = ThreadId
{ realThreadId :: Int
, exceptionVariable :: TVar (Maybe SomeException)
, canBeThrownTo :: TVar Bool
}
-- when a thread is created: canBeThrownTo depends on the masking state (True for Unmasked, False otherwise)
-- when a thread is killed: canBeThrownTo is True
---
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
class IsSubtypeOf a b where
to :: a -> b
from :: b -> Maybe a
instance IsSubtypeOf Int Int where
to = id
from = Just
@barrucadu
barrucadu / isogram.c
Created March 5, 2020 12:50
There are two different definitions of "isogram": this uses the definition "there are no duplicate characters"
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare_chars(const void* ap, const void *bp) {
char a = *((char*) ap);
char b = *((char*) bp);
@barrucadu
barrucadu / more-jmp.c
Last active February 24, 2020 16:39
clang jmp-cflow.c -Weverything -Wno-shadow -Wno-unused-variable -Wno-missing-noreturn -Wno-reserved-id-macro -Wno-unused-macros -Werror
#include <setjmp.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define BEGIN_BLOCK \
{ \
volatile uint8_t orig_exc_i = exc_i; \
volatile uint8_t exc_i = orig_exc_i + 1; \
#include <setjmp.h>
#include <stdio.h>
int main(void) {
jmp_buf buf;
int a, b;
switch(setjmp(buf)) {
case 0:
case 1:
/// Check if two items are in the same equivalence class.
fn in_class(rel: BTreeMap<EUFTerm, BTreeSet<EUFTerm>>, left: &EUFTerm, right: &EUFTerm) -> bool {
let mut seen = BTreeSet::new();
let mut todo = vec![left];
let empty_set = BTreeSet::new();
while let Some(next) = todo.pop() {
for candidate in rel.get(next).unwrap_or(&empty_set).difference(&seen) {
if *candidate == *right {
return true;
Definitions:
```
f(n) = O(g(n)) <==> \exists x_0 \in R, m \in R+, \forall x >= x_0, 0 < g(x) \land |f(x)| <= m * g(x)
```
Let `a(n) = O(b(n))`
Let `c(n) = O(d(n))`
We have: