Skip to content

Instantly share code, notes, and snippets.

View pchiusano's full-sized avatar

Paul Chiusano pchiusano

View GitHub Profile
@pchiusano
pchiusano / instructions.md
Last active June 24, 2025 14:34
LLM context for passable Unison coding assistence. Paste this into the context of your preferred LLM to get an assistant that is more familiar with Unison syntax, its standard library, and its concurrency support.

Rules

Follow the Unison Programming Language Guide

Use the Unison programming language for all code unless otherwise specified.

Use the following procedure to assist me with writing code.

Step 1, before writing any code: confirm types and signatures

@pchiusano
pchiusano / skew.u
Created December 14, 2024 00:20
Skew K-ary tree
-- Structure: a buffer of Arity, then a list of complete trees of exponentially increasing size
type Skew a = Skew [a] [(Nat, [Skew.Tree a])]
type Skew.Tree a = Tree [a] [Skew.Tree a]
Skew.Arity = 4
Skew.cons : a -> Skew a -> Skew a
Skew.cons a = cases Skew hd spine ->
hd' = a +: hd
if List.size hd' < Arity then Skew hd' spine
@pchiusano
pchiusano / batching.u
Last active September 15, 2023 20:10
Batching transactions in durable storage
unique ability Batch where
increment : Nat -> ()
-- Like `State.transact`, but pauses the computation every `batchSize` calls to
-- `Batch.increment` and commits the work so far in one transaction, then resumes
-- the rest of the computation.
State.transact.batched : Nat -> Database -> '{Batch, Transaction, Exception} a ->{State, Exception} a
State.transact.batched batchSize db b =
go : Nat -> Request {Batch} a -> Either ('{Batch,Transaction,Exception} a) a
go acc = cases
@pchiusano
pchiusano / sequence.u
Created January 18, 2023 05:19
Sequence type in Unison supporting most operations in log_32(n) or O(1)
use data Array
structural type Sequence a
= Empty Nat
| Sequence Nat Nat (Array a) (Sequence (Array a)) (Array a)
Sequence.arity : Sequence a -> Nat
Sequence.arity = cases
Sequence.Empty arity -> arity
Sequence arity _ _ _ _ -> arity
@pchiusano
pchiusano / gen.u
Created December 8, 2022 17:04
Fancier `Gen`
unique type test.Result = Fail Failure | Ok Text
unique ability Weighted a where
bump : Nat -> ()
give : a -> ()
unique ability Gen where
generate : '{Weighted a} () -> a
Weighted.map : (a ->{g} b) -> '{Weighted a, g} r -> '{Weighted b,g} r
@pchiusano
pchiusano / updating.md
Last active December 14, 2022 17:10
Updating a dependency in Unison

Here's a transcript of me updating a dependency in Unison. We're going to make this more streamlined, but this is the process for now. First, I made a copy of the branch I was updating (in this case main of distributed):

.distributed> fork main topics.baseupdate

  Done.

Next I pulled the new version of base into lib.base_new:

@pchiusano
pchiusano / suffixtree.u
Created November 22, 2022 23:01
Suffix tree prototyping
-- id is the doc id, a is the text type, v is the value type
unique type Trie id txt
= Remainder txt (Trie.Pos id)
| Branch Nat (Dataset Value (Trie.Pos id)) (Map txt (Value (Trie id txt)))
--Remainder id txt Nat
-- there can be multiple docs at this suffix
-- Branch size
--| Branch Nat (Dataset Value id) (Map txt (Value (Trie id txt)))
unique type Trie.Pos id = Pos id Nat
@pchiusano
pchiusano / quickselect.u
Created October 13, 2021 19:32
Quickselect in Unison
{{
``List.selectBy o k as`` yields the same results as ``List.at k sorted``
where ''sorted'' is a sorted version of ''as'', but in (expected) linear time
without fully sorting the list.
```
List.selectBy Universal.ordering 2 [4,1,3,2,5]
```
```
@pchiusano
pchiusano / value.u
Created September 30, 2021 20:10
remote value type in Unison
{{
``task.pure a`` creates a task that returns ''a'' on {Remote.await}.
That is, {{docExample 1 '(a -> Remote.await (task.pure a) === a) }}
}}
Remote.task.pure : a -> t a
Remote.task.pure a =
t = Remote.empty!
task.complete (Right a)
t
@pchiusano
pchiusano / split.u
Last active September 23, 2021 15:15
Nondeterminism ability
structural ability Split where
skip! : x
both : a -> a -> a
Split! : [a] ->{Split} a
Split! = cases
[] -> skip!
[a] -> a
as ->
(l,r) = halve as