Skip to content

Instantly share code, notes, and snippets.

@Hirrolot
Hirrolot / printf.idr
Last active February 23, 2024 12:08
Generic `printf` implementation in Idris2
data Fmt = FArg Fmt | FChar Char Fmt | FEnd
toFmt : (fmt : List Char) -> Fmt
toFmt ('*' :: xs) = FArg (toFmt xs)
toFmt ( x :: xs) = FChar x (toFmt xs)
toFmt [] = FEnd
PrintfType : (fmt : Fmt) -> Type
PrintfType (FArg fmt) = ({ty : Type} -> Show ty => (obj : ty) -> PrintfType fmt)
PrintfType (FChar _ fmt) = PrintfType fmt
@Hirrolot
Hirrolot / error.txt
Created January 18, 2022 19:43
Zig massive compile-time error
An error occurred:
/tmp/playground3425286633/play.zig:9:9: error: error is ignored. consider using `try`, `catch`, or `if`
switch (c) {
^
/tmp/playground3425286633/play.zig:23:15: note: called from here
try printf("Mr. John has * contacts in *\n", .{ 42, "New York"});
^
/tmp/playground3425286633/play.zig:22:21: note: called from here
pub fn main() !void {
^
@Hirrolot
Hirrolot / printf.zig
Created January 18, 2022 19:48
Generic `printf` implementation in Zig
const std = @import("std");
fn printf(comptime fmt: []const u8, args: anytype) anyerror!void {
const stdout = std.io.getStdOut().writer();
comptime var arg_idx: usize = 0;
inline for (fmt) |c| {
if (c == '*') {
try printArg(stdout, args[arg_idx]);
@Hirrolot
Hirrolot / CoC.ml
Last active May 26, 2024 00:04
Barebones lambda cube in OCaml
(* The syntax of our calculus. Notice that types are represented in the same way
as terms, which is the essence of CoC. *)
type term =
| Var of string
| Appl of term * term
| Binder of binder * string * term * term
| Star
| Box
and binder = Lam | Pi
@Hirrolot
Hirrolot / tagless-final.rs
Last active April 27, 2024 04:30
Tagless-final encoding of a simple arithmetic language in Rust
trait Interp {
type Repr<T>;
fn lit(i: i32) -> Self::Repr<i32>;
fn add(a: Self::Repr<i32>, b: Self::Repr<i32>) -> Self::Repr<i32>;
}
struct Eval;
impl Interp for Eval {
@Hirrolot
Hirrolot / lambda-calculus.ml
Last active January 11, 2024 23:36
A five-line lambda calculus with OCaml's polymorphic variants
let rec eval = function
| `Appl (m, n) -> (
let n' = eval n in
match eval m with `Lam f -> eval (f n') | m' -> `Appl (m', n'))
| (`Lam _ | `Var _) as t -> t
let sprintf = Printf.sprintf
(* Print a given term using De Bruijn levels. *)
let rec pp lvl = function
@Hirrolot
Hirrolot / stlc.ml
Created December 25, 2022 13:56
A two-line lambda calculus with metacircular (shallow) embedding
let lam f = f
let appl f x = f x
@Hirrolot
Hirrolot / stlc-form-meaning-hoas.ml
Last active January 11, 2024 23:35
A simply typed lambda calculus in tagless-final (HOAS)
module type Form = sig
type 'a meaning
val lam : ('a meaning -> 'b meaning) -> ('a -> 'b) meaning
val appl : ('a -> 'b) meaning -> 'a meaning -> 'b meaning
end
(* Evaluate a given term. *)
module Eval = struct
type 'a meaning = 'a
@Hirrolot
Hirrolot / stlc-form-meaning-de-bruijn.ml
Last active January 11, 2024 23:34
A simply typed lambda calculus in tagless-final (De Bruijn indices)
module type Form = sig
type ('env, 'a) meaning
val vz : ('a * 'env, 'a) meaning
val vs : ('env, 'a) meaning -> ('b * 'env, 'a) meaning
val lam : ('a * 'env, 'b) meaning -> ('env, 'a -> 'b) meaning
val appl :
('env, 'a -> 'b) meaning -> ('env, 'a) meaning -> ('env, 'b) meaning
end
@Hirrolot
Hirrolot / untyped-hoas.ml
Last active January 11, 2024 23:32
Untyped lambda calculus with HOAS and De Bruijn levels
type term = Lam of (term -> term) | Appl of term * term | FreeVar of int
let unfurl lvl f = f (FreeVar lvl)
let unfurl2 lvl (f, g) = (unfurl lvl f, unfurl lvl g)
let rec print lvl =
let plunge f = print (lvl + 1) (unfurl lvl f) in
function
| Lam f -> "(λ" ^ plunge f ^ ")"
| Appl (m, n) -> "(" ^ print lvl m ^ " " ^ print lvl n ^ ")"