Skip to content

Instantly share code, notes, and snippets.

@glaebhoerl
glaebhoerl / scopemap.rs
Last active July 3, 2022 12:59
Rust hash table with efficient support for nested scopes (save/restore)
// Based on idea: https://twitter.com/pkhuong/status/1287510400372748290
use hashbrown::raw::RawTable;
pub struct ScopeMap<K, V> {
last_scope_id: ScopeId,
scopes: Vec<ScopeId>, // values are zeroed instead of popped to save a check in get() / is_fresh()
current_scope: ScopeDepth, // index of innermost valid scope
values: RawTable<Entry<K, V>>,
shadowed: Vec<Shadowed<K, V>>,
@glaebhoerl
glaebhoerl / UTLC++.hs
Created May 22, 2019 20:23
UTLC++/Generic
-- see also previous gist https://gist.github.com/glaebhoerl/466267f0c977cef74202f167d6493cc0 and tweets https://twitter.com/glaebhoerl/status/1129851506067427329
-- here we accomplish the same thing but in a totally different way, by relying on the host language for basically everything
{-# LANGUAGE LambdaCase, GADTs, TypeOperators, DataKinds, PolyKinds, Strict, DeriveFunctor, RankNTypes, TypeFamilies, ConstraintKinds #-}
import Prelude hiding (product, sum)
import Data.Type.Equality
import GHC.Exts (Constraint)
{-# LANGUAGE LambdaCase, OverloadedStrings #-}
import Data.String
type Name = String
data Expr
= V Name
| Lam Name Expr
| Either Bool Expr -- "False = Left, True = Right"
{-# LANGUAGE GADTs, DeriveFunctor, RankNTypes #-}
import Prelude hiding (Monad (..))
main = print ()
-----------------------------------------------------------------------
class Functor m => Monad m where
return :: a -> m a
@glaebhoerl
glaebhoerl / A.hs
Last active July 4, 2017 23:56 — forked from rwbarton/A.hs
-- Code taken from http://stackoverflow.com/questions/12735274/breaking-data-set-integrity-without-generalizednewtypederiving/12744568#12744568
-- Discussion on haskell-cafe: http://thread.gmane.org/gmane.comp.lang.haskell.cafe/100870
-- http://www.haskell.org/pipermail/haskell-cafe/2012-October/103984.html
-- Modified to remove orphan instances by rwbarton
-- Simplified by glaebhoerl
module A (A(..), Set, empty, insert, on) where
import Data.Set
import Data.Function
module Person (Person, null, newPerson, isNull, getAge, getName) where
-- constructor is not exported! only this module has access to the internal Maybe
newtype Person = Person (Maybe (Int, String))
null :: Person
null = Person Nothing
newPerson :: Int -> String -> Person
newPerson age name = Person (Just (age, name))
@glaebhoerl
glaebhoerl / GHC_Query
Created October 6, 2012 12:10 — forked from nfrisby/GHC_Query
Exploring kind-indexed type constraints in GHC 7.6
{-# LANGUAGE PolyKinds, GADTs, DataKinds, TypeFamilies, TypeOperators,
MultiParamTypeClasses, FlexibleContexts, FlexibleInstances,
UndecidableInstances #-}
module GHC_Query where
data Proxy (t :: k) = Proxy
data KindProxy (t :: *) = KindProxy -- a type-level proxy for kinds
data Nat = Z | S Nat
@glaebhoerl
glaebhoerl / gist:056051916220a099eb4d
Created November 29, 2014 16:55
Fn, FnMut, FnOnce hierarchy in C++
template<typename Return, typename... Arguments>
struct FnOnce
{
using This = FnOnce<Return, Arguments...>;
virtual Return operator ()(Arguments&&...) && = 0;
};
template<typename Return, typename... Arguments>
struct FnMut: FnOnce<Return, Arguments...>
@glaebhoerl
glaebhoerl / lambda-template-unary.cpp
Created November 29, 2014 16:35
Lazily evaluated type-level lambda calculus in C++
template<typename T, T x>
struct Value
{
static constexpr T value() { return x; }
};
template<int n>
using Int = Value<int, n>;
template<bool b>
@glaebhoerl
glaebhoerl / gist:8795f00d90bec80bc400
Last active August 29, 2015 14:05
Rustic exceptions
## Exceptions
Parts:
* union types / restricted Any
* match-on-type
* `throws Type`, `throw val`
* `try!`
Union types:
Either<A, B, C, D>
Can be any of A, B, C, or D