Skip to content

Instantly share code, notes, and snippets.

View ncfavier's full-sized avatar
🌑

Naïm Favier ncfavier

🌑
View GitHub Profile
{-# LANGUAGE LambdaCase, BlockArguments, ViewPatterns #-}
import Data.MemoTrie
count :: [Int] -> Int
count = memoFix \ go -> \case
[0, 0, 0, 0, 0] -> 1
[a, b, c, d, e] -> sum $ [go [b, c, d, e, a - n] | n <- [1,3..a]]
<> [go [e, d, c, b, a - n] | n <- [2,4..a]]
main = print $ 2 * count (replicate 5 14)
@ncfavier
ncfavier / shellwords.vim
Last active June 8, 2023 15:58
shellwords in vimscript
function! s:unbackslash(str)
return substitute(a:str, '\\\(.\)', '\1', 'g')
endfunction
" Parse a shell command line into a list of words, à la Perl's shellwords or
" Python's shlex.split.
function! s:shellwords(str)
let l:args = []
let l:len = len(a:str)
let l:i = 0
module wip.Delay where
open import 1Lab.Prelude
open import Data.Sum
open import Data.Nat
private variable
ℓ : Level
A B : Type ℓ
record Delay {ℓ} (A : Type ℓ) : Type ℓ where
@ncfavier
ncfavier / N.hs
Last active March 6, 2023 17:01
n-ary homogeneous functions as a free Applicative functor
{-# LANGUAGE UnicodeSyntax #-}
-- n-ary functions s^n → a.
-- N s is the free Applicative on Reader s.
-- It encodes the effect "ask for one s", but since it's an Applicative
-- and not a Monad you can statically know how many arguments a function requires.
data N s a = Pure a | Ask (N s (s → a))
arity :: N s a → Integer
arity (Pure _) = 0
@ncfavier
ncfavier / Cursed.hs
Created February 17, 2023 16:43
Identity function with special treatment to Bool values, by @Player-205
{-# LANGUAGE MagicHash, UnboxedTuples #-}
import GHC.Prim
import Unsafe.Coerce
import Prelude hiding (id)
main :: IO ()
main = do
print (id True)
print (id False)
print (id 15)
{-# OPTIONS --without-K #-}
open import Agda.Primitive
private variable
ℓ ℓ' : Level
A B : Set ℓ
data _≡_ {A : Set ℓ} : A → A → Set ℓ where
refl : {a : A} → a ≡ a
open import Agda.Primitive renaming (Set to Type)
open import Data.Product
open import Relation.Binary.PropositionalEquality

Futamura projections

Let us represent programs from inputs I to outputs O as simple functions I → O, and assume we have a specialiser: a program that partially evaluates another program applied to the "static" part of its input.

@ncfavier
ncfavier / NatMonoidEndo.agda
Last active December 23, 2023 09:52
ℕ ≃ (m : Monoid) → ⟨ m ⟩ → ⟨ m ⟩, moved to https://agda.monade.li/NatChurchMonoid.html
-- Moved to https://agda.monade.li/NatChurchMonoid.html
{ size ? 1000 }: with builtins;
let
enumerate = genList (i: i + 1);
sum = foldl' add 0;
divides = n: k: n / k * k == n;
factors = n: filter (divides n) (enumerate (n / 2));
perfect = n: sum (factors n) == n;
in
filter perfect (enumerate size)
@ncfavier
ncfavier / LEM.agda
Last active March 18, 2023 01:25
LEM ≡ ∀ A → A ∨ ¬ A
module LEM where
open import 1Lab.Type
open import 1Lab.Path
open import 1Lab.HLevel
open import 1Lab.HIT.Truncation
open import Data.Sum
⊥-is-prop : is-prop ⊥
⊥-is-prop ()