Skip to content

Instantly share code, notes, and snippets.

@DarinM223
DarinM223 / build_sharp.sh
Last active September 23, 2023 20:44
Loads all the dependencies of a smi file and then runs the command
#!/bin/bash
cat > build.smi <<EOL
_require "basis.smi"
_require "smlnj-lib.smi"
EOL
cat > build.sml <<EOL
signature PACK_REAL = sig
type real
@DarinM223
DarinM223 / Eval.hs
Last active May 3, 2023 18:33
Example of an interpreter with recursive types using compdata
-- You need DeepSubsumption enabled in order for the example to compile.
{-# LANGUAGE DeepSubsumption #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}
module Eval where
import Control.Monad.State.Strict (StateT, evalStateT, gets, liftIO, modify', void)
import Data.Comp.Multi
@DarinM223
DarinM223 / generic-mutrec.sml
Last active June 3, 2023 15:30
Example of type indexed values using mutual recursion with multiple type variables
infix & +` *`
datatype ('a, 'b) stmt =
Assign of 'a * ('a, 'b) expr
| If of ('a, 'b) expr * ('b, 'a) stmt list * ('a, 'b) stmt list
and ('a, 'b) expr =
Stmt of ('a, 'a) stmt
| Int of 'b
| Bop of ('b, 'a) expr * ('a, 'b) expr
@DarinM223
DarinM223 / generics.mlb
Last active April 12, 2023 21:51
Standard ML generic testing
ann "milletDiagnosticsIgnore all" in
./mltonlib/com/ssh/extended-basis/unstable/basis.mlb
./mltonlib/com/ssh/generic/unstable/lib.mlb
./mltonlib/com/ssh/generic/unstable/with/generic.sml
./mltonlib/com/ssh/generic/unstable/with/eq.sml
./mltonlib/com/ssh/generic/unstable/with/type-hash.sml
./mltonlib/com/ssh/generic/unstable/with/type-info.sml
./mltonlib/com/ssh/generic/unstable/with/hash.sml
./mltonlib/com/ssh/generic/unstable/with/uniplate.sml
./mltonlib/com/ssh/generic/unstable/with/ord.sml
@DarinM223
DarinM223 / fru.sml
Created April 9, 2023 05:42
Functional Record Update in Standard ML
structure Fold =
struct
fun fold (a, f) g = g (a, f)
fun post (w, g) s =
w (fn (a, h) => s (a, g o h))
fun step0 h (a, f) =
fold (h a, f)
fun step1 h (a, f) b =
fold (h (b, a), f)
@DarinM223
DarinM223 / server.mlb
Last active August 13, 2023 01:27
Example of a TCP server using CML in MLton
$(SML_LIB)/basis/basis.mlb
$(SML_LIB)/basis/mlton.mlb
$(SML_LIB)/smlnj-lib/INet/inet-lib.mlb
$(SML_LIB)/cml/cml.mlb
server.sml
@DarinM223
DarinM223 / Log.hs
Created February 5, 2023 04:20
Logging rich messages in co-log
{-# LANGUAGE OverloadedStrings #-}
import Colog
import Control.Monad.IO.Class (MonadIO)
import Data.Text (Text)
foo :: (WithLog env Message m, MonadIO m) => m ()
foo = do
logWarning "Hello"
logWarning "World"
@DarinM223
DarinM223 / SoundEager.hs
Last active April 9, 2023 16:00
Translation of `sound_eager.ml` to Haskell https://okmij.org/ftp/ML/generalization.html
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module SoundEager where
import Control.Monad (unless)
import Control.Monad.Primitive (PrimMonad, PrimState)
import Control.Monad.ST (runST)
import Data.Char (chr, ord)
@DarinM223
DarinM223 / cyclical.rs
Last active September 13, 2022 08:03
Cyclical mutable references in Rust using GhostCell
/// An example of cyclical mutable references in Rust using GhostCell.
/// Struct A contains mutable references to B and C and B has a
/// mutable reference to C and C has a mutable reference to B.
///
/// Unfortunately, doing it this way means forgoing dot syntax for B and C, since
/// only Rc, Arc, and Pin can be used for self, not &'arena GhostCell<'id, Self>.
use bumpalo::Bump;
use ghost_cell::{GhostCell, GhostToken};
struct A<'arena, 'id> {
@DarinM223
DarinM223 / Select.hs
Last active June 16, 2022 03:53
Select for TBQueue similar to Golang's select
{-# LANGUAGE ExistentialQuantification #-}
import Control.Concurrent.STM (STM, TBQueue, atomically, isEmptyTBQueue, isFullTBQueue, newTBQueueIO, readTBQueue, retry, writeTBQueue)
import Control.Monad (filterM, replicateM_)
import System.Random (StdGen, newStdGen, uniformR)
data Handler a
= forall b. Recv (TBQueue b) (b -> IO a)
| forall b. Send (TBQueue b) b (IO a)