Skip to content

Instantly share code, notes, and snippets.

View niuk's full-sized avatar

Kangyuan Niu niuk

View GitHub Profile
interface Eq require
{type T; eq}
interface Eq provide
type T : Type
eq : (T, T) -> Bool
module EqInt : Eq where
type T = Int
eq = builtIn_eqInt
@niuk
niuk / herp.cpp
Created July 20, 2011 18:27
Resource Handling, PART I
/* Say you want to read some stuff into a buffer.
* To do so in C, a (very) naive programmer would write something like: */
void *read_buf_from_file(char *path) {
void *buf = malloc(512);
int fd = open(path, O_RDONLY);
read(fd, buf, 512);
return buf;
}
@niuk
niuk / derp.cpp
Created July 20, 2011 20:02
Resource Handling, PART II
/* The Hammer code for read_buf_from_file would look like this: */
void *read_buf_from_file(char *path) {
int ret = -1;
void *buf = malloc(512) <-
lambda(buf) {
if (ret < 0) free(buf);
};
int fd = open(path, O_RDONLY) <-
lambda(fd) {
main : IO ()
open : IO Handle
write : Handle -> String -> IO ()
close : Handle -> IO ()
main = do
fd <- open "log.txt"
write fd "Hello World!"
close fd
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to localhost [::1] port 22.
debug1: Connection established.
debug1: identity file /home/test/.ssh/identity type -1
debug1: identity file /home/test/.ssh/id_rsa type 1
debug1: identity file /home/test/.ssh/id_dsa type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
@niuk
niuk / gist:1682404
Created January 26, 2012 11:46
This code will segfault.
{-# LANGUAGE
OverloadedStrings,
FlexibleInstances #-}
module Main where
import Prelude
import System.IO
@niuk
niuk / gist:1682411
Created January 26, 2012 11:48
What valgrind tells me.
==8944== Memcheck, a memory error detector
==8944== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==8944== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==8944== Command: ./Generator
==8944==
; ModuleID = 'myModule'
==8944== Invalid write of size 8
==8944== at 0xB3BB8C: ??? (in /home/karl/Haskell/Hammer/Generator)
==8944== Address 0x64ed560 is not stack'd, malloc'd or (recently) free'd
==8944==
@niuk
niuk / gist:1682420
Created January 26, 2012 11:50
This runs just fine.
#include <stdbool.h>
#include <stdio.h>
#include <llvm-c/Core.h>
#include <llvm-c/ExecutionEngine.h>
int main(int argc, char **argv) {
LLVMLinkInJIT();
if (LLVMInitializeNativeTarget()) {
fprintf(stderr, "Failed to initialize native target.\n");
@niuk
niuk / ER.hs
Created February 17, 2012 23:57
Slow as molasses.
import Data.Array
import Data.Graph
import Data.IORef
import System.Random
import Control.Monad
main = do
graphRef <- newIORef .
array (1, 1000) $ zip [1 .. 1000] (repeat [])
let f = do
execve("/usr/local/bin/perl", ["perl", "--version"], [/* 55 vars */]) = 0
brk(0) = 0x132c000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f6c0e678000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/perl5/core_perl/CORE/tls/x86_64/libperl.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/perl5/core_perl/CORE/tls/x86_64", 0x7fff3d538980) = -1 ENOENT (No such file or directory)
open("/usr/lib/perl5/core_perl/CORE/tls/libperl.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/perl5/core_perl/CORE/tls", 0x7fff3d538980) = -1 ENOENT (No such file or directory)
open("/usr/lib/perl5/core_perl/CORE/x86_64/libperl.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/perl5/core_perl/CORE/x86_64", 0x7fff3d538980) = -1 ENOENT (No such file or directory)