Skip to content

Instantly share code, notes, and snippets.

View guibou's full-sized avatar
🛩️
Diving in a sky full of escaped skolems

Carbon based Guibou guibou

🛩️
Diving in a sky full of escaped skolems
  • Saint-Paul - Reunion - France
View GitHub Profile
@guibou
guibou / Examples
Created December 31, 2022 16:50
Iso 8601 duration parsing at type level
ghci> duration @"P1Y1W"
Duration {years = 1, months = 0, days = 0, weeks = 1, hours = 0, minutes = 0, seconds = 0}
ghci> duration @"P1Y1WT10H5M"
Duration {years = 1, months = 0, days = 0, weeks = 1, hours = 10, minutes = 5, seconds = 0}
ghci> duration @"P1Y1WT10H5M120S"
Duration {years = 1, months = 0, days = 0, weeks = 1, hours = 10, minutes = 5, seconds = 120}
ghci> duration @"P10H5M120S"
<interactive>:119:1: error:
• Cannot parse duration. Expecting 'T', got: 10H5M120S
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE GADTs #-}
import GHC.Generics
import Data.Data (Data, Typeable)
data Tag = TagA | TagB
data MyGADT t where
cases :: [a] -> String
cases _ = "Hello"
fiz = \cases l -> cases
main = do
putStrLn $ fiz "Chien" []
locFromECP :: ECP -> PV SrcSpan
locFromECP e = locA . getLoc <$> unECP e
{-
1. Defined but not used: ‘locFromECP’
2. • Ambiguous type variable ‘t0’ arising from a use of ‘unECP’
prevents the constraint ‘(DisambECP (t0 GhcPs))’ from being solved.
Probable fix: use a type annotation to specify what ‘t0’ should be.
These potential instances exist:
instance DisambECP (PatBuilder GhcPs)
@guibou
guibou / TestDebug.hs
Created November 3, 2021 09:36
Test of GHC debugger with GHC 9.2
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE BangPatterns #-}
import Control.Arrow
fact :: Int -> IO Int
fact = go 1
where
go acc 0 = pure acc
go !acc n = do
putStrLn "Yada"
@guibou
guibou / FoldLR.hs
Created July 25, 2021 18:08
`foldLR` automatically uses `foldl` or `foldr` depending on the folding function.
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
class Fold f t acc | f -> t acc where
foldLR :: Foldable foldable => f -> acc -> foldable t -> acc
instance Fold (t -> acc -> acc) t acc where
foldLR = foldr
instance Fold (acc -> t -> acc) t acc where
#include <iostream>
class Callback
{
public:
virtual void call() = 0;
};
Callback *registered_callback;
@guibou
guibou / Foo.hs
Created June 28, 2021 09:10
Deriving Via and RankNTypes constraints do not play well and leads to redundant constraints
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS -Wall -Wredundant-constraints #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ConstrainedClassMethods #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE ConstraintKinds #-}
@guibou
guibou / HaskellCheck.hs
Last active June 9, 2021 18:02
Check that module name inside an haskell file (e.g. "module Foo.Bar where") match the filename (e.g. `prefix/Foo/Bar.hs`)
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
import System.Process
import qualified Data.Text as Text
import qualified Data.Text.IO as Text
import Data.Foldable (for_)
import Data.Char (isLower)
import Control.Monad (when)
import Control.Applicative ((<|>))