Skip to content

Instantly share code, notes, and snippets.

View pchiusano's full-sized avatar

Paul Chiusano pchiusano

View GitHub Profile
@pchiusano
pchiusano / .unisonConfig
Created June 15, 2021 18:09
My .unisonConfig, just in ~/.unisonConfig
# I version my full namespace tree here, it's a snapshot
# of all my current workstreams. If I need to switch
# computers, I might just push here, then pull on the
# other computer.
# `.> push` will go here.
GitUrl = "git@github.com:pchiusano/unisoncode"
# GitUrl {
#
# # Some projects I occasionally make PRs against.
-- splittable RNG type
ability Random where
nat : Nat
split : {Random} (forall g a. '{Random,g} a ->{g} a)
{{ ``Random.natIn i j`` generates a {type Nat} between ''i'' and ''j'',
not including ''j''.
If ''j'' is less than or equal to ''i'', throws an error.
@pchiusano
pchiusano / Each.u
Last active January 22, 2021 00:03
Streaming nondeterminism ability
ability Each where
each : [a] -> a
Each.toStream.handler : Request {Each} a ->{Stream a} ()
Each.toStream.handler = cases
{ a } -> Stream.emit a
{ Each.each as -> resume } -> match as with
[] -> ()
a +: as ->
handle resume a with Each.toStream.handler
@pchiusano
pchiusano / zippy.u
Last active November 11, 2020 07:25
Elementwise ("zippy") traversals ability
-- Ability to range over all the elements of a list
ability Each where
each : [a] -> a
-- Implementation detail - standard early termination ability
ability Abort where abort : x
-- Here's a first usage. `each` lets us traverse multiple lists
-- elementwise, matching up values at corresponding indices.
> handle
@pchiusano
pchiusano / splitmix.u
Last active October 20, 2020 08:34
SplitMix implementation
-- porting https://hackage.haskell.org/package/splitmix-0.1.0.1/docs/src/System.Random.SplitMix.html
-- could use a native popcount / hamming weight instruction
roll : Nat -> Nat ->{Random} Nat
roll n sides =
Nat.sum (List.replicate n '(Random.natIn sides + 1))
-- example, rolling 3 x d8
> Random.splitmix 10349 '(roll 3 8)
@pchiusano
pchiusano / wordcount.u
Last active August 6, 2020 03:20
Word count example in Unison
-- imports, not too exciting
use DSeq Empty One Two
use Storage save restore
use Mem Mem
use Remote at await
-- These are called watch expressions, any line starting with `>`
-- gets evaluated when you save the file (and cached), so you can use
-- your scratch file a bit like a spreadsheet.
> 1 + 1
@pchiusano
pchiusano / distributed.u
Last active May 19, 2020 16:56
Distributed programming API for Unison
unique ability Remote loc task result g where
at : loc -> '{Remote loc task result g, g} a -> task a
fork : '{g} a -> task a
await : task a -> result a
cancel : task a -> ()
location : task a -> loc
type Value a = Value a
Remote.local.sequential.handler : Request {Remote () Value Value g} a ->{g} a
@pchiusano
pchiusano / async.u
Last active February 13, 2020 21:37
Async ability in Unison
-- Nat is just an index into some internal store
-- provided by the handler
unique type Source a = Source Nat
unique type Sink a = Sink Nat
ability Abort where abort : a
-- You wouldn't typically program with these operations directly,
-- but you can implement `fork` in terms of these operations
@pchiusano
pchiusano / rt.hs
Created December 16, 2019 17:35
Unison runtime prototyping (by Dan Doel)
--- Code by Dan Doel - https://github.com/unisonweb/unison/issues/1055#issuecomment-565753502
{-# language BangPatterns #-}
module POC (C(..), eval0, setup) where
import Control.Monad.Primitive
import qualified Data.Vector.Generic.Mutable as GM
import qualified Data.Vector.Unboxed.Mutable as UM
@pchiusano
pchiusano / monads.u
Last active April 27, 2024 08:18
Converting between algebraic effects and monads
-- This gist shows how we can use abilities to provide nicer syntax for any monad.
-- We can view abilities as "just" providing nicer syntax for working with the
-- free monad.
ability Monadic f where
eval : f a -> a
-- Here's a monad, encoded as a first-class value with
-- two polymorphic functions, `pure` and `bind`
type Monad f = Monad (forall a . a -> f a) (forall a b . f a -> (a -> f b) -> f b)