Skip to content

Instantly share code, notes, and snippets.

View ghulette's full-sized avatar

Geoff Hulette ghulette

  • AWS S3-ARG
  • San Francisco, CA
View GitHub Profile
@ghulette
ghulette / ProductCat.hs
Created October 19, 2010 22:07
Product category (sort of)
-- Product category instance
import Prelude hiding ((.),id)
import Control.Category
data Product c d x y = Product (c x y) (d x y)
deriving (Eq,Show)
instance (Category c,Category d) => Category (Product c d) where
id = Product id id
mapAll :: (a -> Maybe a) -> [a] -> Maybe [a]
mapAll _ [] = Just []
mapAll f (x:xs) = case f x of
Just x' -> case mapAll f xs of
Just xs' -> Just (x':xs')
Nothing -> Nothing
Nothing -> Nothing
mapOne :: (a -> Maybe a) -> [a] -> Maybe [a]
mapOne _ [] = Nothing
escapes1 :: RealFloat a => Int -> Complex a -> Bool
escapes1 i c = any ((>) 2 . magnitude) (take i ps)
where ps = iterate p (0 :+ 0)
p = \z -> c + z^(2 :: Int)
escapes2 :: RealFloat a => Int -> Complex a -> Bool
escapes2 i c = escapes2' i c (0 :+ 0)
escapes2' :: RealFloat a => Int -> Complex a -> Complex a -> Bool
escapes2' 0 _ _ = False
module Strategy where
type Strategy m a = a -> m (Maybe a)
success :: Monad m => Strategy m a
success = return . Just
failure :: Monad m => Strategy m a
failure _ = return Nothing
@ghulette
ghulette / gist:7666263
Created November 26, 2013 21:06
This crashes Coq 8.4pl2...
Require Import Le.
(* We shouldn't really need the return clause here, but Coq 8.4pl2
will crash without it... *)
Definition test (i : nat) (x : {n : nat | n <= i}) : nat :=
match x with
| exist O _ => 0
| exist (S x') p =>
match i (* return (S x' <= i) -> nat *) with
type value = int
type ptr = value ref
type st = unit
type 'a imp =
| New of value * (ptr -> 'a imp)
| Read of ptr * (value -> 'a imp)
| Write of ptr * value * 'a imp
| While of (st -> bool) * unit imp * 'a imp
| Return of 'a
@ghulette
ghulette / backtrace.ml
Created April 25, 2017 22:16
How to get a backtrace out of ocaml
(*******************************************************************************
Getting a backtrace out of ocaml is not completely obvious. You
have to compile the code with -g and then run with OCAMLRUNPARAM=b,
like this:
$ ocamlc -g -o test test.ml
$ OCAMLRUNPARAM=b ./test
backtrace_status: true
Failure("nth")
@ghulette
ghulette / args.ml
Created May 1, 2017 17:54
Labeled and optional arguments in OCaml, by example
open Printf
(* Labeled arguments, the easy way *)
let _ =
(* Declare a function with labeled arguments like this: *)
let f ~x ~y = x + y in
(* Type: f : x:int -> y:int -> int *)
(* Call it like this: *)
printf "%d\n" (f ~x:5 ~y:6);
@ghulette
ghulette / Connectives.v
Created November 8, 2017 05:46
P /\ Q <-> ~(P -> ~Q)
Require Import Coq.Logic.Classical.
Theorem conj_iff :
forall P Q, P /\ Q <-> ~(P -> ~Q).
Proof.
split.
unfold not.
intros (HP,HQ) H.
apply H.
apply HP.
@ghulette
ghulette / formula.ml
Last active December 19, 2017 19:16
Using CPS
type formula =
| False
| True
| Atom of string
| Not of formula
| And of formula * formula
| Or of formula * formula
let is_lit = function
| Atom _ | Not (Atom _) -> true