Skip to content

Instantly share code, notes, and snippets.

-- RHS size: {terms: 109, types: 51, coercions: 0, joins: 3/6}
$wvarianceNoInline :: forall {v :: * -> *}. (v Double -> Int) -> (v Double -> Int# -> Box Double) -> v Double -> Double#
$wvarianceNoInline
= \ (@(v_ :: * -> *))
(basicLen :: v_ Double -> Int)
(basicIndex :: v_ Double -> Int# -> Box Double)
(xs_s2g2 :: v_ Double) ->
case xs_s2g2 of vec { __DEFAULT ->
case basicLen vec of { I# len_i ->
let {

Making lens from Traversable

Usually explanation of val Laarhoven lens is started from lens. But for me such construction feels completely artificial. Nothing motivates writing function with such weird type. Why would anyone wants to write functions with weird type signature? How could anyone come up with such idea? Traversable is much better starting point. It's part of base and it's likely that reader is familiar with this type class and appreciates its usefulness.

Traversals

{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
@Shimuuar
Shimuuar / console.txt
Created January 11, 2020 17:20
IEEE754 WAT
$ ghc -fforce-recomp -O2 wat.hs && ./wat
[1 of 1] Compiling Main ( wat.hs, wat.o )
Linking wat ...
NaN
-Infinity
Infinity
-0.0
NaN
-Infinity
Infinity
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
@Shimuuar
Shimuuar / overload.hs
Created August 13, 2019 16:58
Override data types in instance
-- This is an approach to refine ability to selectively override
-- instances when deriving using deriving via method. Idea was first
-- presented here:
--
-- http://caryrobbins.com/dev/overriding-type-class-instances/
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
@Shimuuar
Shimuuar / magick.hs
Created August 28, 2012 18:02 — forked from qnikst/magick.hs
Do magik
import Data.Default
import Debug.Trace
data Proxy a = Proxy
class Magick a where
magickStorage :: Proxy [a] -> String
instance Magick Int where
magickStorage = const "Int"
@Shimuuar
Shimuuar / gist:3501111
Created August 28, 2012 17:25
Do magik
class Magic a where
conjure :: String -> [a]
magikStorage :: [a] -> String
-- Use return type
doMagik :: Magic a => Int -> [a]
doMagik n = concat $ replicate n res
where
res = conjure $ magikStorage res -- Must be lazy in res !!!!
@Shimuuar
Shimuuar / Prime.hs
Created March 16, 2012 13:26 — forked from max630/MainCLI.hs
testing prime speed
{-# LANGUAGE NoMonomorphismRestriction, BangPatterns #-}
module Main where
-- compile: ghc --make -main-is Prime.main Prime.hs -O2
-- $time ./Prime i 500000
-- ./Prime i 500000 4.35s user 0.00s system 99% cpu 4.453 total
-- ./Prime s 500000 7.69s user 0.03s system 99% cpu 7.726 total (!!!)
-- lookup OPT in comments about various optimization points
import System.Environment(getArgs)
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE Rank2Types #-}