Skip to content

Instantly share code, notes, and snippets.

View Sintrastes's full-sized avatar

Nathan BeDell Sintrastes

View GitHub Profile
@Sintrastes
Sintrastes / EmulateMultiReceivers.kt
Created October 15, 2021 18:53
Example of how to emulate multiple recievers with bounded polymorphism in Kotlin
interface Logging {
fun log(message: String)
}
interface Prompt {
fun promptUser(): String
}
fun <Ctx> Ctx.exampleFn() where Ctx: Logging, Ctx: Prompt {
@Sintrastes
Sintrastes / Day-o.hs
Created October 2, 2021 14:35
Example of using the day convolution to build up a comonad for a product type using an applicative-like style.
{-# LANGUAGE ExistentialQuantification #-}
data Store a b = Store a (a -> b)
instance Functor (Store a) where
fmap f (Store x g) = Store x (f . g)
data Day f g a = forall b c. Day (f b) (g c) (b -> c -> a)
@Sintrastes
Sintrastes / Recipes.curry
Created September 15, 2021 00:55
Example application of functional logic programming to deduce recipes for creating composite rasters based on available data/
data Expr =
Elevation
| Slope
| Landcover
| Add Expr Expr
| Mul Expr Expr
| Pow Expr Expr
| Const Float
deriving(Eq)
@Sintrastes
Sintrastes / GenericsExample.kt
Last active September 8, 2021 13:48
Black magic type-hackery to emulate typed units of measure in Kotlin with inline classes and "type-class-like" constructs built with reified generics.
interface Form<Ctx,A> {
fun getValue(): A
}
// Note: I can do something like this, but I can't do
// something like:
// Form<Void, WDouble<Length, in Unit<Length> && ConvertibleTo<Meters>
val test: Form<Void, WDouble<Length, in Unit<Length>>>
get() = TODO()
@Sintrastes
Sintrastes / Buildable.kt
Last active July 27, 2021 16:52
An example of the Buildable interface in Kotlin.
/**
* Interface for a type that implements a
* typesafe builder for some other type A.
*/
interface Builder<A> {
fun build(): A?
}
/**
@Sintrastes
Sintrastes / IntersectionTypes.hs
Last active June 17, 2021 02:27
Attempted emulation of OOP "interface"-style intersection types with type classes in Haskell.
{-# LANGUAGE
ConstraintKinds,
KindSignatures,
ExistentialQuantification,
DataKinds,
TypeFamilies,
TypeOperators,
AllowAmbiguousTypes,
TypeSynonymInstances,
FlexibleInstances,
@Sintrastes
Sintrastes / ComonadicVDOM.hs
Last active June 13, 2021 00:41
Comonadic VDOMs
-- Example of a simple VDOM implemented using a cofree comonad.
data Cofree f a = Cofree a (f (Cofree f a))
class Diffable a where
data Diff a :: *
diff :: a -> a -> Diff a
-- Could this have another parameter for "value type"?
@Sintrastes
Sintrastes / KComonad.hs
Created June 8, 2021 23:20
Typeclass for Comonads in a Kleisli category over a monad.
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
class KComonad m w where
extractM :: w a -> m a
-- extendM :: (w a -> m b) -> w a -> m (w b)
duplicateM :: w a -> m (w (w a))
-- Properties:
-- Any Monad is a KMonad automatically.
-- Any KMonad over a base monad m is automatically
-- a KMonad over a monad m whenever there is a monad
@Sintrastes
Sintrastes / BCKW.idr
Last active May 29, 2021 20:15
Haskell Curry's BCKW combinators as a GADT in Idris, making use of Idiom notation.
module Main
-- | Simple implementation of the simply typed lambda calculus
-- with products, sums, and a natural number object
-- build on Haskell Curry's BCKW combinators.
-- Note: This could be extended by supplying a set of
-- "base morphisms", and making a type that depends on those.
infixr 7 ~>
data (~>) : Type -> Type -> Type where
-- Introduce a constant from an Idris value.
@Sintrastes
Sintrastes / subtyping.idr
Created May 24, 2021 14:14
Implementation of subtyping in Idris.
class Sub x y where
lift : x -> y
instance Sub x x where
lift = id
pure : a -> a
pure x = x
(<*>) : (Sub a' a, Sub b b') => (a -> b) -> a' -> b'