Skip to content

Instantly share code, notes, and snippets.

@Lysxia
Lysxia / F.hs
Created September 12, 2022 09:44
Defunctionalization with only one constructor (and plenty of strings)
{-# LANGUAGE
DataKinds,
PolyKinds,
TypeFamilies #-}
module F where
import Data.Void
import Data.Functor.Const
import Data.Kind (Type)
import GHC.TypeLits
@Lysxia
Lysxia / fci.hs
Last active September 7, 2022 12:04
Idea for first-class instances
-- What if...
viaTraversable :: forall t u. (Traversable t, Functor u, Foldable u, Coercible t u) => Instance (Traversable u)
viaTraversable = instance Traversable u where
traverse f = fmap coerce . traverse @t f . coerce
sequence = fmap coerce . sequence @t
-- then we could have DerivingVia for traversable...
deriving instance Traversable MyU = viaTraversable @MyT
@Lysxia
Lysxia / HTMLParser.hs
Last active September 5, 2022 14:07
Applicative HTML Parser
{-# LANGUAGE GADTs, DeriveFunctor, LambdaCase #-}
import Data.Functor.Product
import Data.Void
import Control.Applicative
type Tag = String
type Attr = String
data HTML
@Lysxia
Lysxia / T.hs
Created September 4, 2022 18:33
Applicative tree parser
{-# LANGUAGE GADTs #-}
import Data.Map (Map)
import qualified Data.Map as Map
import Control.Applicative (Applicative(..), liftA)
type Tag = String
type Forest = Map Tag Tree
data Tree = Node String Forest
@Lysxia
Lysxia / BBD2FCN.agda
Last active September 2, 2022 19:57
Embedding of "Binders by day, labels by night" into "First-class names for effect handlers" in Agda (WIP)
-- Embedding of "Binders by day, labels by night" (BindersByDay.agda)
-- into "First-class names for effect handlers" (FirstClassNames.agda)
-- (syntax only)
--
-- Main idea: translate dependent name abstraction
-- ⟦ λᴺ a . M ⟧
-- to two System-F abstractions
-- Λ (η : ★) . λ (a : Ev η) . ⟦ M ⟧
-- Line 156: ⟦ ƛᴺ M ⟧ᵛ = Λ ƛ ⟦ M ⟧
@Lysxia
Lysxia / S.v
Created July 4, 2022 22:01
Escardo's infinite search
From Coq Require Import Arith.
Axiom funext : forall (f g : nat -> bool), (forall i, f i = g i) -> f = g.
Definition is_modulus (p : (nat -> bool) -> bool) (n : nat) : Prop :=
forall f g, (forall i, i < n -> f i = g i) -> p f = p g.
Axiom compacity : forall p, exists n, is_modulus p n.
Definition extend (b : bool) (f : nat -> bool) : nat -> bool :=
-- Minimax and alpha-beta pruning
--
-- Minimax can trivially be generalized to work on any lattice (@gminimax@).
-- Then alpha-beta is actually an instance of minimax.
--
-- Contents:
-- 0. Basic definitions: players and games
-- 1. Direct implementations of minimax and alpha-beta
-- 2. Generalized minimax and instantiation to alpha-beta
-- 3. QuickCheck tests
From Coq Require Import List Arith.
Import ListNotations.
Local Open Scope list_scope.
Inductive instr : Set :=
| Select : nat -> instr
| Backspace : instr.
Definition prog := list instr.
@Lysxia
Lysxia / P.hs
Created April 3, 2022 15:02
A DRY idiom with monadic profunctors
{-# LANGUAGE RankNTypes, QuantifiedConstraints, PolyKinds, TypeFamilies #-}
module P where
import Data.Kind (Type)
bar :: forall g. Reflective g => g String Int Int
bar = undefined
baz :: forall g. Reflective g => Int -> g String Int Int
{-# LANGUAGE TypeFamilies, DataKinds, ConstraintKinds, UndecidableInstances, TypeApplications, ScopedTypeVariables #-}
module Countdown where
import Data.Proxy
import GHC.TypeNats
import Data.Kind
import qualified Fcf as F
data C2 :: Nat -> F.Exp Constraint