Skip to content

Instantly share code, notes, and snippets.

View namin's full-sized avatar

Nada Amin namin

View GitHub Profile
@brandonwillard
brandonwillard / sequence_probabilities.py
Last active November 17, 2023 23:55
Computing sequence probabilities in Outlines
import torch
import outlines.models as models
from outlines.text.generate.regex import choice
from outlines.text.generate.continuation import continuation
from outlines.text.generate.sample import greedy
def make_greedy_tracker(generator):
@kahole
kahole / index.html
Last active April 26, 2024 06:04
*scratch*.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>*scratch*</title>
<style>
body {
font-family: Hack, Menlo, Monaco, 'Droid Sans Mono', 'Courier New', monospace;
white-space: pre;

Principled Meta Programming for Scala

This note outlines a principled way to meta-programming in Scala. It tries to combine the best ideas from LMS and Scala macros in a minimalistic design.

  • LMS: Types matter. Inputs, outputs and transformations should all be statically typed.

  • Macros: Quotations are ultimately more easy to deal with than implicit-based type-lifting

  • LMS: Some of the most interesting and powerful applications of meta-programming

-- The meta-circular interpreter from section 5 of Reynolds's Definitional
-- Interpreters for Higher Order Programming Languages
-- (http://www.cs.uml.edu/~giam/91.531/Textbooks/definterp.pdf)
data EXP
= CONST Const
| VAR Var
| APPL Appl
| LAMBDA Lambda
| COND Cond
object unsoundForwardRef {
trait LowerBound[T] {
type M >: T;
}
trait UpperBound[U] {
type M <: U;
}
val bounded1 : LowerBound[Int] with UpperBound[String] = hideForwardRef
-- Here I relate problem with unrealizable contexts in μDOT with known problems
-- in dependent type theory. I use Agda for illustration.
--
-- I also discuss the viewpoint given by denotational semantics.
module SoundnessOpenTermsXiRule where
open import Relation.Binary.PropositionalEquality
open import Data.Nat
open import Data.Bool
@milessabin
milessabin / gist:c51b6851548dae403abf
Created May 9, 2014 10:11
Type safe selectDynamic without macros
miles@frege:~$ scala
Welcome to Scala version 2.11.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_55).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import scala.language.dynamics
import scala.language.dynamics
scala> case class Assoc[K, V](value: V)
defined class Assoc
@milessabin
milessabin / gist:aae285025a32fac0f5c1
Last active August 26, 2017 10:28
Trivial type safe heterogenous map using only dependent method types, singleton-typed String literal keys and implicits.
scala> trait Assoc[K] { type V ; val value: V }
defined trait Assoc
scala> def mkAssoc[V0](k: String, v: V0): Assoc[k.type] { type V = V0 } =
| new Assoc[k.type] { type V = V0 ; val value = v }
mkAssoc: [V0](k: String, v: V0)Assoc[k.type]{type V = V0}
scala> implicit def nameAssoc = mkAssoc("Name", "Mary")
nameAssoc: Assoc[String("Name")]{type V = String}
@bobatkey
bobatkey / gadts.sml
Created January 5, 2014 18:34
Encoding of GADTs in SML/NJ
(* This is a demonstration of the use of the SML module system to
encode (Generalized Algebraic Datatypes) GADTs via Church
encodings. The basic idea is to use the Church encoding of GADTs in
System Fomega and translate the System Fomega type into the module
system. As I demonstrate below, this allows things like the
singleton type of booleans, and the equality type, to be
represented.
This was inspired by Jon Sterling's blog post about encoding proofs
in the SML module system:
@jackrusher
jackrusher / heart-murmur.clj
Last active December 24, 2015 16:09
My first attempt at an aleatoric composition with Overtone. An example of the audio output is here: https://soundcloud.com/jackrusher/heart-murmur
(ns overtone-playground.heart-murmur
(:use overtone.core
overtone.inst.sampled-piano
overtone.samples.piano))
;; (boot-server)
;; (sampled-piano)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Reproducible randomness