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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DefaultSignatures #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
{-# LANGUAGE MagicHash #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE QuantifiedConstraints #-} | |
{-# LANGUAGE RankNTypes #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE StandaloneDeriving #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE DeriveFunctor #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE KindSignatures #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE StandaloneDeriving #-} | |
{-# LANGUAGE TypeApplications #-} | |
{-# LANGUAGE TypeFamilies #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 !!!! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE FunctionalDependencies #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE DeriveDataTypeable #-} | |
{-# LANGUAGE EmptyDataDecls #-} | |
{-# LANGUAGE Rank2Types #-} |
NewerOlder