Skip to content

Instantly share code, notes, and snippets.

@minoki
minoki / Main.hs
Created February 22, 2018 12:02
関数内ローカル変数に IORef を使うな
{-# LANGUAGE MagicHash #-}
module Main where
import Data.IORef
import Control.Monad (forM_)
import System.Environment (getArgs)
import Control.Monad.State.Class
import qualified Control.Monad.State.Strict as StateStrict
import qualified Control.Monad.State.Lazy as StateLazy
import Control.Monad.Writer.Strict as WriterStrict
import GHC.Prim
@minoki
minoki / dumparg.c
Created June 10, 2018 03:44
Experiments on MSVCRT's setlocale() and system()
// dumparg.c
#include <stdio.h>
#include <wchar.h>
int wmain(int argc, const wchar_t *argv[])
{
for (int i = 0; i < argc; ++i) {
const wchar_t *s = argv[i];
printf("argv[%d] = ", i);
while (*s != 0) {
if (*s < 0x80 && isprint(*s)) {
{-# LANGUAGE BangPatterns #-}
import Gauge.Main
import Control.Monad
import Control.Monad.State.Strict
import Data.IORef
import qualified Data.Vector.Unboxed.Mutable as V
import Data.Primitive.MutVar
{-
benchmarked vector
@minoki
minoki / result.txt
Created April 21, 2019 10:45
Haskellでもエラトステネスの篩がしたい!
benchmarked trial division (2)/1000
time 337.3 μs (333.0 μs .. 342.5 μs)
0.996 R² (0.987 R² .. 0.999 R²)
mean 344.1 μs (339.5 μs .. 353.6 μs)
std dev 22.82 μs (7.337 μs .. 41.88 μs)
variance introduced by outliers: 42% (moderately inflated)
benchmarked trial division (2)/10000
time 22.05 ms (21.57 ms .. 22.77 ms)
0.996 R² (0.989 R² .. 0.999 R²)

実習用マシンへのTeX Live 2019のインストール

エクスプローラーのアドレス部分に ftp://download.ks.prv を打ち込む。 mirror > texlive > tlnet を開く。 install-tl-windows.exe をデスクトップにコピーする。

(ブラウザーで ftp://download.ks.prv/mirror/texlive/tlnet を開いて、 install-tl-windows.exe をダウンロードするのでも良い。)

展開

local PDF_DIR = ".converted-pdf"
os.execute("mkdir -p " .. PDF_DIR)
local function file_exists(fn)
local f = io.open(fn,"r")
if f then
f:close()
return true
else
return false
@minoki
minoki / Main.purs
Created September 5, 2019 09:04
短絡評価の有無で停止するか否かが変わる例
module Main where
import Prelude
import Data.HeytingAlgebra
import Effect (Effect)
import Effect.Console (log)
foo :: forall a. HeytingAlgebra a => Unit -> a
foo x = foo x
@minoki
minoki / customshow.hs
Created October 8, 2019 15:55
OVERLAPPINGを使ってShow [Char]の定義を上書きするようなやり方はやばいぞ
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE IncoherentInstances #-} -- これがないとエラーになる(この拡張はかなりやばい)
import Data.List
class MyShow a where
myShow :: a -> String
instance MyShow Int where myShow = show
@minoki
minoki / CmpNat.hs
Last active November 22, 2019 10:50
Generating CmpNat constraint at runtime
#!/usr/bin/env stack
-- stack --resolver lts-14.15 script --package reflection
{-# LANGUAGE DataKinds, RankNTypes, TypeOperators, TypeFamilies, ScopedTypeVariables, TypeApplications #-}
import GHC.TypeNats
import Data.Type.Equality
import Unsafe.Coerce
import Data.Proxy
import Data.Reflection
cmpNat :: (KnownNat a, KnownNat b)
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
import Unboxable
import Mod
import Data.Coerce
-- Defining Unboxable instance is as good as exporting the data constructor!
castUnboxable :: Unboxable a => Rep a -> a
castUnboxable = coerce