Skip to content

Instantly share code, notes, and snippets.

View kfl's full-sized avatar
🤓
Happy

Ken Friis Larsen kfl

🤓
Happy
View GitHub Profile
@kfl
kfl / eval.fsx
Last active January 13, 2022 11:04
A simple monadic evaluator in F# with various monads
// Working with various monads in F#
type OptionBuilder() =
member _.Bind(a, f) = Option.bind f a
member _.Return a = Some a
member _.ReturnFrom a = a
member _.Zero () = None
member _.Combine (a, b) = Option.orElseWith b a
member _.Delay f = f
@kfl
kfl / README.md
Last active March 5, 2020 08:02
How use the WebAssembly reference interpreter to test WASM modules

Setup

You have some WASM code in fact.wat that you want to test.

You have installed the WebAssembly reference interpreter, and can call it as wasm (or <some_path>/wasm).

@kfl
kfl / module1.wat
Last active April 16, 2023 23:36
Javascript WebAssembly multiple modules
(module
(import "shared" "memory" (memory 1))
(import "shared" "table" (table 1 anyfunc))
(elem (i32.const 0) $read1) ;; set table[0] to read1 for indirect calling
(func $read1 (result i32)
i32.const 4
i32.load)
(func $read0 (result i32)
{-# LANGUAGE GADTs, ScopedTypeVariables #-}
{-# LANGUAGE PolyKinds, KindSignatures #-}
{-# LANGUAGE DataKinds, TypeFamilies, TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
data Nat = Z | S Nat
type family (n :: Nat) :+ (m :: Nat) :: Nat
type instance Z :+ m = m
type instance (S n) :+ m = S (n :+ m)
@kfl
kfl / fact.wat
Created February 28, 2019 08:47
fact function in WebAssembly
(module
(export "fact" (func $fact))
(func $fact (param $n i32) (result i32)
(local $result i32)
(set_local $result (i32.const 1))
(if (get_local $n)
(loop $start
(set_local $result (i32.mul (get_local $n) (get_local $result)))
(tee_local $n (i32.sub (get_local $n) (i32.const 1)))
@kfl
kfl / simpleallocate.wat
Last active March 5, 2020 08:32
Simple WASM allocator
(module $simpleallocate
(global $ALIGN_MASK i32 (i32.const 7))
(global $MAX_SIZE_32 i32 (i32.const 1073741824))
(global $PAGE_SIZE i32 (i32.const 65536))
(global $next_free (mut i32) (i32.const 0))
(func $bytes_to_pages (param $n i32) (result i32)
(i32.add (i32.div_u (local.get $n)
(global.get $PAGE_SIZE))
(i32.const 1)))
@kfl
kfl / evenodd.wat
Last active February 22, 2019 16:23
even/odd in flat and sexp style
(module $evenodd
(export "even" (func $even))
(export "odd" (func $odd))
(func $even (param $n i32) (result i32)
get_local $n
i32.const 0
i32.eq
if (result i32)
i32.const 1
else
@kfl
kfl / expr.idr
Last active March 30, 2017 13:11
Evaluation of expressions in Idris
data Expr : Type -> Type where
Con : Int -> Expr Int
Add : Expr Int -> Expr Int -> Expr Int
Mult : Expr Int -> Expr Int -> Expr Int
IsZ : Expr Int -> Expr Bool
If : Expr Bool -> Expr t -> Expr t -> Expr t
And : Expr Bool -> Expr Bool -> Expr Bool
Or : Expr Bool -> Expr Bool -> Expr Bool
Tuple : Expr t -> Expr s -> Expr (t,s)
Fst : Expr(t,s) -> Expr t
@kfl
kfl / keybase.md
Created September 22, 2014 17:51

Keybase proof

I hereby claim:

  • I am kfl on github.
  • I am kfl (https://keybase.io/kfl) on keybase.
  • I have a public key whose fingerprint is 3F6E 48E7 2054 C9F6 B5D8 50D9 8A74 5E7A 434F 54BF

To claim this, I am signing this object:

@kfl
kfl / mosml.rb
Created September 10, 2014 17:26
Moscow ML homebrew formula
require "formula"
class Mosml < Formula
homepage "http://mosml.org"
url "https://github.com/kfl/mosml/archive/ver-2.10.1.tar.gz"
sha1 "e53fa82074d1c60499dc4ca83521c229e0655ccc"
depends_on "gmp"
def install