Skip to content

Instantly share code, notes, and snippets.

View pchiusano's full-sized avatar

Paul Chiusano pchiusano

View GitHub Profile
@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
@pchiusano
pchiusano / threading.u
Last active October 19, 2021 01:58
Cooperative threading
> Async.pure.run 'let
List.parMap (x -> x*10) [1,2,3,4,5]
> Async.pure.run 'let
use Async put fork await empty!
tr = empty!
t1 = fork '(await tr + 1)
t2 = fork '(put 9 tr)
Async.await t1
@pchiusano
pchiusano / kit.u
Last active September 9, 2021 22:22
Distributed data structure construction kit and map reduce
unique ability Located d where
location : d a -> Location {}
unique ability Storage.Distributed d where
restore : d a -> a
save : a -> d a
saveNear : d x -> a -> d a
unique type Kit d m a
= Empty