Skip to content

Instantly share code, notes, and snippets.

View Lev135's full-sized avatar
📚
Studying mathematics

Lev Dvorkin Lev135

📚
Studying mathematics
  • 05:31 (UTC +03:00)
View GitHub Profile
@Lev135
Lev135 / gist:0ce89e17f960371dac178d1e797affef
Created July 28, 2026 18:12
Git view pretty commit tree command
git config --global alias.logt 'log --graph --pretty=oneline --abbrev-commit --all --decorate'
@Lev135
Lev135 / Lam.hs
Last active July 13, 2026 10:34
megaparsec with custom token stream (Lib.hs -- general definitions, Simple.hs & Lam.hs -- examples of usage)
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Lam where
import Control.Applicative
import qualified Control.Monad.Combinators.Expr as Expr
import qualified Data.Char as Char
import qualified Data.List.NonEmpty as NE
@Lev135
Lev135 / Main.hs
Created July 10, 2026 13:06
Several ways to modify records with parameters using lenses
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE NoFieldSelectors #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE DeriveFunctor #-}
module Main where
import Control.Lens
testRec :: Rec (Int, String)
\documentclass{beamer}
\usepackage{lipsum}
% Paragraph spacing
\setlength{\parskip}{1ex}
\makeatletter
\def\@listi{\leftmargin\leftmargini
\topsep 0pt % Space before/after list
\parsep 0pt % Space between paragraphs within an item
\itemsep 0pt} % Space between separate items
@Lev135
Lev135 / AbstractOptic.hs
Created July 12, 2023 12:56
Abstraction over profunctor optics' kinds
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
@Lev135
Lev135 / Optics.md
Last active October 7, 2023 10:41
Writing profunctor optics by the same template

Writing profunctor optics by the same template

All of the profunctor optics kind have the same, very simple, pattern:

type AnOptic p s t a b = p a b -> p s t
type Optic c s t a b = forall p. c p => AnOptic p s t a b

type Iso s t a b = Optic Profunctor s t a b
@Lev135
Lev135 / Optics.hs
Created July 12, 2023 09:16
Profunctor optics, obtained by the same template
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
import Control.Monad
import Data.Bifunctor
type AnOptic p s t a b = p a b -> p s t
Benchmark bench-trans-speed: RUNNING...
benchmarking 1/20 modify/pure/2000
time 1.033 μs (1.022 μs .. 1.052 μs)
0.991 R² (0.981 R² .. 0.997 R²)
mean 1.130 μs (1.084 μs .. 1.207 μs)
std dev 182.7 ns (129.1 ns .. 265.3 ns)
variance introduced by outliers: 95% (severely inflated)
benchmarking 1/20 modify/ParserT State/2000
time 1.250 μs (1.236 μs .. 1.264 μs)
@Lev135
Lev135 / monadic-lens.md
Created June 10, 2023 22:39
Monadic variants of optics from Haskell lens library

Monadic lens

So much has been written about lens and other optics in Haskell that these ideas are likely not very original. However, I'll try.

Initial problem

Let's say we have some recursive data type:

type Record = Map String Value
data Value = I Int | R Record
  deriving (Generic, Show)
@Lev135
Lev135 / LensM.hs
Last active June 10, 2023 00:12
Monadic lens in Van Laarhoven representation
{-# LANGUAGE RankNTypes #-}
module LensM where
import Control.Applicative (Const(..))
import Data.Functor.Identity (Identity(..))
import Data.Functor.Contravariant (Contravariant)
import Data.Functor.Compose
class Functor f => MfaAms f where