code
stringlengths 2
1.05M
| repo_name
stringlengths 5
101
| path
stringlengths 4
991
| language
stringclasses 3
values | license
stringclasses 5
values | size
int64 2
1.05M
|
|---|---|---|---|---|---|
-----------------------------------------------------------------------------
-- |
-- Module : Distribution.Simple.JHC
-- Copyright : Isaac Jones 2003-2006
-- License : BSD3
--
-- Maintainer : cabal-devel@haskell.org
-- Portability : portable
--
-- This module contains most of the JHC-specific code for configuring, building
-- and installing packages.
module Distribution.Simple.JHC (
configure, getInstalledPackages,
buildLib, buildExe,
installLib, installExe
) where
import Distribution.PackageDescription as PD
( PackageDescription(..), BuildInfo(..), Executable(..)
, Library(..), libModules, hcOptions, usedExtensions )
import Distribution.InstalledPackageInfo
( emptyInstalledPackageInfo, )
import qualified Distribution.InstalledPackageInfo as InstalledPackageInfo
import Distribution.Simple.PackageIndex (InstalledPackageIndex)
import qualified Distribution.Simple.PackageIndex as PackageIndex
import Distribution.Simple.LocalBuildInfo
( LocalBuildInfo(..), ComponentLocalBuildInfo(..) )
import Distribution.Simple.BuildPaths
( autogenModulesDir, exeExtension )
import Distribution.Simple.Compiler
( CompilerFlavor(..), CompilerId(..), Compiler(..), AbiTag(..)
, PackageDBStack, Flag, languageToFlags, extensionsToFlags )
import Language.Haskell.Extension
( Language(Haskell98), Extension(..), KnownExtension(..))
import Distribution.Simple.Program
( ConfiguredProgram(..), jhcProgram, ProgramConfiguration
, userMaybeSpecifyPath, requireProgramVersion, lookupProgram
, rawSystemProgram, rawSystemProgramStdoutConf )
import Distribution.Version
( Version(..), orLaterVersion )
import Distribution.Package
( Package(..), ComponentId(ComponentId),
pkgName, pkgVersion, )
import Distribution.Simple.Utils
( createDirectoryIfMissingVerbose, writeFileAtomic
, installOrdinaryFile, installExecutableFile
, intercalate )
import System.FilePath ( (</>) )
import Distribution.Verbosity
import Distribution.Text
( Text(parse), display )
import Distribution.Compat.ReadP
( readP_to_S, string, skipSpaces )
import Distribution.System ( Platform )
import Data.List ( nub )
import Data.Char ( isSpace )
import qualified Data.Map as M ( empty )
import Data.Maybe ( fromMaybe )
import qualified Data.ByteString.Lazy.Char8 as BS.Char8
-- -----------------------------------------------------------------------------
-- Configuring
configure :: Verbosity -> Maybe FilePath -> Maybe FilePath
-> ProgramConfiguration -> IO (Compiler, Maybe Platform, ProgramConfiguration)
configure verbosity hcPath _hcPkgPath conf = do
(jhcProg, _, conf') <- requireProgramVersion verbosity
jhcProgram (orLaterVersion (Version [0,7,2] []))
(userMaybeSpecifyPath "jhc" hcPath conf)
let Just version = programVersion jhcProg
comp = Compiler {
compilerId = CompilerId JHC version,
compilerAbiTag = NoAbiTag,
compilerCompat = [],
compilerLanguages = jhcLanguages,
compilerExtensions = jhcLanguageExtensions,
compilerProperties = M.empty
}
compPlatform = Nothing
return (comp, compPlatform, conf')
jhcLanguages :: [(Language, Flag)]
jhcLanguages = [(Haskell98, "")]
-- | The flags for the supported extensions
jhcLanguageExtensions :: [(Extension, Flag)]
jhcLanguageExtensions =
[(EnableExtension TypeSynonymInstances , "")
,(DisableExtension TypeSynonymInstances , "")
,(EnableExtension ForeignFunctionInterface , "")
,(DisableExtension ForeignFunctionInterface , "")
,(EnableExtension ImplicitPrelude , "") -- Wrong
,(DisableExtension ImplicitPrelude , "--noprelude")
,(EnableExtension CPP , "-fcpp")
,(DisableExtension CPP , "-fno-cpp")
]
getInstalledPackages :: Verbosity -> PackageDBStack -> ProgramConfiguration
-> IO InstalledPackageIndex
getInstalledPackages verbosity _packageDBs conf = do
-- jhc --list-libraries lists all available libraries.
-- How shall I find out, whether they are global or local
-- without checking all files and locations?
str <- rawSystemProgramStdoutConf verbosity jhcProgram conf ["--list-libraries"]
let pCheck :: [(a, String)] -> [a]
pCheck rs = [ r | (r,s) <- rs, all isSpace s ]
let parseLine ln =
pCheck (readP_to_S
(skipSpaces >> string "Name:" >> skipSpaces >> parse) ln)
return $
PackageIndex.fromList $
map (\p -> emptyInstalledPackageInfo {
InstalledPackageInfo.installedComponentId =
ComponentId (display p),
InstalledPackageInfo.sourcePackageId = p
}) $
concatMap parseLine $
lines str
-- -----------------------------------------------------------------------------
-- Building
-- | Building a package for JHC.
-- Currently C source files are not supported.
buildLib :: Verbosity -> PackageDescription -> LocalBuildInfo
-> Library -> ComponentLocalBuildInfo -> IO ()
buildLib verbosity pkg_descr lbi lib clbi = do
let Just jhcProg = lookupProgram jhcProgram (withPrograms lbi)
let libBi = libBuildInfo lib
let args = constructJHCCmdLine lbi libBi clbi (buildDir lbi) verbosity
let pkgid = display (packageId pkg_descr)
pfile = buildDir lbi </> "jhc-pkg.conf"
hlfile= buildDir lbi </> (pkgid ++ ".hl")
writeFileAtomic pfile . BS.Char8.pack $ jhcPkgConf pkg_descr
rawSystemProgram verbosity jhcProg $
["--build-hl="++pfile, "-o", hlfile] ++
args ++ map display (libModules lib)
-- | Building an executable for JHC.
-- Currently C source files are not supported.
buildExe :: Verbosity -> PackageDescription -> LocalBuildInfo
-> Executable -> ComponentLocalBuildInfo -> IO ()
buildExe verbosity _pkg_descr lbi exe clbi = do
let Just jhcProg = lookupProgram jhcProgram (withPrograms lbi)
let exeBi = buildInfo exe
let out = buildDir lbi </> exeName exe
let args = constructJHCCmdLine lbi exeBi clbi (buildDir lbi) verbosity
rawSystemProgram verbosity jhcProg (["-o",out] ++ args ++ [modulePath exe])
constructJHCCmdLine :: LocalBuildInfo -> BuildInfo -> ComponentLocalBuildInfo
-> FilePath -> Verbosity -> [String]
constructJHCCmdLine lbi bi clbi _odir verbosity =
(if verbosity >= deafening then ["-v"] else [])
++ hcOptions JHC bi
++ languageToFlags (compiler lbi) (defaultLanguage bi)
++ extensionsToFlags (compiler lbi) (usedExtensions bi)
++ ["--noauto","-i-"]
++ concat [["-i", l] | l <- nub (hsSourceDirs bi)]
++ ["-i", autogenModulesDir lbi]
++ ["-optc" ++ opt | opt <- PD.ccOptions bi]
-- It would be better if JHC would accept package names with versions,
-- but JHC-0.7.2 doesn't accept this.
-- Thus, we have to strip the version with 'pkgName'.
++ (concat [ ["-p", display (pkgName pkgid)]
| (_, pkgid) <- componentPackageDeps clbi ])
jhcPkgConf :: PackageDescription -> String
jhcPkgConf pd =
let sline name sel = name ++ ": "++sel pd
lib = fromMaybe (error "no library available") . library
comma = intercalate "," . map display
in unlines [sline "name" (display . pkgName . packageId)
,sline "version" (display . pkgVersion . packageId)
,sline "exposed-modules" (comma . PD.exposedModules . lib)
,sline "hidden-modules" (comma . otherModules . libBuildInfo . lib)
]
installLib :: Verbosity
-> LocalBuildInfo
-> FilePath
-> FilePath
-> FilePath
-> PackageDescription
-> Library
-> ComponentLocalBuildInfo
-> IO ()
installLib verb _lbi dest _dyn_dest build_dir pkg_descr _lib _clbi = do
let p = display (packageId pkg_descr)++".hl"
createDirectoryIfMissingVerbose verb True dest
installOrdinaryFile verb (build_dir </> p) (dest </> p)
installExe :: Verbosity -> FilePath -> FilePath -> (FilePath,FilePath) -> PackageDescription -> Executable -> IO ()
installExe verb dest build_dir (progprefix,progsuffix) _ exe = do
let exe_name = exeName exe
src = exe_name </> exeExtension
out = (progprefix ++ exe_name ++ progsuffix) </> exeExtension
createDirectoryIfMissingVerbose verb True dest
installExecutableFile verb (build_dir </> src) (dest </> out)
|
martinvlk/cabal
|
Cabal/Distribution/Simple/JHC.hs
|
Haskell
|
bsd-3-clause
| 8,758
|
{-# OPTIONS_GHC -fwarn-unsafe #-}
{-# LANGUAGE FlexibleInstances #-}
module SH_Overlap9_A (
C(..)
) where
import SH_Overlap9_B
instance
{-# OVERLAPS #-}
C [Int] where
f _ = "[Int]"
|
urbanslug/ghc
|
testsuite/tests/safeHaskell/overlapping/SH_Overlap9_A.hs
|
Haskell
|
bsd-3-clause
| 198
|
-- !!! Test passing doubles to a ccall
import Foreign.C
foreign import ccall unsafe "math.h sin"
c_sin :: CDouble -> IO CDouble
main = c_sin 1.0 >>= print
|
urbanslug/ghc
|
testsuite/tests/ffi/should_run/ffi003.hs
|
Haskell
|
bsd-3-clause
| 160
|
{-# LANGUAGE UnicodeSyntax, MultiParamTypeClasses, FlexibleInstances #-}
module T2902_B_PairingSum (Sum(..), PSum) where
import T2902_Sum
data PSum a b = Empty | Tree a b [PSum a b]
instance (Ord a, Eq b, Num b) β Sum PSum a b where
insert v r = union $ Tree v r []
union x Empty = x
union Empty x = x
union x@(Tree v r xs) y@(Tree w s ys) =
case compare v w of
LT β Tree v r (y:xs)
GT β Tree w s (x:ys)
EQ β case r + s of
0 β z
t β insert v t z
where z = union (unions xs) (unions ys)
unions [] = Empty
unions [x] = x
unions (x : y : zs) = union (union x y) (unions zs)
extractMin Empty = undefined
extractMin (Tree v r xs) = ((v,r), unions xs)
fromList [] = Empty
fromList ((v,r):xs) = insert v r $ fromList xs
toList Empty = []
toList x = let (y, z) = extractMin x in y : toList z
|
ghc-android/ghc
|
testsuite/tests/perf/should_run/T2902_B_PairingSum.hs
|
Haskell
|
bsd-3-clause
| 876
|
module Main where
import HIRST (mainH)
main = mainH
|
mihaimaruseac/hirst-v00
|
src/Main.hs
|
Haskell
|
mit
| 54
|
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified SDL.Image as Img
import qualified SDL as SDL
import Control.Concurrent (threadDelay)
import Control.Monad (unless)
import Linear.Affine
import Linear.V2
import Linear.V4
frameRate = 25
main :: IO ()
main = do
SDL.initialize [SDL.InitVideo]
Img.initImg [Img.InitPNG]
window <- SDL.createWindow "Example01" SDL.defaultWindow
SDL.showWindow window
--
p01SF <- Img.surfacePNG "img/p01.png"
rdr <- SDL.createRenderer window (-1) SDL.defaultRenderer
p01TX <- SDL.createTextureFromSurface rdr p01SF
SDL.freeSurface p01SF
--
let loop (x,y,i) = do
events <- SDL.pollEvents
let quit = any (== SDL.QuitEvent) $ map SDL.eventPayload events
--
SDL.clear rdr
SDL.copy rdr p01TX Nothing $ Just $ SDL.Rectangle (P $ V2 x y) (V2 30 30)
SDL.present rdr
threadDelay $ 10^6 `div` frameRate
unless (quit || i >= 15 * frameRate) $
loop (if x >= 770 then 30 else x+1 , if y >= 570 then 30 else y+2,i+1)
-- exec our main loop
loop (30,30,0)
SDL.destroyWindow window
SDL.destroyTexture p01TX
Img.quitImg
SDL.quit
|
jaiyalas/sdl2-image
|
example/unsorted/Example02.hs
|
Haskell
|
mit
| 1,241
|
import Data.List (sort)
import Text.ParserCombinators.ReadP (ReadP)
import Text.ParserCombinators.ReadPrec (lift)
import Text.Read (readPrec)
import qualified Text.ParserCombinators.ReadP as RP
import Common (rpInt)
main :: IO ()
main = do
partOne >>= print
partTwo >>= print
partOne :: IO Int
partOne = process $ paperSize . read
partTwo :: IO Int
partTwo = process $ ribbon . read
process :: (String -> Int) -> IO Int
process f = sum . map f . lines <$> readFile "../input/02.txt"
data Box = Box Int Int Int
deriving Show
-- |
-- >>> read "2x3x4" :: Box
-- Box 2 3 4
--
instance Read Box where
readPrec = lift rpBox
rpBox :: ReadP Box
rpBox = do
l <- rpInt
_ <- RP.char 'x'
w <- rpInt
_ <- RP.char 'x'
Box l w <$> rpInt
-- |
-- >>> paperSize $ read "2x3x4"
-- 58
-- >>> paperSize $ read "1x1x10"
-- 43
--
paperSize :: Box -> Int
paperSize = (+) <$> slack <*> area
slack :: Box -> Int
slack (Box l w h) = i * j
where [i,j,_] = sort [l,w,h]
-- |
-- >>> area $ read "2x3x4"
-- 52
-- >>> area $ read "1x1x10"
-- 42
--
area :: Box -> Int
area (Box l w h) = 2*l*w + 2*w*h + 2*h*l
-- |
-- >>> ribbon $ read "2x3x4"
-- 34
-- >>> ribbon $ read "1x1x10"
-- 14
--
ribbon :: Box -> Int
ribbon (Box l w h) = (i + i + j + j) + (i * j * k)
where [i,j,k] = sort [l,w,h]
|
wizzup/advent_of_code
|
2015/haskell/exe/Day02.hs
|
Haskell
|
mit
| 1,330
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Core.InputGLFW() where
import Core.Input
import qualified Graphics.UI.GLFW as GLFW
instance ToKey GLFW.Key where
toKey k = case k of
GLFW.Key'Space -> CharKey ' '
GLFW.Key'Apostrophe -> CharKey '\''
GLFW.Key'Comma -> CharKey ','
GLFW.Key'Minus -> CharKey '-'
GLFW.Key'Period -> CharKey '.'
GLFW.Key'Slash -> CharKey '/'
GLFW.Key'0 -> CharKey '0'
GLFW.Key'1 -> CharKey '1'
GLFW.Key'2 -> CharKey '2'
GLFW.Key'3 -> CharKey '3'
GLFW.Key'4 -> CharKey '4'
GLFW.Key'5 -> CharKey '5'
GLFW.Key'6 -> CharKey '6'
GLFW.Key'7 -> CharKey '7'
GLFW.Key'8 -> CharKey '8'
GLFW.Key'9 -> CharKey '9'
GLFW.Key'Semicolon -> CharKey ';'
GLFW.Key'Equal -> CharKey '='
GLFW.Key'A -> CharKey 'a'
GLFW.Key'B -> CharKey 'b'
GLFW.Key'C -> CharKey 'c'
GLFW.Key'D -> CharKey 'd'
GLFW.Key'E -> CharKey 'e'
GLFW.Key'F -> CharKey 'f'
GLFW.Key'G -> CharKey 'g'
GLFW.Key'H -> CharKey 'i'
GLFW.Key'J -> CharKey 'j'
GLFW.Key'K -> CharKey 'k'
GLFW.Key'L -> CharKey 'l'
GLFW.Key'M -> CharKey 'm'
GLFW.Key'N -> CharKey 'n'
GLFW.Key'O -> CharKey 'o'
GLFW.Key'P -> CharKey 'p'
GLFW.Key'Q -> CharKey 'q'
GLFW.Key'R -> CharKey 'r'
GLFW.Key'S -> CharKey 's'
GLFW.Key'T -> CharKey 't'
GLFW.Key'U -> CharKey 'u'
GLFW.Key'V -> CharKey 'v'
GLFW.Key'W -> CharKey 'w'
GLFW.Key'X -> CharKey 'x'
GLFW.Key'Y -> CharKey 'y'
GLFW.Key'Z -> CharKey 'z'
GLFW.Key'LeftBracket -> CharKey '['
GLFW.Key'Backslash -> CharKey '\\'
GLFW.Key'RightBracket -> CharKey ']'
GLFW.Key'GraveAccent -> CharKey '`'
v -> SpecialKey $ toSpecialKey v
instance ToKey GLFW.MouseButton where
toKey k = MouseButton $ toMouseButton k
instance ToMouseButton GLFW.MouseButton where
toMouseButton k = case k of
GLFW.MouseButton'1 -> LeftButton
GLFW.MouseButton'2 -> RightButton
GLFW.MouseButton'3 -> MiddleButton
GLFW.MouseButton'4 -> AdditionalButton 4
GLFW.MouseButton'5 -> AdditionalButton 5
GLFW.MouseButton'6 -> AdditionalButton 6
GLFW.MouseButton'7 -> AdditionalButton 7
GLFW.MouseButton'8 -> AdditionalButton 8
instance ToKeyState GLFW.KeyState where
toKeyState s = case s of
GLFW.KeyState'Pressed -> Down
GLFW.KeyState'Released -> Up
GLFW.KeyState'Repeating -> Repeating
instance ToKeyState GLFW.MouseButtonState where
toKeyState s = case s of
GLFW.MouseButtonState'Pressed -> Down
GLFW.MouseButtonState'Released -> Up
instance ToKeyState Bool where
toKeyState True = Down
toKeyState False = Up
instance ToModifiers GLFW.ModifierKeys where
toModifiers (GLFW.ModifierKeys shift' ctrl' alt' super') = Modifiers (toKeyState shift') (toKeyState ctrl') (toKeyState alt') (toKeyState super')
instance ToSpecialKey GLFW.Key where
toSpecialKey k = case k of
GLFW.Key'World1 -> KeyWorld1
GLFW.Key'World2 -> KeyWorld2
GLFW.Key'Escape -> KeyEscape
GLFW.Key'Enter -> KeyEnter
GLFW.Key'Tab -> KeyTab
GLFW.Key'Backspace -> KeyBackspace
GLFW.Key'Insert -> KeyInsert
GLFW.Key'Delete -> KeyDelete
GLFW.Key'Right -> KeyRight
GLFW.Key'Left -> KeyLeft
GLFW.Key'Down -> KeyDown
GLFW.Key'Up -> KeyUp
GLFW.Key'PageUp -> KeyPageUp
GLFW.Key'PageDown -> KeyPageDown
GLFW.Key'Home -> KeyHome
GLFW.Key'End -> KeyEnd
GLFW.Key'CapsLock -> KeyCapsLock
GLFW.Key'ScrollLock -> KeyScrollLock
GLFW.Key'NumLock -> KeyNumLock
GLFW.Key'PrintScreen -> KeyPrintScreen
GLFW.Key'Pause -> KeyPause
GLFW.Key'F1 -> KeyF1
GLFW.Key'F2 -> KeyF2
GLFW.Key'F3 -> KeyF3
GLFW.Key'F4 -> KeyF4
GLFW.Key'F5 -> KeyF5
GLFW.Key'F6 -> KeyF6
GLFW.Key'F7 -> KeyF7
GLFW.Key'F8 -> KeyF8
GLFW.Key'F9 -> KeyF9
GLFW.Key'F10 -> KeyF10
GLFW.Key'F11 -> KeyF11
GLFW.Key'F12 -> KeyF12
GLFW.Key'F13 -> KeyF13
GLFW.Key'F14 -> KeyF14
GLFW.Key'F15 -> KeyF15
GLFW.Key'F16 -> KeyF16
GLFW.Key'F17 -> KeyF17
GLFW.Key'F18 -> KeyF18
GLFW.Key'F19 -> KeyF19
GLFW.Key'F20 -> KeyF20
GLFW.Key'F21 -> KeyF21
GLFW.Key'F22 -> KeyF22
GLFW.Key'F23 -> KeyF23
GLFW.Key'F24 -> KeyF24
GLFW.Key'F25 -> KeyF25
GLFW.Key'Pad0 -> KeyPad0
GLFW.Key'Pad1 -> KeyPad1
GLFW.Key'Pad2 -> KeyPad2
GLFW.Key'Pad3 -> KeyPad3
GLFW.Key'Pad4 -> KeyPad4
GLFW.Key'Pad5 -> KeyPad5
GLFW.Key'Pad6 -> KeyPad6
GLFW.Key'Pad7 -> KeyPad7
GLFW.Key'Pad8 -> KeyPad8
GLFW.Key'Pad9 -> KeyPad9
GLFW.Key'PadDecimal -> KeyPadDecimal
GLFW.Key'PadDivide -> KeyPadDivide
GLFW.Key'PadMultiply -> KeyPadMultiply
GLFW.Key'PadSubtract -> KeyPadSubtract
GLFW.Key'PadAdd -> KeyPadAdd
GLFW.Key'PadEnter -> KeyPadEnter
GLFW.Key'PadEqual -> KeyPadEqual
GLFW.Key'LeftShift -> KeyShiftL
GLFW.Key'LeftControl -> KeyCtrlL
GLFW.Key'LeftAlt -> KeyAltL
GLFW.Key'LeftSuper -> KeySuperL
GLFW.Key'RightShift -> KeyShiftR
GLFW.Key'RightControl -> KeyCtrlR
GLFW.Key'RightAlt -> KeyAltR
GLFW.Key'RightSuper -> KeySuperR
GLFW.Key'Menu -> KeyMenu
v -> KeyUnknown (fromEnum v)
|
NCrashed/sinister
|
src/client/Core/InputGLFW.hs
|
Haskell
|
mit
| 5,161
|
module GOL where
import Data.Array.IArray
import Data.Array.Unboxed
data World = World {
gridWidth :: Int,
gridHeight :: Int,
grid :: Grid
} deriving (Show)
type Grid = UArray (Int, Int) Bool
setupGrid :: Int -> Int -> World
setupGrid x y = let cells = replicate (x*y) False
grid = listArray ((0, 0), (x-1, y-1)) cells
in World { gridWidth = x, gridHeight = y, grid = grid }
neighbourhood :: World -> (Int, Int) -> [Bool]
neighbourhood world (x, y) = map (index $ grid world) neighbourPositions
where neighbourPositions = [(a, b) | a <- [x-1..x+1], b <- [y-1..y+1], (a,b) /= (x,y)]
index grid position = grid ! torusIndex position
torusIndex (x, y) = ((w + x) `mod` w, (h + y) `mod` h)
w = gridWidth world
h = gridHeight world
liveNeighbours :: World -> (Int, Int) -> Int
liveNeighbours grid (position) = length . filter (== True) $ neighbourhood grid position
liveOrDie :: World -> (Int, Int) -> Bool
liveOrDie world position = case liveNeighbours world position of
2 -> grid world ! position
3 -> True
_ -> False
evolve :: World -> World
evolve g = g { grid = listArray ((0, 0), (gridWidth g - 1, gridHeight g - 1)) newCells }
where positions = indices $ grid g
newCells = map (liveOrDie g) positions
|
Lateks/gol
|
src/GOL.hs
|
Haskell
|
mit
| 1,414
|
-- xmonad config used by Vic Fryzel
-- Author: Vic Fryzel
-- https://github.com/vicfryzel/xmonad-config
import System.IO
import System.Exit
import XMonad
import XMonad.Hooks.DynamicLog
import XMonad.Hooks.ManageDocks
import XMonad.Hooks.ManageHelpers
import XMonad.Hooks.SetWMName
import XMonad.Layout.Fullscreen
import XMonad.Layout.NoBorders
import XMonad.Layout.Spiral
import XMonad.Layout.Tabbed
import XMonad.Layout.ThreeColumns
import XMonad.Util.Run(spawnPipe)
import XMonad.Util.EZConfig(additionalKeys)
import Graphics.X11.ExtraTypes.XF86
import qualified XMonad.StackSet as W
import qualified Data.Map as M
------------------------------------------------------------------------
-- Terminal
-- The preferred terminal program, which is used in a binding below and by
-- certain contrib modules.
--
--myTerminal = "/usr/local/bin/xfce4-terminal"
myTerminal = "/usr/local/bin/terminator -u"
-- The command to lock the screen or show the screensaver.
myScreensaver = "/usr/bin/xscreensaver-command -l"
-- The command to take a selective screenshot, where you select
-- what you'd like to capture on the screen.
mySelectScreenshot = "select-screenshot"
-- The command to take a fullscreen screenshot.
myScreenshot = "screenshot"
-- The command to use as a launcher, to launch commands that don't have
-- preset keybindings.
myLauncher = "$(yeganesh -x -- -fn 'monospace-8' -nb '#000000' -nf '#FFFFFF' -sb '#7C7C7C' -sf '#CEFFAC')"
-- Location of your xmobar.hs / xmobarrc
myXmobarrc = "~/.xmonad/xmobar-single.hs"
------------------------------------------------------------------------
-- Workspaces
-- The default number of workspaces (virtual screens) and their names.
--
myWorkspaces = ["1:web","2:work","3:code","4:docs","5:media","6:me","7:dia","8:Chat","9:Win"]
--myWorkspaces = ["1:web","2:work","3:code","4:docs","5:media"] ++ map show [6..9]
--myWorkspaces =
-- [
-- "7:Chat", "8:Dbg", "9:Win",
-- "4:Docs", "5:1Dev", "6:1Web",
-- "1:Web", "2:work", "3:Dev",
-- "0:VM", "Extr1", "Extr2"
-- ]
startupWorkspace = "2:work" -- which workspace do you want to be on after launch?
------------------------------------------------------------------------
-- Window rules
-- Execute arbitrary actions and WindowSet manipulations when managing
-- a new window. You can use this to, for example, always float a
-- particular program, or have a client always appear on a particular
-- workspace.
--
-- To find the property name associated with a program, use
-- > xprop | grep WM_CLASS
-- and click on the client you're interested in.
--
-- To match on the WM_NAME, you can use 'title' in the same way that
-- 'className' and 'resource' are used below.
--
myManageHook = composeAll
[ className =? "Chromium" --> doShift "2:web"
, className =? "Google-chrome" --> doShift "2:web"
, resource =? "desktop_window" --> doIgnore
, className =? "Galculator" --> doFloat
, className =? "Steam" --> doFloat
, className =? "Gimp" --> doFloat
, resource =? "gpicview" --> doFloat
, className =? "MPlayer" --> doFloat
, className =? "VirtualBox" --> doShift "4:vm"
, className =? "Xchat" --> doShift "5:media"
, className =? "stalonetray" --> doIgnore
, isFullscreen --> (doF W.focusDown <+> doFullFloat)]
------------------------------------------------------------------------
-- Layouts
-- You can specify and transform your layouts by modifying these values.
-- If you change layout bindings be sure to use 'mod-shift-space' after
-- restarting (with 'mod-q') to reset your layout state to the new
-- defaults, as xmonad preserves your old layout settings by default.
--
-- The available layouts. Note that each layout is separated by |||,
-- which denotes layout choice.
--
myLayout = avoidStruts (
Tall 1 (3/100) (1/2) |||
Mirror (Tall 1 (3/100) (1/2)) |||
ThreeColMid 1 (3/100) (1/2) |||
--Tall 1 (3/100) (1/2) |||
--Mirror (Tall 1 (3/100) (1/2)) |||
tabbed shrinkText tabConfig |||
Full |||
spiral (6/7)) |||
noBorders (fullscreenFull Full)
------------------------------------------------------------------------
-- Colors and borders
-- Currently based on the ir_black theme.
--
myNormalBorderColor = "#7c7c7c"
myFocusedBorderColor = "#ffb6b0"
-- Colors for text and backgrounds of each tab when in "Tabbed" layout.
tabConfig = defaultTheme {
activeBorderColor = "#7C7C7C",
activeTextColor = "#CEFFAC",
activeColor = "#000000",
inactiveBorderColor = "#7C7C7C",
inactiveTextColor = "#EEEEEE",
inactiveColor = "#000000"
}
-- Color of current window title in xmobar.
xmobarTitleColor = "#FFB6B0"
-- Color of current workspace in xmobar.
xmobarCurrentWorkspaceColor = "#CEFFAC"
-- Width of the window border in pixels.
myBorderWidth = 1
------------------------------------------------------------------------
-- Key bindings
--
-- modMask lets you specify which modkey you want to use. The default
-- is mod1Mask ("left alt"). You may also consider using mod3Mask
-- ("right alt"), which does not conflict with emacs keybindings. The
-- "windows key" is usually mod4Mask.
--
myModMask = mod4Mask
--myModMask = mod1Mask
myKeys conf@(XConfig {XMonad.modMask = modMask}) = M.fromList $
----------------------------------------------------------------------
-- Custom key bindings
--
-- Start a terminal. Terminal to start is specified by myTerminal variable.
[ ((modMask .|. shiftMask, xK_Return),
spawn $ XMonad.terminal conf)
-- Lock the screen using command specified by myScreensaver.
, ((modMask .|. controlMask, xK_l),
spawn myScreensaver)
-- Spawn the launcher using command specified by myLauncher.
-- Use this to launch programs without a key binding.
--mod+p evan
--, ((myModMask, xK_p), spawn "synapse")
-- evan 2019 mod+d
, ((myModMask, xK_d), spawn "dmenu_run")
--, ((modMask, xK_p),
-- spawn myLauncher)
-- Take a selective screenshot using the command specified by mySelectScreenshot.
, ((modMask .|. shiftMask, xK_p),
spawn mySelectScreenshot)
-- Take a full screenshot using the command specified by myScreenshot.
, ((modMask .|. controlMask .|. shiftMask, xK_p),
spawn myScreenshot)
-- Mute volume.
, ((0, xF86XK_AudioMute),
spawn "amixer -q set Master toggle")
-- Decrease volume.
, ((0, xF86XK_AudioLowerVolume),
spawn "amixer -q set Master 5%-")
-- Increase volume.
, ((0, xF86XK_AudioRaiseVolume),
spawn "amixer -q set Master 5%+")
-- Mute volume.
, ((modMask .|. controlMask, xK_m),
spawn "amixer -q set Master toggle")
-- Decrease volume.
, ((modMask .|. controlMask, xK_j),
spawn "amixer -q set Master 5%-")
-- Increase volume.
, ((modMask .|. controlMask, xK_k),
spawn "amixer -q set Master 5%+")
-- Audio previous.
, ((0, 0x1008FF16),
spawn "")
-- Play/pause.
, ((0, 0x1008FF14),
spawn "")
-- Audio next.
, ((0, 0x1008FF17),
spawn "")
-- Eject CD tray.
, ((0, 0x1008FF2C),
spawn "eject -T")
--------------------------------------------------------------------
-- "Standard" xmonad key bindings
--
-- Close focused window.
, ((modMask .|. shiftMask, xK_c),
kill)
-- Cycle through the available layout algorithms.
, ((modMask, xK_space),
sendMessage NextLayout)
-- Reset the layouts on the current workspace to default.
, ((modMask .|. shiftMask, xK_space),
setLayout $ XMonad.layoutHook conf)
-- Resize viewed windows to the correct size.
, ((modMask, xK_n),
refresh)
-- Move focus to the next window.
, ((modMask, xK_Tab),
windows W.focusDown)
-- Move focus to the next window.
, ((modMask, xK_j),
windows W.focusDown)
-- Move focus to the previous window.
, ((modMask, xK_k),
windows W.focusUp )
-- Move focus to the master window.
, ((modMask, xK_m),
windows W.focusMaster )
-- Swap the focused window and the master window.
, ((modMask, xK_Return),
windows W.swapMaster)
-- Swap the focused window with the next window.
, ((modMask .|. shiftMask, xK_j),
windows W.swapDown )
-- Swap the focused window with the previous window.
, ((modMask .|. shiftMask, xK_k),
windows W.swapUp )
-- Shrink the master area.
, ((modMask, xK_h),
sendMessage Shrink)
-- Expand the master area.
, ((modMask, xK_l),
sendMessage Expand)
-- Push window back into tiling.
, ((modMask, xK_t),
withFocused $ windows . W.sink)
-- Increment the number of windows in the master area.
, ((modMask, xK_comma),
sendMessage (IncMasterN 1))
-- Decrement the number of windows in the master area.
, ((modMask, xK_period),
sendMessage (IncMasterN (-1)))
-- Toggle the status bar gap.
-- TODO: update this binding with avoidStruts, ((modMask, xK_b),
-- Quit xmonad.
, ((modMask .|. shiftMask, xK_q),
io (exitWith ExitSuccess))
-- Restart xmonad.
, ((modMask, xK_q),
restart "xmonad" True)
]
++
-- mod-[1..9], Switch to workspace N
-- mod-shift-[1..9], Move client to workspace N
[((m .|. modMask, k), windows $ f i)
| (i, k) <- zip (XMonad.workspaces conf) [xK_1 .. xK_9]
, (f, m) <- [(W.greedyView, 0), (W.shift, shiftMask)]]
++
-- mod-{w,e,r}, Switch to physical/Xinerama screens 1, 2, or 3
-- mod-shift-{w,e,r}, Move client to screen 1, 2, or 3
[((m .|. modMask, key), screenWorkspace sc >>= flip whenJust (windows . f))
| (key, sc) <- zip [xK_w, xK_e, xK_r] [0..]
, (f, m) <- [(W.view, 0), (W.shift, shiftMask)]]
------------------------------------------------------------------------
-- Mouse bindings
--
-- Focus rules
-- True if your focus should follow your mouse cursor.
myFocusFollowsMouse :: Bool
myFocusFollowsMouse = True
myMouseBindings (XConfig {XMonad.modMask = modMask}) = M.fromList $
[
-- mod-button1, Set the window to floating mode and move by dragging
((modMask, button1),
(\w -> focus w >> mouseMoveWindow w))
-- mod-button2, Raise the window to the top of the stack
, ((modMask, button2),
(\w -> focus w >> windows W.swapMaster))
-- mod-button3, Set the window to floating mode and resize by dragging
, ((modMask, button3),
(\w -> focus w >> mouseResizeWindow w))
-- you may also bind events to the mouse scroll wheel (button4 and button5)
]
------------------------------------------------------------------------
-- Status bars and logging
-- Perform an arbitrary action on each internal state change or X event.
-- See the 'DynamicLog' extension for examples.
--
-- To emulate dwm's status bar
--
-- > logHook = dynamicLogDzen
--
------------------------------------------------------------------------
-- Startup hook
-- Perform an arbitrary action each time xmonad starts or is restarted
-- with mod-q. Used by, e.g., XMonad.Layout.PerWorkspace to initialize
-- per-workspace layout choices.
-- by evan
myStartupHook = do
spawn "/home/evan/.xmonad/autostart.sh"
-- spawnOnce "/home/evan/.xmonad/autostart.sh"
-- By default, do nothing.
--myStartupHook = return ()
------------------------------------------------------------------------
-- Run xmonad with all the defaults we set up.
--
main = do
xmproc <- spawnPipe ("xmobar " ++ myXmobarrc)
xmonad $ defaults {
logHook = dynamicLogWithPP $ xmobarPP {
ppOutput = hPutStrLn xmproc
, ppTitle = xmobarColor xmobarTitleColor "" . shorten 100
, ppCurrent = xmobarColor xmobarCurrentWorkspaceColor ""
, ppSep = " "
}
, manageHook = manageDocks <+> myManageHook
-- , startupHook = docksStartupHook <+> setWMName "LG3D"
, startupHook = setWMName "LG3D"
-- spawn "~/.xmonad/startup-hook" -- evan
, handleEventHook = docksEventHook
}
------------------------------------------------------------------------
-- Combine it all together
-- A structure containing your configuration settings, overriding
-- fields in the default config. Any you don't override, will
-- use the defaults defined in xmonad/XMonad/Config.hs
--
-- No need to modify this.
--
defaults = defaultConfig {
-- simple stuff
terminal = myTerminal,
focusFollowsMouse = myFocusFollowsMouse,
borderWidth = myBorderWidth,
modMask = myModMask,
workspaces = myWorkspaces,
normalBorderColor = myNormalBorderColor,
focusedBorderColor = myFocusedBorderColor,
-- key bindings
keys = myKeys,
mouseBindings = myMouseBindings,
-- hooks, layouts
layoutHook = smartBorders $ myLayout,
manageHook = myManageHook,
startupHook = myStartupHook
}
|
evan886/myxmonad
|
4bsd/2020/xmonad.hs
|
Haskell
|
mit
| 12,793
|
module MattermostBot.Data (
module MattermostBot.Data.Config
, module MattermostBot.Data.Slack
) where
import MattermostBot.Data.Config
import MattermostBot.Data.Slack
|
marcelbuesing/mattermost-bot
|
src/MattermostBot/Data.hs
|
Haskell
|
mit
| 177
|
{-# LANGUAGE OverloadedStrings #-}
module Test.Smoke.App.PrintResults
( printResult,
)
where
import Control.Monad (forM_, when)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Reader (ask)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.String (fromString)
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Vector as Vector
import System.IO.Error (ioeGetErrorString)
import Test.Smoke
import Test.Smoke.App.Diff
import Test.Smoke.App.OptionTypes
import Test.Smoke.App.Print
import Test.Smoke.App.PrintErrors
import Test.Smoke.Paths
import Text.Printf (printf)
data PartName = ShortName String | LongName String
printResult :: TestResult -> Output ()
printResult result@(TestResult testPlan@TestPlan {planTest = test} statusResult stdOutResult stdErrResult fileResults)
| isSuccess result =
putGreenLn " succeeded"
| otherwise = do
printFailingInput
"args"
( Text.unlines . Vector.toList . Vector.map fromString . unArgs
<$> testArgs test
)
printFailingInput "input" (planStdIn testPlan <$ testStdIn test)
printFailingOutput "status" (toAssertionResult ((<> "\n") . showInt . unStatus <$> statusResult))
printFailingOutput "stdout" stdOutResult
printFailingOutput "stderr" stdErrResult
printFailingFilesOutput fileResults
printResult (TestError _ testError) = printTestError testError
printResult (TestIgnored _) = putYellowLn " ignored"
printFailingInput :: (Foldable f, FixtureType a) => String -> f a -> Output ()
printFailingInput name value =
forM_ value $ \v -> do
putRed $ fromString $ indentedKey (" " ++ name ++ ":")
putPlainLn $ indented outputIndentation (serializeFixture v)
printFailingOutput :: FixtureType a => String -> AssertionResult a -> Output ()
printFailingOutput name = printFailures (ShortName name)
printFailingFilesOutput ::
Map (RelativePath File) (AssertionResult TestFileContents) -> Output ()
printFailingFilesOutput fileResults =
if all isSuccess (Map.elems fileResults)
then return ()
else do
putRedLn " files:"
forM_ (Map.assocs fileResults) $ uncurry printFailingFileOutput
printFailingFileOutput :: FixtureType a => RelativePath File -> AssertionResult a -> Output ()
printFailingFileOutput path = printFailures (LongName (" " ++ toFilePath path))
printFailures :: FixtureType a => PartName -> AssertionResult a -> Output ()
printFailures _ AssertionSuccess =
return ()
printFailures name (AssertionFailure (SingleAssertionFailure failure)) = do
printFailureName name (failureIsInline failure)
printFailure failure
printFailures name (AssertionFailure (MultipleAssertionFailures failures)) = do
printFailureName name isInline
printFailure firstFailure
forM_ (Vector.tail failures) $ \failure -> do
putRed " or:"
when isInline $ putPlain " "
printFailure failure
where
firstFailure = Vector.head failures
isInline = failureIsInline firstFailure
printFailureName :: PartName -> Bool -> Output ()
printFailureName (ShortName name) isInline = do
putRed $ " " <> Text.pack name <> ":"
when isInline $ putPlain $ Text.replicate (outputIndentation - length name - 3) " "
printFailureName (LongName name) _ = do
putRedLn $ " " <> Text.pack name <> ":"
putPlain $ fromString $ indentedKey ""
failureIsInline :: AssertionFailure a -> Bool
failureIsInline AssertionFailureDiff {} = True
failureIsInline AssertionFailureContains {} = False
failureIsInline AssertionFailureExpectedFileError {} = True
failureIsInline AssertionFailureActualFileError {} = True
printFailure :: FixtureType a => AssertionFailure a -> Output ()
printFailure (AssertionFailureDiff (Expected expected) (Actual actual)) =
printDiff (serializeFixture expected) (serializeFixture actual)
printFailure (AssertionFailureContains (Expected expected) (Actual actual)) = do
putPlainLn ""
putRedLn " expected to contain:"
putRedLn $ indentedAll nestedOutputIndentation (serializeFixture expected)
putRed " actual: "
putRedLn $ indented nestedOutputIndentation (serializeFixture actual)
printFailure (AssertionFailureExpectedFileError fileError (Actual actual)) = do
printFailureFileError fileError
putRed " actual: "
putRedLn $ indented nestedOutputIndentation (serializeFixture actual)
printFailure (AssertionFailureActualFileError fileError) =
printFailureFileError fileError
printFailureFileError :: SmokeFileError -> Output ()
printFailureFileError (MissingFile path) = do
putPlainLn ""
putRedLn $ " The fixture " <> showPath path <> " does not exist."
printFailureFileError (CouldNotReadFile _ exception) = do
putRedLn $ fromString (ioeGetErrorString exception)
printDiff :: Text -> Text -> Output ()
printDiff left right = do
AppOptions
{ optionsColor = color,
optionsDiffEngine = DiffEngine {engineRender = renderDiff}
} <-
ask
diff <- liftIO $ renderDiff color left right
putPlainLn $ indented outputIndentation diff
toAssertionResult :: FixtureType a => EqualityResult a -> AssertionResult a
toAssertionResult EqualitySuccess = AssertionSuccess
toAssertionResult (EqualityFailure expected actual) = AssertionFailure $ SingleAssertionFailure $ AssertionFailureDiff expected actual
indentedKey :: String -> String
indentedKey = printf ("%-" ++ show outputIndentation ++ "s")
|
SamirTalwar/Smoke
|
src/app/Test/Smoke/App/PrintResults.hs
|
Haskell
|
mit
| 5,383
|
module Mockups.Parsers.Box where
import Control.Applicative
import Data.Attoparsec.Char8
import qualified Data.ByteString.Char8 as BS
import Mockups.Parsers.Combinators
import Mockups.Elements.Element
boxParser :: Parser ContainerAttr
boxParser = vboxParser <|> hboxParser
vboxParser :: Parser ContainerAttr
vboxParser = do
Vbox <$> withOptsAttrs "vbox" parseSize
hboxParser :: Parser ContainerAttr
hboxParser = do
Hbox <$> withOptsAttrs "hbox" parseSize
parseSize :: Parser (BS.ByteString, BoxAttr)
parseSize = withAttrName "size" (BoxSize <$> decimal)
|
ostapneko/tiny-mockups
|
src/main/Mockups/Parsers/Box.hs
|
Haskell
|
mit
| 609
|
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Main where
import Nanomsg
import Test.Framework.TH (defaultMainGenerator)
import Test.Framework.Providers.QuickCheck2 (testProperty)
import Test.QuickCheck
import Test.QuickCheck.Monadic
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as C
import Control.Concurrent (threadDelay)
import Control.Applicative ( (<$>) )
import Data.Maybe (catMaybes)
instance Arbitrary ByteString where
arbitrary = C.pack <$> arbitrary
-- dummy test
prop_reverse :: [Int] -> Bool
prop_reverse xs =
xs == reverse (reverse xs)
-- test Pub and Sub sockets
prop_PubSub :: Property
prop_PubSub = monadicIO $ do
msgs <- pick arbitrary
pre $ not (null msgs)
res <- run $ do
pub <- socket Pub
ep1 <- bind pub "inproc://pubsub"
sub1 <- socket Sub
ep2 <- connect sub1 "inproc://pubsub"
subscribe sub1 $ C.pack ""
sub2 <- socket Sub
ep3 <- connect sub2 "inproc://pubsub"
subscribe sub2 $ C.pack ""
threadDelay 1000
r <- mapM (sendMsg pub sub1 sub2) msgs
unsubscribe sub2 $ C.pack ""
unsubscribe sub1 $ C.pack ""
shutdown sub2 ep3
shutdown sub1 ep2
shutdown pub ep1
close pub
close sub1
close sub2
threadDelay 1000
return r
assert $ and res
where
sendMsg pub sub1 sub2 msg = do
send pub msg
send pub msg
a <- recv sub1
b <- recv sub1
c <- recv sub2
d <- recv sub2
return $ a == msg && b == msg && c == msg && d == msg
-- test Pair sockets
prop_Pair :: Property
prop_Pair = monadicIO $ do
msgs <- pick arbitrary
pre $ not (null msgs)
res <- run $ do
s1 <- socket Pair
_ <- bind s1 "inproc://pair"
s2 <- socket Pair
_ <- connect s2 "inproc://pair"
threadDelay 1000
-- Send message from s1 to s2, then back from s2 to s1, then make sure it hasn't changed
r <- mapM (\m -> send s1 m >> recv s2 >>= send s2 >> recv s1 >>= return . (== m)) msgs
close s1
close s2
threadDelay 1000
return r
assert $ and res
-- test Pipeline (Push & Pull) sockets
prop_Pipeline :: Property
prop_Pipeline = monadicIO $ do
msgs <- pick arbitrary
pre $ not (null msgs)
res <- run $ do
push <- socket Push
_ <- bind push "inproc://pipeline"
pull1 <- socket Pull
pull2 <- socket Pull
_ <- connect pull1 "inproc://pipeline"
_ <- connect pull2 "inproc://pipeline"
threadDelay 1000
r <- mapM (testSockets push pull1 pull2) msgs
close push
close pull1
close pull2
threadDelay 1000
return r
assert $ and res
where
testSockets push pull1 pull2 msg = do
send push msg
send push msg
send push msg
threadDelay 1000
a <- recv' pull1
b <- recv' pull1
c <- recv' pull1
d <- recv' pull2
e <- recv' pull2
f <- recv' pull2
let xs = catMaybes [a, b, c, d, e, f]
return $ all (== msg) xs && (length xs == 3)
-- test Req and Rep sockets
prop_ReqRep :: Property
prop_ReqRep = monadicIO $ do
msgs <- pick arbitrary
pre $ not (null msgs)
res <- run $ do
req <- socket Req
_ <- bind req "inproc://reqrep"
rep <- socket Rep
_ <- connect rep "inproc://reqrep"
threadDelay 1000
r <- mapM (\m -> send req m >> recv rep >>= send rep >> recv req >>= return . (== m)) msgs
close req
close rep
threadDelay 1000
return r
assert $ and res
-- test Bus socket
prop_Bus :: Property
prop_Bus = monadicIO $ do
msgs <- pick arbitrary
pre $ not (null msgs)
res <- run $ do
-- Probably not how you're supposed to connect Bus nodes..
b1 <- socket Bus
_ <- bind b1 "inproc://bus1"
b2 <- socket Bus
_ <- connect b2 "inproc://bus1"
_ <- bind b2 "inproc://bus2"
b3 <- socket Bus
_ <- connect b3 "inproc://bus2"
_ <- bind b3 "inproc://bus3"
_ <- connect b1 "inproc://bus3"
threadDelay 1000
r <- mapM (testSockets b1 b2 b3) msgs
close b1
close b2
close b3
threadDelay 1000
return r
assert $ and res
where
testSockets b1 b2 b3 msg = do
send b1 msg
a <- recv b2
b <- recv b3
send b2 msg
c <- recv b1
d <- recv b3
send b3 msg
e <- recv b1
f <- recv b2
return $ all (== msg) [a, b, c, d, e, f]
prop_TestOptions :: Property
prop_TestOptions = monadicIO $ do
res <- run $ do
req <- socket Req
_ <- bind req "tcp://*:5560"
surveyor <- socket Surveyor
_ <- bind surveyor "inproc://surveyor"
threadDelay 1000
setTcpNoDelay req 1
v1 <- tcpNoDelay req
setTcpNoDelay req 0
v2 <- tcpNoDelay req
setRequestResendInterval req 30000
v3 <- requestResendInterval req
setIpv4Only req 0
v4 <- ipv4Only req
setIpv4Only req 1
v5 <- ipv4Only req
setSndPrio req 7
v6 <- sndPrio req
setReconnectInterval req 50
v7 <- reconnectInterval req
setReconnectIntervalMax req 400
v8 <- reconnectIntervalMax req
setRcvBuf req 200000
v9 <- rcvBuf req
setSndBuf req 150000
v10 <- sndBuf req
setLinger req 500
v11 <- linger req
setSurveyorDeadline surveyor 2000
v12 <- surveyorDeadline surveyor
close req
close surveyor
threadDelay 1000
return [v1 == 1, v2 == 0, v3 == 30000, v4 == 0, v5 == 1, v6 == 7,
v7 == 50, v8 == 400, v9 == 200000, v10 == 150000, v11 == 500, v12 == 2000]
assert $ and res
main :: IO ()
main = $defaultMainGenerator
|
christianlavoie/nanomsg-haskell
|
tests/Properties.hs
|
Haskell
|
mit
| 6,271
|
longest :: [String] -> String -> String
longest (word:rest) current =
if length word > length current
then longest rest word
else longest rest current
longest [] current = current
longestWord :: String -> String
longestWord text = longest (words text) ""
main :: IO ()
main = do
contents <- getContents
print $ length $ longestWord contents
|
considerate/progp
|
Haskell/longest.hs
|
Haskell
|
mit
| 349
|
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module KMC.SymbolicFST
(FST(..)
,Edge
,fstStateSize
,fstTransSize
,edgesToList
,edgesFromList
,fstEvalEpsilonEdges
,fstEdges
,fstAbstractEvalEdgesAll
,isChoiceState
,isSkipState
,isJoinState
,coarsestPredicateSet
,prefixTests
,rightClosure,rightInputClosure
,mapEdges
,trim
,eForward,eBackward,eForwardEpsilon,eBackwardEpsilon
,enumerateStates
,run
,runSequential
,runBacktracking
) where
import Control.Applicative
import Control.Monad (guard)
import Data.Map ((!))
import qualified Data.Map as M
import Data.Maybe (maybeToList)
import Data.Monoid
import qualified Data.Set as S
import KMC.Backtracking
import KMC.Theories
import Prelude
-- | (Non-)deterministic Finite State Transducer. To be well-formed, the
-- following conditions must be met:
--
-- (i) For any state s, the only transitions out of s are epsilon-transitions or
-- symbol transitions, but not both.
data FST q pred func =
FST
{ fstS :: S.Set q
, fstE :: OrderedEdgeSet q pred func
, fstI :: q
, fstF :: S.Set q
}
-- | Type synonym for a transition. A transition is either labeled by a
-- predicate and an output function; or it is labeled by an output string. This
-- models a set of transitions between the two states where the output label may
-- depend on the symbol being read; in the case of epsilon-transitions, a plain
-- output string is provided.
type Edge q pred func = (q, Either (pred, func) (Rng func), q)
-- | An ordered edge set with fast reverse lookup.
data OrderedEdgeSet q pred func =
OrderedEdgeSet { eForward :: M.Map q [(pred,func,q)]
, eBackward :: M.Map q [(pred,func,q)]
, eForwardEpsilon :: M.Map q [(Rng func, q)]
, eBackwardEpsilon :: M.Map q [(Rng func, q)]
}
deriving instance (Eq q, Eq pred, Eq func, Eq (Rng func)) => Eq (OrderedEdgeSet q pred func)
deriving instance (Ord q, Ord pred, Ord func, Ord (Rng func)) => Ord (OrderedEdgeSet q pred func)
deriving instance (Show q, Show pred, Show func, Show (Rng func)) => Show (OrderedEdgeSet q pred func)
deriving instance (Eq q, Eq pred, Eq func, Eq (Rng func)) => Eq (FST q pred func)
deriving instance (Ord q, Ord pred, Ord func, Ord (Rng func)) => Ord (FST q pred func)
deriving instance (Show q, Show pred, Show func, Show (Rng func)) => Show (FST q pred func)
-- | Construct an edge set from a list of edges
edgesFromList :: (Ord q) => [Edge q pred func] -> OrderedEdgeSet q pred func
edgesFromList es =
OrderedEdgeSet
{ eForward = M.fromListWith (flip (++)) [ (q, [(a, b, q')]) | (q, Left (a,b), q') <- es ]
, eBackward = M.fromListWith (flip (++)) [ (q', [(a, b, q)]) | (q, Left (a,b), q') <- es ]
, eForwardEpsilon = M.fromListWith (flip (++)) [(q, [(y, q')]) | (q, Right y, q') <- es ]
, eBackwardEpsilon = M.fromListWith (flip (++)) [(q', [(y,q)]) | (q, Right y, q') <- es ]
}
-- | Get the number of states in the FST
fstStateSize :: FST q pred func -> Int
fstStateSize = S.size . fstS
-- | Get the number of transitions in the FST
fstTransSize :: FST q pred func -> Int
fstTransSize fst' = M.size (eForward (fstE fst')) + M.size (eForwardEpsilon (fstE fst'))
{-
unionEdges :: (Ord q) => OrderedEdgeSet q pred func
-> OrderedEdgeSet q pred func
-> OrderedEdgeSet q pred func
unionEdges es es' =
OrderedEdgeSet
{ eForward = M.unionWith (++) (eForward es) (eForward es')
, eBackward = M.unionWith (++) (eBackward es) (eBackward es')
, eForwardEpsilon = M.unionWith (++) (eForwardEpsilon es) (eForwardEpsilon es')
, eBackwardEpsilon = M.unionWith (++) (eBackwardEpsilon es) (eBackwardEpsilon es')
}
-}
flipEdges :: OrderedEdgeSet q pred func -> OrderedEdgeSet q pred func
flipEdges es = OrderedEdgeSet {eForward = eBackward es
,eBackward = eForward es
,eForwardEpsilon = eBackwardEpsilon es
,eBackwardEpsilon = eForwardEpsilon es}
-- | Get the underlying list representation of an ordered edge set
edgesToList :: OrderedEdgeSet q pred func -> [(q, Either (pred, func) (Rng func), q)]
edgesToList es = symEdgesToList (eForward es) ++ epsEdgesToList (eForwardEpsilon es)
-- | Get the list representation of only the symbol edges in an ordered edge set
symEdgesToList :: M.Map q [(pred, func, q)] -> [(q, Either (pred, func) (Rng func), q)]
symEdgesToList es = [ (q, Left (a,b), q') | (q, xs) <- M.toList es, (a,b,q') <- xs ]
-- | Get the list representation of only the epsilon edges in an ordered edge set
epsEdgesToList :: M.Map q [(Rng func, q)] -> [(q, Either (pred, func) (Rng func), q)]
epsEdgesToList es = [ (q, Right y, q') | (q, xs) <- M.toList es, (y, q') <- xs ]
-- | Map four functions over the edges in an FST. Symbol edges may be turned
-- into epsilon edges and vice versa.
mapEdges :: (Ord q)
=> (q -> [(pred1,func1,q)] -> [(pred2,func2,q)])
-> (q -> [(pred1,func1,q)] -> [(Rng func2, q)])
-> (q -> [(Rng func1, q)] -> [(pred2,func2,q)])
-> (q -> [(Rng func1, q)] -> [(Rng func2, q)])
-> FST q pred1 func1 -> FST q pred2 func2
mapEdges symsym symeps epssym epseps fst' =
fst' { fstE = edgesFromList (symEdges1 ++ symEdges2 ++ epsEdges1 ++ epsEdges2) }
where
esym = eForward $ fstE fst'
eeps = eForwardEpsilon $ fstE fst'
symEdges1 = symEdgesToList $ M.mapWithKey symsym $ esym
symEdges2 = symEdgesToList $ M.mapWithKey epssym $ eeps
epsEdges1 = epsEdgesToList $ M.mapWithKey symeps $ esym
epsEdges2 = epsEdgesToList $ M.mapWithKey epseps $ eeps
-- | Given an input symbol and a starting state, compute the concrete set of
-- transitions from that state.
evalEdges :: (Ord st
,Function func
,SetLike pred (Dom func))
=> OrderedEdgeSet st pred func
-> st -> Dom func -> [(Rng func, st)]
evalEdges (OrderedEdgeSet { eForward = me }) q x =
case M.lookup q me of
Nothing -> []
Just es -> concatMap evalEdge es
where
evalEdge (p, f, q')
| member x p = [(eval f x, q')]
| otherwise = []
-- | Given a state, compute the concrete set of epsilon-transitions from that
-- state.
evalEpsilonEdges :: (Ord st) => OrderedEdgeSet st pred func
-> st -> [(Rng func, st)]
evalEpsilonEdges (OrderedEdgeSet { eForwardEpsilon = meps }) q =
case M.lookup q meps of
Nothing -> []
Just es -> es
-- | If abstractEvalEdgesAll fst q phi == [(f1, q1), ..., (fn, qn)], then
-- for all qi and for all a in [[phi]], q steps to qi reading a.
abstractEvalEdgesAll :: (Ord st
,PartialOrder pred)
=>
OrderedEdgeSet st pred func
-> st -> pred -> [(func, st)]
abstractEvalEdgesAll (OrderedEdgeSet { eForward = me}) q p =
case M.lookup q me of
Nothing -> []
Just es -> [ (f, q') | (p', f, q') <- es, p `lte` p' ]
-- | Like evalEpsilonEdges, but given an FST.
fstEvalEpsilonEdges :: (Ord st) => FST st pred func -> st -> [(Rng func, st)]
fstEvalEpsilonEdges aut = evalEpsilonEdges (fstE aut)
-- | Get the list of forward symbol edges from a state
fstEdges :: (Ord st) => FST st pred func -> st -> [(pred, func, st)]
fstEdges fst' q =
case M.lookup q (eForward $ fstE fst') of
Nothing -> []
Just es -> es
-- | Like fstEvalEdges, but given an FST.
fstEvalEdges :: (Ord st
,Function func
,SetLike pred (Dom func))
=> FST st pred func -> st -> Dom func -> [(Rng func, st)]
fstEvalEdges fst' q a = evalEdges (fstE fst') q a
-- | Like abstractEvalEdgesAll, but given an FST.
fstAbstractEvalEdgesAll :: (Ord st
,PartialOrder pred)
=> FST st pred func -> st -> pred -> [(func, st)]
fstAbstractEvalEdgesAll aut = abstractEvalEdgesAll (fstE aut)
-- | Is the given state a non-deterministic choice state, or an input action
-- state?
isChoiceState :: (Ord st) => FST st pred func -> st -> Bool
isChoiceState fst' q =
case (M.member q (eForward . fstE $ fst'), M.member q (eForwardEpsilon . fstE $ fst')) of
(True, True) -> error "Inconsistent FST - a state is both a choice and symbol state"
(_, b) -> b
isSkipState :: (Ord st) => FST st pred func -> st -> Bool
isSkipState fst' q =
case M.lookup q (eForwardEpsilon . fstE $ fst') of
Just [_] -> True
_ -> False
isJoinState :: (Ord st) => FST st pred func -> st -> Bool
isJoinState fst' q =
let nBackEdges = length (concat (maybeToList (M.lookup q (eBackwardEpsilon . fstE $ fst'))))
+ length (concat (maybeToList (M.lookup q (eBackward . fstE $ fst'))))
in nBackEdges > 1
-- | Given a state set A (represented as a list), compute the coarsest predicate
-- set obtained from the set
-- { p | p is a predicate on a transition starting in q, q in A }
coarsestPredicateSet :: (Boolean pred
,PartialOrder pred
,Ord st
,Ord pred) =>
FST st pred func
-> [st]
-> [pred]
coarsestPredicateSet fst' qs = coarsestPartition ps
where
ps = S.toList $ S.fromList
[ p | q <- qs
, (p, _, _) <- maybe [] id (M.lookup q (eForward . fstE $ fst')) ]
-- | Compute an ordered right closure with output
rightClosure :: (Ord st, Monoid (Rng func)) => FST st pred func -> st -> [(Rng func, st)]
rightClosure fst' = snd . go S.empty mempty
where
go vis out q =
case fstEvalEpsilonEdges fst' q of
[] -> (vis, [(out, q)])
xs -> foldl (\(vis', acc) (w, q') ->
if S.member q' vis' then
(vis', acc)
else
let (vis'', ys) = go (S.insert q' vis') (mappend out w) q'
in (vis'', acc ++ ys))
(vis, [])
xs
---------------
-- LCP analysis
---------------
-- | Compute the longest deterministic prefix for a pointed state set
-- (A, q).
ldp :: (Ord st, Ord pred, PartialOrder pred, Boolean pred) =>
FST st pred func -> S.Set st -> st -> [pred]
ldp fst' = go
where
go ctx q =
case M.lookup q (eForward $ fstE fst') of
Just [(p,_,q')] | p `elem` (coarsestPredicateSet fst' $ S.toList ctx) ->
p:go (stepAll fst' p ctx) q'
_ -> []
-- | Given a set A and a predicate p, compute the set A' of states that can be reached via a
-- transition from A labeled by a predicate containing p.
stepAll :: (Ord st, PartialOrder pred) => FST st pred func -> pred -> S.Set st -> S.Set st
stepAll fst' p = S.unions . map aux . S.toList
where
aux q = S.unions $ map (rightInputClosure fst' . snd)
$ fstAbstractEvalEdgesAll fst' q p
-- | Compute an unordered right closure without output on the input automaton of an FST
rightInputClosure :: (Ord st) => FST st pred func -> st -> S.Set st
rightInputClosure fst' = snd . go S.empty
where
go vis q =
case fstEvalEpsilonEdges fst' q of
[] -> (vis, S.singleton q)
xs -> foldl (\(vis', acc) (_, q') ->
if S.member q' vis then
(vis', acc)
else
let (vis'', ys) = go (S.insert q' vis') q'
in (vis'', S.union acc ys))
(vis, S.empty)
xs
prefixTests :: (Boolean pred, PartialOrder pred, Ord st, Ord pred) =>
FST st pred func
-> Bool
-> [st]
-> [([pred], S.Set st)]
prefixTests fst' singletonMode states =
[ (t, killed t) | t <- tests ]
where
ldps = [ (ldp fst' (S.fromList states) q, q) | not singletonMode, q <- states ]
tests = S.toList $ S.fromList $ [ [p] | p <- coarsestPredicateSet fst' states ] ++ map fst ldps
killed t = S.fromList [ q | (ps, q) <- ldps, not (t `entails` ps) ]
entails _ [] = True
entails (t:ts) (p:ps) = (t `eq` p) && (ts `entails` ps)
entails [] (_:_) = False
-- | Substitute state type by any enumerable type.
enumerateStates :: (Ord st, Ord a, Enum a) => FST st pred func -> FST a pred func
enumerateStates fst' =
FST { fstS = S.fromList (M.elems statesMap)
, fstE = edgesFromList
[ (aux q, lbl, aux q') | (q, lbl, q') <- edgesToList (fstE fst') ]
, fstI = aux (fstI fst')
, fstF = S.fromList [ aux q | q <- S.toList (fstF fst') ]
}
where
statesMap = M.fromList (zip (S.toList (fstS fst')) [toEnum 0..])
aux q = statesMap M.! q
accessibleStates :: (Ord q, PartialOrder pred, Boolean pred)
=> OrderedEdgeSet q pred func -> S.Set q -> S.Set q
accessibleStates es initialWS = go initialWS S.empty
where
go ws acc
| S.null ws = acc
| (q, ws') <- S.deleteFindMin ws =
let succs = S.fromList $ map snd (abstractEvalEdgesAll es q bot)
++ map snd (evalEpsilonEdges es q)
in go (S.union ws' (S.difference succs acc)) (S.insert q acc)
coaccessibleStates :: (Ord q, PartialOrder pred, Boolean pred)
=> OrderedEdgeSet q pred func -> S.Set q -> S.Set q
coaccessibleStates es = accessibleStates (flipEdges es)
trim :: (Ord q, PartialOrder pred, Boolean pred)
=> FST q pred func -> FST q pred func
trim fst' =
FST { fstI = fstI fst'
, fstF = S.intersection (fstF fst') useful
, fstS = useful
, fstE = edgesFromList [ (q, lbl, q') | (q, lbl, q') <- edgesToList (fstE fst')
, S.member q useful
, S.member q' useful ]
}
where
useful = S.intersection (accessibleStates (fstE fst') (S.singleton (fstI fst')))
(coaccessibleStates (fstE fst') (fstF fst'))
-------------
-- Simulation
-------------
-- | Simulate a non-deterministic FST with single-symbol predicates
run :: (Function func
,SetLike pred (Dom func)
,Monoid (Rng func)
,Ord st)
=> FST st pred func -> [Dom func] -> [Rng func]
run fst' inp = do
(os, q') <- go [ ([o], q') | (o,q') <- rightClosure fst' (fstI fst') ] inp
guard (S.member q' (fstF fst'))
return (mconcat $ reverse os)
where
go s [] = s
go s (a:as) = go (close . step a $ s) as
close s = prune S.empty [ (o':os, q') | (os, q) <- s, (o', q') <- rightClosure fst' q ]
prune _ [] = []
prune vis ((os,q):s) | S.member q vis = prune vis s
| otherwise = (os,q):prune (S.insert q vis) s
step a s = [ (o':os, q') | (os, q) <- s, (o',q') <- fstEvalEdges fst' q a ]
-- | Simulate a sequential machine with multi-symbol predicates
runSequential :: (Function func
,Dom func ~ [a]
,UniformListSet pred a
,Monoid (Rng func)
,Ord st)
=> FST st pred func -> [a] -> Maybe (Rng func)
runSequential fst' = go (fstI fst')
where
go q [] = if S.member q (fstF fst') then Just mempty else Nothing
go q w | isChoiceState fst' q = do
[(o, q')] <- pure (fstEvalEpsilonEdges fst' q)
o' <- go q' w
return $ mappend o o'
go q w = do
ts <- M.lookup q (eForward $ fstE fst')
[(v, o, q')] <- pure [ (v, eval f u, q')
| (p, f, q') <- ts
, let n = listLength p
, let (u,v) = splitAt n w
, member u p ]
o' <- go q' v
return $ mappend o o'
runBacktracking :: (Monoid m, Function func, SetLike pred (Dom func), Stream s (Dom func))
=> (Rng func -> m)
-> FST Int pred func
-> P s m
runBacktracking phi fst' = lfp!(fstI fst') <* eof
where
lfp = M.fromList [ (q, go q) | q <- S.toList $ fstS fst' ]
go q =
(if isJoinState fst' q then barrier q else pure ())
*> case fstEvalEpsilonEdges fst' q of
[] -> case fstEdges fst' q of
[] -> pure mempty
es -> consume es
-- TODO: Only insert fBarrier if q is on an epsilon-loop
es -> fBarrier q *> choose es
consume es =
foldl1 (<|>)
[ (mappend . phi . eval func <$> litp (flip member p)) <*> (lfp!q')
| (p, func, q') <- es ]
choose [] = empty
choose es = foldl1 (<|>) [ mappend (phi y) <$> (lfp!q') | (y, q') <- es ]
{-
-- | Example: This is how to interpret an action FST
interpAction :: (Function func
,SetLike pred Word8
,Dom func ~ Word8
,Rng func ~ [Either Word8 RegAction])
=> FST Int pred func -> ByteString -> Maybe Action
interpAction fst' b = runP' (runBacktracking (mconcat . map adjActionSem) fst') (0::Int, b)
-}
|
diku-kmc/kleenexlang
|
src/KMC/SymbolicFST.hs
|
Haskell
|
mit
| 17,283
|
{-# LANGUAGE OverloadedStrings #-}
-- | Legacy types from Keter version 0.4. Retained to keep backwards
-- compatibility in config file format.
module Keter.Types.V04 where
import Control.Applicative
import Data.Aeson
import Data.Bool
import Data.Conduit.Network (HostPreference)
import Data.Default
import qualified Data.Set as Set
import Data.String (fromString)
import Data.Yaml.FilePath
import qualified System.FilePath as F
import Keter.Types.Common
import Network.HTTP.ReverseProxy.Rewrite
import qualified Network.Wai.Handler.Warp as Warp
import qualified Network.Wai.Handler.WarpTLS as WarpTLS
import qualified Network.TLS.SessionManager as TLSSession
import Prelude hiding (FilePath)
data AppConfig = AppConfig
{ configExec :: F.FilePath
, configArgs :: [Text]
, configHost :: Text
, configSsl :: Bool
, configExtraHosts :: Set Text
, configRaw :: Object
}
instance ParseYamlFile AppConfig where
parseYamlFile basedir = withObject "AppConfig" $ \o -> AppConfig
<$> lookupBase basedir o "exec"
<*> o .:? "args" .!= []
<*> o .: "host"
<*> o .:? "ssl" .!= False
<*> o .:? "extra-hosts" .!= Set.empty
<*> return o
data BundleConfig = BundleConfig
{ bconfigApp :: Maybe AppConfig
, bconfigStaticHosts :: Set StaticHost
, bconfigRedirects :: Set Redirect
}
instance ParseYamlFile BundleConfig where
parseYamlFile basedir = withObject "BundleConfig" $ \o -> BundleConfig
<$> ((Just <$> parseYamlFile basedir (Object o)) <|> pure Nothing)
<*> lookupBaseMaybe basedir o "static-hosts" .!= Set.empty
<*> o .:? "redirects" .!= Set.empty
data StaticHost = StaticHost
{ shHost :: Text
, shRoot :: FilePath
}
deriving (Eq, Ord)
instance ParseYamlFile StaticHost where
parseYamlFile basedir = withObject "StaticHost" $ \o -> StaticHost
<$> o .: "host"
<*> lookupBase basedir o "root"
data Redirect = Redirect
{ redFrom :: Text
, redTo :: Text
}
deriving (Eq, Ord)
instance FromJSON Redirect where
parseJSON (Object o) = Redirect
<$> o .: "from"
<*> o .: "to"
parseJSON _ = fail "Wanted an object"
data KeterConfig = KeterConfig
{ kconfigDir :: F.FilePath
, kconfigPortMan :: PortSettings
, kconfigHost :: HostPreference
, kconfigPort :: Port
, kconfigSsl :: Maybe TLSConfig
, kconfigSetuid :: Maybe Text
, kconfigReverseProxy :: Set ReverseProxyConfig
, kconfigIpFromHeader :: Bool
, kconfigConnectionTimeBound :: Int
-- ^ Maximum request time in milliseconds per connection.
}
instance Default KeterConfig where
def = KeterConfig
{ kconfigDir = "."
, kconfigPortMan = def
, kconfigHost = "*"
, kconfigPort = 80
, kconfigSsl = Nothing
, kconfigSetuid = Nothing
, kconfigReverseProxy = Set.empty
, kconfigIpFromHeader = False
, kconfigConnectionTimeBound = fiveMinutes
}
-- | Default connection time bound in milliseconds.
fiveMinutes :: Int
fiveMinutes = 5 * 60 * 1000
instance ParseYamlFile KeterConfig where
parseYamlFile basedir = withObject "KeterConfig" $ \o -> KeterConfig
<$> lookupBase basedir o "root"
<*> o .:? "port-manager" .!= def
<*> (fmap fromString <$> o .:? "host") .!= kconfigHost def
<*> o .:? "port" .!= kconfigPort def
<*> (o .:? "ssl" >>= maybe (return Nothing) (fmap Just . parseYamlFile basedir))
<*> o .:? "setuid"
<*> o .:? "reverse-proxy" .!= Set.empty
<*> o .:? "ip-from-header" .!= False
<*> o .:? "connection-time-bound" .!= fiveMinutes
data TLSConfig = TLSConfig !Warp.Settings !WarpTLS.TLSSettings
instance ParseYamlFile TLSConfig where
parseYamlFile basedir = withObject "TLSConfig" $ \o -> do
cert <- lookupBase basedir o "certificate"
key <- lookupBase basedir o "key"
host <- (fmap fromString <$> o .:? "host") .!= "*"
port <- o .:? "port" .!= 443
session <- bool Nothing (Just TLSSession.defaultConfig) <$> o .:? "session" .!= False
return $! TLSConfig
( Warp.setHost host
$ Warp.setPort port
Warp.defaultSettings)
WarpTLS.defaultTlsSettings
{ WarpTLS.certFile = cert
, WarpTLS.keyFile = key
, WarpTLS.tlsSessionManagerConfig = session
}
-- | Controls execution of the nginx thread. Follows the settings type pattern.
-- See: <http://www.yesodweb.com/book/settings-types>.
data PortSettings = PortSettings
{ portRange :: [Port]
-- ^ Which ports to assign to apps. Defaults to unassigned ranges from IANA
}
instance Default PortSettings where
def = PortSettings
-- Top 10 Largest IANA unassigned port ranges with no unauthorized uses known
{ portRange = [43124..44320]
++ [28120..29166]
++ [45967..46997]
++ [28241..29117]
++ [40001..40840]
++ [29170..29998]
++ [38866..39680]
++ [43442..44122]
++ [41122..41793]
++ [35358..36000]
}
instance FromJSON PortSettings where
parseJSON = withObject "PortSettings" $ \_ -> PortSettings
<$> return (portRange def)
|
tolysz/keter
|
Keter/Types/V04.hs
|
Haskell
|
mit
| 5,798
|
module Main where
import Criterion.Main
import Control.Lens
import Control.Monad.Trans.Class ( lift )
import Control.Monad.Trans.Either ( EitherT(..)
, runEitherT
, hoistEither )
import Data.Default ( def )
import qualified Data.Vector as V
import Linear.V2
import TrueSkill ( Parameter(..)
, Player
, skills
, makeSkills
, fromMuSigma2
, sigmaOffense
, sigmaDefense
)
import TrueSkill.Autodiff hiding ( lift )
import Train
import Types
-- | Default player skill mean.
defaultMuOffense :: Floating d => d
defaultMuOffense = 3.0 / 11.0
-- | Default player skill standard deviation.
defaultSigmaOffense :: Floating d => d
defaultSigmaOffense = 0.1
-- | Default player skill mean.
defaultMuDefense :: Floating d => d
defaultMuDefense = 1.0 / 11.0
-- | Default player skill standard deviation.
defaultSigmaDefense :: Floating d => d
defaultSigmaDefense = 0.1
defaultParameter :: Floating d => Parameter d
defaultParameter = def
{ _sigmaOffense = defaultSigmaOffense / 5.0
, _sigmaDefense = defaultSigmaDefense / 5.0
}
defaultPlayer :: Floating d => Player d
defaultPlayer = skills .~ makeSkills
(fromMuSigma2 defaultMuOffense (defaultSigmaOffense**2))
(fromMuSigma2 defaultMuDefense (defaultSigmaDefense**2))
$ def
buildEval :: V.Vector Game -> V.Vector Game -> Double
buildEval trainData valData = value
where
value = objective 3 trainData valData
[ defaultParameter ^. sigmaOffense
, defaultParameter ^. sigmaDefense
, defaultMuOffense
, defaultSigmaOffense**2
, defaultMuDefense
, defaultSigmaDefense**2
]
gradEval :: V.Vector Game -> V.Vector Game -> (Double, V2 Double)
gradEval trainData valData = value `seq` grad `seq` (value, grad)
where
-- For testing with the `ad' package on hackage.
-- (value, grad) = grad' objective' [ defaultParameter ^. sigmaOffense
-- , defaultParameter ^. sigmaDefense
-- ]
-- objective' :: (Floating d, Ord d) => [d] -> d
-- objective' [a, b] = objective trainData valData [ a
-- , b
-- , defaultMuOffense
-- , defaultSigmaOffense^2
-- , defaultMuDefense
-- , defaultSigmaDefense^2
-- ]
AD value grad = objective 3 trainData valData initial
initial :: [AD]
initial = map (uncurry makeAD) $ zip [ defaultParameter ^. sigmaOffense
, defaultParameter ^. sigmaDefense
, defaultMuOffense
, defaultSigmaOffense^(2 :: Int)
, defaultMuDefense
, defaultSigmaDefense^(2 :: Int)
] [0..]
benchmark :: V.Vector Game -> V.Vector Game -> IO ()
benchmark trainData valData = defaultMain [
bgroup "TrueSkill" [ bench "grad" $ whnf (gradEval trainData) valData
, bench "eval" $ whnf (buildEval trainData) valData
]
]
main :: IO ()
main = do
r <- runEitherT $ do
trainData <- hoistEither =<<
(lift $
readGamesFromCsv "bundesliga/shuffled_train.csv")
valData <- hoistEither =<<
(lift $
readGamesFromCsv "bundesliga/shuffled_validation.csv")
lift $ benchmark trainData valData
-- lift $ print $ gradEval trainData valData
case r of
Left err -> putStrLn err
Right _ -> return ()
|
mkiefel/trueskill
|
bench_app.hs
|
Haskell
|
bsd-2-clause
| 4,239
|
{-# LANGUAGE BangPatterns #-}
import Data.List
import Data.Maybe
data Mintree = Leaf !Integer !Integer
| Node !Integer !Integer !Integer !Mintree !Mintree deriving Show
tmin (Leaf m _) = m
tmin (Node m _ _ _ _) = m
buildtree xs = snd (buildtree' 0 (fromIntegral $ (length xs) - 1) xs)
buildtree' a b xs@(x:xs') =
if a == b
then (xs', Leaf x a)
else let c = a + (b - a) `div` 2
(xs', left) = buildtree' a c xs
(xs'', right) = buildtree' (c + 1) b xs'
in (xs'', Node (min (tmin left) (tmin right)) a b left right)
mm a Nothing = a
mm Nothing b = b
mm (Just a) (Just b) = Just $ min a b
findmin (Leaf m a') a b = if a' >= a && a' <= b then Just m else Nothing
findmin (Node m a' b' l r) a b =
if a <= a' && b' <= b
then Just m
else if b < a' || b' < a
then Nothing
else mm (findmin l a b) (findmin r a b)
tst t = do
l <- getLine
let (a:b:[]) = str2numlist l
putStrLn (show (fromJust $ findmin t a b))
main = do
l1 <- getLine
let (n:m:[]) = str2numlist l1
l2 <- getLine
let !t = buildtree $ str2numlist l2
mapM_ (const (tst t)) [1 .. m]
str2numlist :: String -> [Integer]
str2numlist = (map read) . explode
explode = unfoldr f
where f !str = let (!chunk, !rest) = span (/= ' ') str
in if null chunk
then Nothing
else if null rest
then Just (chunk, rest)
else Just (chunk, tail rest)
|
pbl64k/HackerRank-Contests
|
2014-02-07-FP/RangeMinimumQuery/rmq.accepted.hs
|
Haskell
|
bsd-2-clause
| 1,587
|
-- | fluid_log is not present because it is a vararg function
-- and ffi to them is deprecated:
--
-- \"Note that for a C function defined to accept a variable number of arguments,
-- all arguments beyond the explicitly typed arguments suffer argument promotion.
-- However, because C permits the calling convention to be different for such
-- functions, a Haskell system will, in general, not be able to make use of
-- variable argument functions. Hence, their use is deprecated in portable code.\"
--
-- from <http://www.haskell.org/onlinereport/haskell2010/haskellch8.html>
-- (8.5.1 at the very end)
module Sound.Fluidsynth.Log
( LogLevel(..)
, Logger
, defaultLogger
, setLogger
) where
import Control.Monad.Trans(liftIO)
import Control.Exception(assert)
import Foreign.C.String(peekCString, withCString)
import Foreign.Ptr(nullPtr)
import Sound.Fluidsynth.Internal.Types
import Sound.Fluidsynth.Internal.FFI.Log
data LogLevel
= LogLevelPanic
| LogLevelError
| LogLevelWarning
| LogLevelInfo
| LogLevelDebug
llToC :: LogLevel -> C'fluid_log_level
llToC LogLevelPanic = c'FLUID_PANIC
llToC LogLevelError = c'FLUID_ERR
llToC LogLevelWarning = c'FLUID_WARN
llToC LogLevelInfo = c'FLUID_INFO
llToC LogLevelDebug = c'FLUID_DBG
llFromC :: C'fluid_log_level -> LogLevel
llFromC ll =
case () of
_ | ll == c'FLUID_PANIC -> LogLevelPanic
| ll == c'FLUID_ERR -> LogLevelError
| ll == c'FLUID_WARN -> LogLevelWarning
| ll == c'FLUID_INFO -> LogLevelInfo
| ll == c'FLUID_DBG -> LogLevelDebug
| otherwise -> assert False undefined
type Logger = LogLevel -> String -> FluidSynth ()
-- | Will just print to stderr
defaultLogger :: Logger
defaultLogger lvl str = FluidSynth $
liftIO $ withCString str $ \cstr ->
c'fluid_default_log_function (llToC lvl) cstr nullPtr
-- | Set custom 'Logger' for a specific 'LogLevel'
setLogger :: LogLevel
-> Logger -- ^ new 'Logger'
-> FluidSynth Logger -- ^ previous 'Logger'
setLogger ll logger = do
dataPtr <- fluidDataPtr
cback <- liftIO $ mk'fluid_log_function_t callback
prevcback <- liftIO $ c'fluid_set_log_function (llToC ll) cback dataPtr
return $ fromCallback $ mK'fluid_log_function_t prevcback
where callback cll cstr dataPtr = do
str <- peekCString cstr
runFluidMonad (logger (llFromC cll) str) dataPtr
fromCallback f ll' str = do
dataPtr <- fluidDataPtr
liftIO $ withCString str $ \cstr -> f (llToC ll') cstr dataPtr
|
projedi/fluidsynth-hs-complete
|
src/Sound/Fluidsynth/Log.hs
|
Haskell
|
bsd-3-clause
| 2,534
|
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
module High.Functor.Bifunctor where
import High.Functor.PFunctor (PFunctor)
import High.Functor.QFunctor (QFunctor)
data Bifunctor :: ((* -> *) -> (* -> *) -> *) -> ((* -> *) -> (* -> *) -> *) -> ((* -> *) -> (* -> *) -> *) -> ((* -> *) -> (* -> *) -> (* -> *)) -> * where
Bifunctor ::
{ extend_pfunctor :: PFunctor cat1 dat pro
, extend_qfunctor :: QFunctor cat2 dat pro
} -> Bifunctor cat1 cat2 dat pro
|
Hexirp/untypeclass
|
src/High/Functor/Bifunctor.hs
|
Haskell
|
bsd-3-clause
| 553
|
import System.Nemesis
import System.Nemesis.Utils
import Prelude hiding ((-))
main = run nemesis
nemesis = do
clean
[ "**/*.hi"
, "**/*.o"
, "manifest"
]
desc "dist"
task "dist" - do
sh "cabal clean"
sh "cabal configure"
sh "cabal sdist"
desc "watch hs"
task "watch-hs" - do
sh "find . -name '*.hs' | entr runghc Nemesis.hs emacs-restart"
desc "emacs-restart"
task "emacs-restart" - do
sh "emacsclient -e '(haskell-restart)'"
|
nfjinjing/nemesis
|
Nemesis.hs
|
Haskell
|
bsd-3-clause
| 482
|
module Network.LambNyaa.Sink (
Sink (..), sink, sink_, printItem, seen, unseen
) where
import Data.Monoid
import Network.LambNyaa.Config
import Network.LambNyaa.Item
import Network.LambNyaa.Database
newtype Sink = Sink {unSink :: Config -> [Item] -> IO ()}
-- | Create a Sink from any IO action.
sink :: (Config -> [Item] -> IO ()) -> Sink
sink = Sink
-- | Create a Sink from any IO action; Config-less version.
sink_ :: ([Item] -> IO ()) -> Sink
sink_ f = sink $ const f
-- | Print an Item to stdout.
printItem :: Sink
printItem = sink_ $ mapM_ print
-- | Mark an item as seen.
seen :: Sink
seen = sink $ \cfg is -> withSQLite cfg $ \c -> do
mapM_ (\i -> markSeen (itmIdentifier i) True c) is
-- | Mark an item as seen.
unseen :: Sink
unseen = sink $ \cfg is -> withSQLite cfg $ \c -> do
mapM_ (\i -> markSeen (itmIdentifier i) False c) is
instance Monoid Sink where
mempty = sink_ . const $ return ()
mappend a b = sink $ \cfg is -> do
unSink a cfg is
unSink b cfg is
|
valderman/lambnyaa
|
Network/LambNyaa/Sink.hs
|
Haskell
|
bsd-3-clause
| 999
|
module TaskList
( dispatch
) where
import Euclid
import FastDegree
import GeneratePrimeNumber
import qualified RSA as R
import Factorization
import CustomCodes
dispatch :: String -> [String] -> IO ()
dispatch "-extend-euclid" params = case params of
[a, b] -> print $ euc (read a) (read b)
_ -> printHelper
dispatch "-fast-degree" params = case params of
[n, a, e] -> print $ deg (read n) (read a) (read e)
_ -> printHelper
dispatch "-prime-factor" params = case params of
[n] -> do
prime <- genPrime (read n)
print prime
_ -> printHelper
dispatch "-rsa-params-factor" params = case params of
[n, k_name] -> do
params@(pub_k, pr_k) <- R.genParams (read n)
writeFile ("./.keys/" ++ k_name ++ "_rsa.pub") (show pub_k)
writeFile ("./.keys/" ++ k_name ++ "_rsa") (show pr_k)
print params
_ -> printHelper
dispatch "-encrypt" params = case params of
[k_name, text] -> do
pub_k <- readFile ("./.keys/" ++ k_name ++ "_rsa.pub")
print $ R.encrypt (read pub_k) text
_ -> printHelper
dispatch "-decrypt" params = case params of
[k_name, code] -> do
pr_k <- readFile ("./.keys/" ++ k_name ++ "_rsa")
print $ R.decrypt (read pr_k) (read code)
_ -> printHelper
dispatch "-second-task" params = case params of
[n, e, sw] -> do
pq <- fact (read n)
print pq
let p_key = R.calcPrivateKey pq (read e)
putStrLn $ customDecrypt p_key (read sw)
_ -> printHelper
dispatch "-help" _ = printHelper
dispatch _ _ = printHelper
printHelper :: IO ()
printHelper = putStr $ "cmd = [-extend-euclid | -fast-degree | -prime-factor | -rsa-params-facor | -encrypt | -decrypt | -second-task | -help]\n"
++ "in/out values = -extend-euclid {A} {B} -> {x} {y}\n"
++ " -fast-degree {N - modulo} {A - number} {E - degree} -> {r - result}\n"
++ " -prime-factor {N - bit count} -> {p - prime number}\n"
++ " -rsa-params-factor {N - bit count} {KEYS_NAME} -> {(pub_key, pr_key) - public and private keys with modulo}\n"
++ " -encrypt {KEYS_NAME} {TEXT} -> {code}\n"
++ " -decrypt {KEYS_NAME} {CODE} -> {text}\n"
++ " -second-task {N, E, SW} -> {(p, q) - factorization result} {text}\n"
|
GOGEN/rsa
|
app/TaskList.hs
|
Haskell
|
bsd-3-clause
| 3,545
|
divide :: Double -> Double -> Double
divide 0 b = b
divide a b = b / a
|
YoshikuniJujo/funpaala
|
samples/07_polymorphic/divide0.hs
|
Haskell
|
bsd-3-clause
| 71
|
{-# OPTIONS -cpp #-}
-- OPTIONS required for ghc-6.4.x compat, and must appear first
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -cpp #-}
{-# OPTIONS_NHC98 -cpp #-}
{-# OPTIONS_JHC -fcpp #-}
-----------------------------------------------------------------------------
-- |
-- Module : Distribution.Client.InstallSymlink
-- Copyright : (c) Duncan Coutts 2008
-- License : BSD-like
--
-- Maintainer : cabal-devel@haskell.org
-- Stability : provisional
-- Portability : portable
--
-- Managing installing binaries with symlinks.
-----------------------------------------------------------------------------
module Distribution.Client.InstallSymlink (
symlinkBinaries,
symlinkBinary,
) where
#if mingw32_HOST_OS || mingw32_TARGET_OS
import Distribution.Package (PackageIdentifier)
import Distribution.Client.InstallPlan (InstallPlan)
import Distribution.Client.Setup (InstallFlags)
import Distribution.Simple.Setup (ConfigFlags)
symlinkBinaries :: ConfigFlags
-> InstallFlags
-> InstallPlan
-> IO [(PackageIdentifier, String, FilePath)]
symlinkBinaries _ _ _ = return []
symlinkBinary :: FilePath -> FilePath -> String -> String -> IO Bool
symlinkBinary _ _ _ _ = fail "Symlinking feature not available on Windows"
#else
import Distribution.Client.Types
( SourcePackage(..), ConfiguredPackage(..) )
import Distribution.Client.Setup
( InstallFlags(installSymlinkBinDir) )
import qualified Distribution.Client.InstallPlan as InstallPlan
import Distribution.Client.InstallPlan (InstallPlan)
import Distribution.Package
( PackageIdentifier, Package(packageId) )
import Distribution.Compiler
( CompilerId(..) )
import qualified Distribution.PackageDescription as PackageDescription
import Distribution.PackageDescription
( PackageDescription )
import Distribution.PackageDescription.Configuration
( finalizePackageDescription )
import Distribution.Simple.Setup
( ConfigFlags(..), fromFlag, fromFlagOrDefault, flagToMaybe )
import qualified Distribution.Simple.InstallDirs as InstallDirs
import System.Posix.Files
( getSymbolicLinkStatus, isSymbolicLink, createSymbolicLink
, removeLink )
import System.Directory
( canonicalizePath )
import System.FilePath
( (</>), splitPath, joinPath, isAbsolute )
import Prelude hiding (catch, ioError)
import System.IO.Error
( isDoesNotExistError, ioError )
import Control.Exception
( catch, assert )
import Data.Maybe
( catMaybes )
-- | We would like by default to install binaries into some location that is on
-- the user's PATH. For per-user installations on Unix systems that basically
-- means the @~/bin/@ directory. On the majority of platforms the @~/bin/@
-- directory will be on the user's PATH. However some people are a bit nervous
-- about letting a package manager install programs into @~/bin/@.
--
-- A comprimise solution is that instead of installing binaries directly into
-- @~/bin/@, we could install them in a private location under @~/.cabal/bin@
-- and then create symlinks in @~/bin/@. We can be careful when setting up the
-- symlinks that we do not overwrite any binary that the user installed. We can
-- check if it was a symlink we made because it would point to the private dir
-- where we install our binaries. This means we can install normally without
-- worrying and in a later phase set up symlinks, and if that fails then we
-- report it to the user, but even in this case the package is still in an ok
-- installed state.
--
-- This is an optional feature that users can choose to use or not. It is
-- controlled from the config file. Of course it only works on posix systems
-- with symlinks so is not available to Windows users.
--
symlinkBinaries :: ConfigFlags
-> InstallFlags
-> InstallPlan
-> IO [(PackageIdentifier, String, FilePath)]
symlinkBinaries configFlags installFlags plan =
case flagToMaybe (installSymlinkBinDir installFlags) of
Nothing -> return []
Just symlinkBinDir
| null exes -> return []
| otherwise -> do
publicBinDir <- canonicalizePath symlinkBinDir
-- TODO: do we want to do this here? :
-- createDirectoryIfMissing True publicBinDir
fmap catMaybes $ sequence
[ do privateBinDir <- pkgBinDir pkg
ok <- symlinkBinary
publicBinDir privateBinDir
publicExeName privateExeName
if ok
then return Nothing
else return (Just (pkgid, publicExeName,
privateBinDir </> privateExeName))
| (pkg, exe) <- exes
, let publicExeName = PackageDescription.exeName exe
privateExeName = prefix ++ publicExeName ++ suffix
pkgid = packageId pkg
prefix = substTemplate pkgid prefixTemplate
suffix = substTemplate pkgid suffixTemplate ]
where
exes =
[ (pkg, exe)
| InstallPlan.Installed cpkg _ <- InstallPlan.toList plan
, let pkg = pkgDescription cpkg
, exe <- PackageDescription.executables pkg
, PackageDescription.buildable (PackageDescription.buildInfo exe) ]
pkgDescription :: ConfiguredPackage -> PackageDescription
pkgDescription (ConfiguredPackage (SourcePackage _ pkg _) flags _) =
case finalizePackageDescription flags
(const True)
platform compilerId [] pkg of
Left _ -> error "finalizePackageDescription ConfiguredPackage failed"
Right (desc, _) -> desc
-- This is sadly rather complicated. We're kind of re-doing part of the
-- configuration for the package. :-(
pkgBinDir :: PackageDescription -> IO FilePath
pkgBinDir pkg = do
defaultDirs <- InstallDirs.defaultInstallDirs
compilerFlavor
(fromFlag (configUserInstall configFlags))
(PackageDescription.hasLibs pkg)
let templateDirs = InstallDirs.combineInstallDirs fromFlagOrDefault
defaultDirs (configInstallDirs configFlags)
absoluteDirs = InstallDirs.absoluteInstallDirs
(packageId pkg) compilerId InstallDirs.NoCopyDest
templateDirs
canonicalizePath (InstallDirs.bindir absoluteDirs)
substTemplate pkgid = InstallDirs.fromPathTemplate
. InstallDirs.substPathTemplate env
where env = InstallDirs.initialPathTemplateEnv pkgid compilerId
fromFlagTemplate = fromFlagOrDefault (InstallDirs.toPathTemplate "")
prefixTemplate = fromFlagTemplate (configProgPrefix configFlags)
suffixTemplate = fromFlagTemplate (configProgSuffix configFlags)
platform = InstallPlan.planPlatform plan
compilerId@(CompilerId compilerFlavor _) = InstallPlan.planCompiler plan
symlinkBinary :: FilePath -- ^ The canonical path of the public bin dir
-- eg @/home/user/bin@
-> FilePath -- ^ The canonical path of the private bin dir
-- eg @/home/user/.cabal/bin@
-> String -- ^ The name of the executable to go in the public
-- bin dir, eg @foo@
-> String -- ^ The name of the executable to in the private bin
-- dir, eg @foo-1.0@
-> IO Bool -- ^ If creating the symlink was sucessful. @False@
-- if there was another file there already that we
-- did not own. Other errors like permission errors
-- just propagate as exceptions.
symlinkBinary publicBindir privateBindir publicName privateName = do
ok <- targetOkToOverwrite (publicBindir </> publicName)
(privateBindir </> privateName)
case ok of
NotOurFile -> return False
NotExists -> mkLink >> return True
OkToOverwrite -> rmLink >> mkLink >> return True
where
relativeBindir = makeRelative publicBindir privateBindir
mkLink = createSymbolicLink (relativeBindir </> privateName)
(publicBindir </> publicName)
rmLink = removeLink (publicBindir </> publicName)
-- | Check a filepath of a symlink that we would like to create to see if it
-- is ok. For it to be ok to overwrite it must either not already exist yet or
-- be a symlink to our target (in which case we can assume ownership).
--
targetOkToOverwrite :: FilePath -- ^ The filepath of the symlink to the private
-- binary that we would like to create
-> FilePath -- ^ The canonical path of the private binary.
-- Use 'canonicalizePath' to make this.
-> IO SymlinkStatus
targetOkToOverwrite symlink target = handleNotExist $ do
status <- getSymbolicLinkStatus symlink
if not (isSymbolicLink status)
then return NotOurFile
else do target' <- canonicalizePath symlink
-- This relies on canonicalizePath handling symlinks
if target == target'
then return OkToOverwrite
else return NotOurFile
where
handleNotExist action = catch action $ \ioexception ->
-- If the target doesn't exist then there's no problem overwriting it!
if isDoesNotExistError ioexception
then return NotExists
else ioError ioexception
data SymlinkStatus
= NotExists -- ^ The file doesn't exist so we can make a symlink.
| OkToOverwrite -- ^ A symlink already exists, though it is ours. We'll
-- have to delete it first bemore we make a new symlink.
| NotOurFile -- ^ A file already exists and it is not one of our existing
-- symlinks (either because it is not a symlink or because
-- it points somewhere other than our managed space).
deriving Show
-- | Take two canonical paths and produce a relative path to get from the first
-- to the second, even if it means adding @..@ path components.
--
makeRelative :: FilePath -> FilePath -> FilePath
makeRelative a b = assert (isAbsolute a && isAbsolute b) $
let as = splitPath a
bs = splitPath b
commonLen = length $ takeWhile id $ zipWith (==) as bs
in joinPath $ [ ".." | _ <- drop commonLen as ]
++ [ b' | b' <- drop commonLen bs ]
#endif
|
IreneKnapp/Faction
|
faction/Distribution/Client/InstallSymlink.hs
|
Haskell
|
bsd-3-clause
| 10,568
|
module Day24 where
import Data.Function
import Data.List
partOne = product $ head $ sortOn product $ head $
groupBy ((==) `on` length) $
sortOn length $
filter (\xs -> genericSum xs == weightNeeded 3 && g xs 3) $ subsequences input
partTwo = product $ head $ sortOn product $ head $
groupBy ((==) `on` length) $
sortOn length $
filter (\xs -> genericSum xs == weightNeeded 4 && g2 xs) $ subsequences input
g xs i = any (\ys -> genericSum ys == weightNeeded i) $ subsequences (input \\ xs)
g2 xs = any (\ys -> genericSum ys == weightNeeded 4 && g ys 4) $ subsequences (input \\ xs)
genericSum = fromIntegral . sum
weightNeeded x = genericSum input / x
input :: [Integer]
input = map read $ words "1 3 5 11 13 17 19 23 29 31 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113"
|
z0isch/advent-of-code
|
src/Day24.hs
|
Haskell
|
bsd-3-clause
| 822
|
{-# OPTIONS_GHC -w #-}
{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE CPP #-}
-----------------------------------------------------------------------------
-- |
-- Module : Text.PrettyPrint.Leijen
-- Copyright : Daan Leijen (c) 2000, http://www.cs.uu.nl/~daan
-- License : BSD-style (see the file LICENSE)
--
-- Maintainer : otakar.smrz cmu.edu
-- Stability : provisional
-- Portability : portable
--
-- Pretty print module based on Philip Wadler's \"prettier printer\"
--
-- @
-- \"A prettier printer\"
-- Draft paper, April 1997, revised March 1998.
-- <http://cm.bell-labs.com/cm/cs/who/wadler/papers/prettier/prettier.ps>
-- @
--
-- PPrint is an implementation of the pretty printing combinators
-- described by Philip Wadler (1997). In their bare essence, the
-- combinators of Wadler are not expressive enough to describe some
-- commonly occurring layouts. The PPrint library adds new primitives
-- to describe these layouts and works well in practice.
--
-- The library is based on a single way to concatenate documents,
-- which is associative and has both a left and right unit. This
-- simple design leads to an efficient and short implementation. The
-- simplicity is reflected in the predictable behaviour of the
-- combinators which make them easy to use in practice.
--
-- A thorough description of the primitive combinators and their
-- implementation can be found in Philip Wadler's paper
-- (1997). Additions and the main differences with his original paper
-- are:
--
-- * The nil document is called empty.
--
-- * The above combinator is called '<$>'. The operator '</>' is used
-- for soft line breaks.
--
-- * There are three new primitives: 'align', 'fill' and
-- 'fillBreak'. These are very useful in practice.
--
-- * Lots of other useful combinators, like 'fillSep' and 'list'.
--
-- * There are two renderers, 'renderPretty' for pretty printing and
-- 'renderCompact' for compact output. The pretty printing algorithm
-- also uses a ribbon-width now for even prettier output.
--
-- * There are two displayers, 'displayS' for strings and 'displayIO' for
-- file based output.
--
-- * There is a 'Pretty' class.
--
-- * The implementation uses optimised representations and strictness
-- annotations.
--
-- Full documentation available at <http://www.cs.uu.nl/~daan/download/pprint/pprint.html>.
-----------------------------------------------------------
module Text.PrettyPrint.Leijen (
-- * Documents
Doc, putDoc, hPutDoc,
-- * Basic combinators
empty, char, text, (<>), nest, line, linebreak, group, softline,
softbreak,
-- * Alignment
--
-- The combinators in this section can not be described by Wadler's
-- original combinators. They align their output relative to the
-- current output position - in contrast to @nest@ which always
-- aligns to the current nesting level. This deprives these
-- combinators from being \`optimal\'. In practice however they
-- prove to be very useful. The combinators in this section should
-- be used with care, since they are more expensive than the other
-- combinators. For example, @align@ shouldn't be used to pretty
-- print all top-level declarations of a language, but using @hang@
-- for let expressions is fine.
align, hang, indent, encloseSep, list, tupled, semiBraces,
-- * Operators
(<+>), (<$>), (</>), (<$$>), (<//>),
-- * List combinators
hsep, vsep, fillSep, sep, hcat, vcat, fillCat, cat, punctuate,
-- * Fillers
fill, fillBreak,
-- * Bracketing combinators
enclose, squotes, dquotes, parens, angles, braces, brackets,
-- * Character documents
lparen, rparen, langle, rangle, lbrace, rbrace, lbracket, rbracket,
squote, dquote, semi, colon, comma, space, dot, backslash, equals,
-- * Primitive type documents
string, int, integer, float, double, rational,
-- * Pretty class
Pretty(..),
-- * Rendering
SimpleDoc(..), renderPretty, renderCompact, displayS, displayIO
-- * Undocumented
, bool
, column, nesting, width
, isEmpty
) where
import System.IO (Handle,hPutStr,hPutChar,stdout)
#if __GLASGOW_HASKELL__ >= 710
import Prelude hiding ((<$>))
#endif
infixr 5 </>,<//>,<$>,<$$>
infixr 6 <>,<+>
-----------------------------------------------------------
-- list, tupled and semiBraces pretty print a list of
-- documents either horizontally or vertically aligned.
-----------------------------------------------------------
-- | The document @(list xs)@ comma separates the documents @xs@ and
-- encloses them in square brackets. The documents are rendered
-- horizontally if that fits the page. Otherwise they are aligned
-- vertically. All comma separators are put in front of the elements.
list :: [Doc] -> Doc
list = encloseSep lbracket rbracket comma
-- | The document @(tupled xs)@ comma separates the documents @xs@ and
-- encloses them in parenthesis. The documents are rendered
-- horizontally if that fits the page. Otherwise they are aligned
-- vertically. All comma separators are put in front of the elements.
tupled :: [Doc] -> Doc
tupled = encloseSep lparen rparen comma
-- | The document @(semiBraces xs)@ separates the documents @xs@ with
-- semi colons and encloses them in braces. The documents are rendered
-- horizontally if that fits the page. Otherwise they are aligned
-- vertically. All semi colons are put in front of the elements.
semiBraces :: [Doc] -> Doc
semiBraces = encloseSep lbrace rbrace semi
-- | The document @(encloseSep l r sep xs)@ concatenates the documents
-- @xs@ separated by @sep@ and encloses the resulting document by @l@
-- and @r@. The documents are rendered horizontally if that fits the
-- page. Otherwise they are aligned vertically. All separators are put
-- in front of the elements. For example, the combinator 'list' can be
-- defined with @encloseSep@:
--
-- > list xs = encloseSep lbracket rbracket comma xs
-- > test = text "list" <+> (list (map int [10,200,3000]))
--
-- Which is layed out with a page width of 20 as:
--
-- @
-- list [10,200,3000]
-- @
--
-- But when the page width is 15, it is layed out as:
--
-- @
-- list [10
-- ,200
-- ,3000]
-- @
encloseSep :: Doc -> Doc -> Doc -> [Doc] -> Doc
encloseSep left right sep ds
= case ds of
[] -> left <> right
[d] -> left <> d <> right
_ -> align (cat (zipWith (<>) (left : repeat sep) ds) <> right)
-----------------------------------------------------------
-- punctuate p [d1,d2,...,dn] => [d1 <> p,d2 <> p, ... ,dn]
-----------------------------------------------------------
-- | @(punctuate p xs)@ concatenates all documents in @xs@ with
-- document @p@ except for the last document.
--
-- > someText = map text ["words","in","a","tuple"]
-- > test = parens (align (cat (punctuate comma someText)))
--
-- This is layed out on a page width of 20 as:
--
-- @
-- (words,in,a,tuple)
-- @
--
-- But when the page width is 15, it is layed out as:
--
-- @
-- (words,
-- in,
-- a,
-- tuple)
-- @
--
-- (If you want put the commas in front of their elements instead of
-- at the end, you should use 'tupled' or, in general, 'encloseSep'.)
punctuate :: Doc -> [Doc] -> [Doc]
punctuate p [] = []
punctuate p [d] = [d]
punctuate p (d:ds) = (d <> p) : punctuate p ds
-----------------------------------------------------------
-- high-level combinators
-----------------------------------------------------------
-- | The document @(sep xs)@ concatenates all documents @xs@ either
-- horizontally with @(\<+\>)@, if it fits the page, or vertically with
-- @(\<$\>)@.
--
-- > sep xs = group (vsep xs)
sep :: [Doc] -> Doc
sep = group . vsep
-- | The document @(fillSep xs)@ concatenates documents @xs@
-- horizontally with @(\<+\>)@ as long as its fits the page, than
-- inserts a @line@ and continues doing that for all documents in
-- @xs@.
--
-- > fillSep xs = foldr (\<\/\>) empty xs
fillSep :: [Doc] -> Doc
fillSep = fold (</>)
-- | The document @(hsep xs)@ concatenates all documents @xs@
-- horizontally with @(\<+\>)@.
hsep :: [Doc] -> Doc
hsep = fold (<+>)
-- | The document @(vsep xs)@ concatenates all documents @xs@
-- vertically with @(\<$\>)@. If a 'group' undoes the line breaks
-- inserted by @vsep@, all documents are separated with a space.
--
-- > someText = map text (words ("text to lay out"))
-- >
-- > test = text "some" <+> vsep someText
--
-- This is layed out as:
--
-- @
-- some text
-- to
-- lay
-- out
-- @
--
-- The 'align' combinator can be used to align the documents under
-- their first element
--
-- > test = text "some" <+> align (vsep someText)
--
-- Which is printed as:
--
-- @
-- some text
-- to
-- lay
-- out
-- @
vsep :: [Doc] -> Doc
vsep = fold (<$>)
-- | The document @(cat xs)@ concatenates all documents @xs@ either
-- horizontally with @(\<\>)@, if it fits the page, or vertically with
-- @(\<$$\>)@.
--
-- > cat xs = group (vcat xs)
cat :: [Doc] -> Doc
cat = group . vcat
-- | The document @(fillCat xs)@ concatenates documents @xs@
-- horizontally with @(\<\>)@ as long as its fits the page, than inserts
-- a @linebreak@ and continues doing that for all documents in @xs@.
--
-- > fillCat xs = foldr (\<\/\/\>) empty xs
fillCat :: [Doc] -> Doc
fillCat = fold (<//>)
-- | The document @(hcat xs)@ concatenates all documents @xs@
-- horizontally with @(\<\>)@.
hcat :: [Doc] -> Doc
hcat = fold (<>)
-- | The document @(vcat xs)@ concatenates all documents @xs@
-- vertically with @(\<$$\>)@. If a 'group' undoes the line breaks
-- inserted by @vcat@, all documents are directly concatenated.
vcat :: [Doc] -> Doc
vcat = fold (<$$>)
fold f [] = empty
fold f ds = foldr1 f ds
-- | The document @(x \<\> y)@ concatenates document @x@ and document
-- @y@. It is an associative operation having 'empty' as a left and
-- right unit. (infixr 6)
(<>) :: Doc -> Doc -> Doc
x <> y = x `beside` y
-- | The document @(x \<+\> y)@ concatenates document @x@ and @y@ with a
-- @space@ in between. (infixr 6)
(<+>) :: Doc -> Doc -> Doc
x <+> y = x <> space <> y
-- | The document @(x \<\/\> y)@ concatenates document @x@ and @y@ with a
-- 'softline' in between. This effectively puts @x@ and @y@ either
-- next to each other (with a @space@ in between) or underneath each
-- other. (infixr 5)
(</>) :: Doc -> Doc -> Doc
x </> y = x <> softline <> y
-- | The document @(x \<\/\/\> y)@ concatenates document @x@ and @y@ with
-- a 'softbreak' in between. This effectively puts @x@ and @y@ either
-- right next to each other or underneath each other. (infixr 5)
(<//>) :: Doc -> Doc -> Doc
x <//> y = x <> softbreak <> y
-- | The document @(x \<$\> y)@ concatenates document @x@ and @y@ with a
-- 'line' in between. (infixr 5)
(<$>) :: Doc -> Doc -> Doc
x <$> y = x <> line <> y
-- | The document @(x \<$$\> y)@ concatenates document @x@ and @y@ with
-- a @linebreak@ in between. (infixr 5)
(<$$>) :: Doc -> Doc -> Doc
x <$$> y = x <> linebreak <> y
-- | The document @softline@ behaves like 'space' if the resulting
-- output fits the page, otherwise it behaves like 'line'.
--
-- > softline = group line
softline :: Doc
softline = group line
-- | The document @softbreak@ behaves like 'empty' if the resulting
-- output fits the page, otherwise it behaves like 'line'.
--
-- > softbreak = group linebreak
softbreak :: Doc
softbreak = group linebreak
-- | Document @(squotes x)@ encloses document @x@ with single quotes
-- \"'\".
squotes :: Doc -> Doc
squotes = enclose squote squote
-- | Document @(dquotes x)@ encloses document @x@ with double quotes
-- '\"'.
dquotes :: Doc -> Doc
dquotes = enclose dquote dquote
-- | Document @(braces x)@ encloses document @x@ in braces, \"{\" and
-- \"}\".
braces :: Doc -> Doc
braces = enclose lbrace rbrace
-- | Document @(parens x)@ encloses document @x@ in parenthesis, \"(\"
-- and \")\".
parens :: Doc -> Doc
parens = enclose lparen rparen
-- | Document @(angles x)@ encloses document @x@ in angles, \"\<\" and
-- \"\>\".
angles :: Doc -> Doc
angles = enclose langle rangle
-- | Document @(brackets x)@ encloses document @x@ in square brackets,
-- \"[\" and \"]\".
brackets :: Doc -> Doc
brackets = enclose lbracket rbracket
-- | The document @(enclose l r x)@ encloses document @x@ between
-- documents @l@ and @r@ using @(\<\>)@.
--
-- > enclose l r x = l <> x <> r
enclose :: Doc -> Doc -> Doc -> Doc
enclose l r x = l <> x <> r
-- | The document @lparen@ contains a left parenthesis, \"(\".
lparen :: Doc
lparen = char '('
-- | The document @rparen@ contains a right parenthesis, \")\".
rparen :: Doc
rparen = char ')'
-- | The document @langle@ contains a left angle, \"\<\".
langle :: Doc
langle = char '<'
-- | The document @rangle@ contains a right angle, \">\".
rangle :: Doc
rangle = char '>'
-- | The document @lbrace@ contains a left brace, \"{\".
lbrace :: Doc
lbrace = char '{'
-- | The document @rbrace@ contains a right brace, \"}\".
rbrace :: Doc
rbrace = char '}'
-- | The document @lbracket@ contains a left square bracket, \"[\".
lbracket :: Doc
lbracket = char '['
-- | The document @rbracket@ contains a right square bracket, \"]\".
rbracket :: Doc
rbracket = char ']'
-- | The document @squote@ contains a single quote, \"'\".
squote :: Doc
squote = char '\''
-- | The document @dquote@ contains a double quote, '\"'.
dquote :: Doc
dquote = char '"'
-- | The document @semi@ contains a semi colon, \";\".
semi :: Doc
semi = char ';'
-- | The document @colon@ contains a colon, \":\".
colon :: Doc
colon = char ':'
-- | The document @comma@ contains a comma, \",\".
comma :: Doc
comma = char ','
-- | The document @space@ contains a single space, \" \".
--
-- > x <+> y = x <> space <> y
space :: Doc
space = char ' '
-- | The document @dot@ contains a single dot, \".\".
dot :: Doc
dot = char '.'
-- | The document @backslash@ contains a back slash, \"\\\".
backslash :: Doc
backslash = char '\\'
-- | The document @equals@ contains an equal sign, \"=\".
equals :: Doc
equals = char '='
-----------------------------------------------------------
-- Combinators for prelude types
-----------------------------------------------------------
-- string is like "text" but replaces '\n' by "line"
-- | The document @(string s)@ concatenates all characters in @s@
-- using @line@ for newline characters and @char@ for all other
-- characters. It is used instead of 'text' whenever the text contains
-- newline characters.
string :: String -> Doc
string "" = empty
string ('\n':s) = line <> string s
string s = case (span (/='\n') s) of
(xs,ys) -> text xs <> string ys
bool :: Bool -> Doc
bool b = text (show b)
-- | The document @(int i)@ shows the literal integer @i@ using
-- 'text'.
int :: Int -> Doc
int i = text (show i)
-- | The document @(integer i)@ shows the literal integer @i@ using
-- 'text'.
integer :: Integer -> Doc
integer i = text (show i)
-- | The document @(float f)@ shows the literal float @f@ using
-- 'text'.
float :: Float -> Doc
float f = text (show f)
-- | The document @(double d)@ shows the literal double @d@ using
-- 'text'.
double :: Double -> Doc
double d = text (show d)
-- | The document @(rational r)@ shows the literal rational @r@ using
-- 'text'.
rational :: Rational -> Doc
rational r = text (show r)
-----------------------------------------------------------
-- overloading "pretty"
-----------------------------------------------------------
-- | The member @prettyList@ is only used to define the @instance Pretty
-- a => Pretty [a]@. In normal circumstances only the @pretty@ function
-- is used.
class Pretty a where
pretty :: a -> Doc
prettyList :: [a] -> Doc
prettyList = list . map pretty
instance Pretty a => Pretty [a] where
pretty = prettyList
instance Pretty Doc where
pretty = id
instance Pretty () where
pretty () = text "()"
instance Pretty Bool where
pretty b = bool b
instance Pretty Char where
pretty c = char c
prettyList s = string s
instance Pretty Int where
pretty i = int i
instance Pretty Integer where
pretty i = integer i
instance Pretty Float where
pretty f = float f
instance Pretty Double where
pretty d = double d
--instance Pretty Rational where
-- pretty r = rational r
instance (Pretty a,Pretty b) => Pretty (a,b) where
pretty (x,y) = tupled [pretty x, pretty y]
instance (Pretty a,Pretty b,Pretty c) => Pretty (a,b,c) where
pretty (x,y,z)= tupled [pretty x, pretty y, pretty z]
instance Pretty a => Pretty (Maybe a) where
pretty Nothing = empty
pretty (Just x) = pretty x
-----------------------------------------------------------
-- semi primitive: fill and fillBreak
-----------------------------------------------------------
-- | The document @(fillBreak i x)@ first renders document @x@. It
-- than appends @space@s until the width is equal to @i@. If the
-- width of @x@ is already larger than @i@, the nesting level is
-- increased by @i@ and a @line@ is appended. When we redefine @ptype@
-- in the previous example to use @fillBreak@, we get a useful
-- variation of the previous output:
--
-- > ptype (name,tp)
-- > = fillBreak 6 (text name) <+> text "::" <+> text tp
--
-- The output will now be:
--
-- @
-- let empty :: Doc
-- nest :: Int -> Doc -> Doc
-- linebreak
-- :: Doc
-- @
fillBreak :: Int -> Doc -> Doc
fillBreak f x = width x (\w ->
if (w > f) then nest f linebreak
else text (spaces (f - w)))
-- | The document @(fill i x)@ renders document @x@. It than appends
-- @space@s until the width is equal to @i@. If the width of @x@ is
-- already larger, nothing is appended. This combinator is quite
-- useful in practice to output a list of bindings. The following
-- example demonstrates this.
--
-- > types = [("empty","Doc")
-- > ,("nest","Int -> Doc -> Doc")
-- > ,("linebreak","Doc")]
-- >
-- > ptype (name,tp)
-- > = fill 6 (text name) <+> text "::" <+> text tp
-- >
-- > test = text "let" <+> align (vcat (map ptype types))
--
-- Which is layed out as:
--
-- @
-- let empty :: Doc
-- nest :: Int -> Doc -> Doc
-- linebreak :: Doc
-- @
fill :: Int -> Doc -> Doc
fill f d = width d (\w ->
if (w >= f) then empty
else text (spaces (f - w)))
width :: Doc -> (Int -> Doc) -> Doc
width d f = column (\k1 -> d <> column (\k2 -> f (k2 - k1)))
-----------------------------------------------------------
-- semi primitive: Alignment and indentation
-----------------------------------------------------------
-- | The document @(indent i x)@ indents document @x@ with @i@ spaces.
--
-- > test = indent 4 (fillSep (map text
-- > (words "the indent combinator indents these words !")))
--
-- Which lays out with a page width of 20 as:
--
-- @
-- the indent
-- combinator
-- indents these
-- words !
-- @
indent :: Int -> Doc -> Doc
indent i d = hang i (text (spaces i) <> d)
-- | The hang combinator implements hanging indentation. The document
-- @(hang i x)@ renders document @x@ with a nesting level set to the
-- current column plus @i@. The following example uses hanging
-- indentation for some text:
--
-- > test = hang 4 (fillSep (map text
-- > (words "the hang combinator indents these words !")))
--
-- Which lays out on a page with a width of 20 characters as:
--
-- @
-- the hang combinator
-- indents these
-- words !
-- @
--
-- The @hang@ combinator is implemented as:
--
-- > hang i x = align (nest i x)
hang :: Int -> Doc -> Doc
hang i d = align (nest i d)
-- | The document @(align x)@ renders document @x@ with the nesting
-- level set to the current column. It is used for example to
-- implement 'hang'.
--
-- As an example, we will put a document right above another one,
-- regardless of the current nesting level:
--
-- > x $$ y = align (x <$> y)
--
-- > test = text "hi" <+> (text "nice" $$ text "world")
--
-- which will be layed out as:
--
-- @
-- hi nice
-- world
-- @
align :: Doc -> Doc
align d = column (\k ->
nesting (\i -> nest (k - i) d)) --nesting might be negative :-)
-----------------------------------------------------------
-- Primitives
-----------------------------------------------------------
-- | The abstract data type @Doc@ represents pretty documents.
--
-- @Doc@ is an instance of the 'Show' class. @(show doc)@ pretty
-- prints document @doc@ with a page width of 100 characters and a
-- ribbon width of 40 characters.
--
-- > show (text "hello" <$> text "world")
--
-- Which would return the string \"hello\\nworld\", i.e.
--
-- @
-- hello
-- world
-- @
data Doc = Empty
| Char Char -- invariant: char is not '\n'
| Text !Int String -- invariant: text doesn't contain '\n'
| Line !Bool -- True <=> when undone by group, do not insert a space
| Cat Doc Doc
| Nest !Int Doc
| Union Doc Doc -- invariant: first lines of first doc longer than the first lines of the second doc
| Column (Int -> Doc)
| Nesting (Int -> Doc)
isEmpty :: Doc -> Bool
isEmpty Empty = True
isEmpty _ = False
-- | The data type @SimpleDoc@ represents rendered documents and is
-- used by the display functions.
--
-- The @Int@ in @SText@ contains the length of the string. The @Int@
-- in @SLine@ contains the indentation for that line. The library
-- provides two default display functions 'displayS' and
-- 'displayIO'. You can provide your own display function by writing a
-- function from a @SimpleDoc@ to your own output format.
data SimpleDoc = SEmpty
| SChar Char SimpleDoc
| SText !Int String SimpleDoc
| SLine !Int SimpleDoc
-- | The empty document is, indeed, empty. Although @empty@ has no
-- content, it does have a \'height\' of 1 and behaves exactly like
-- @(text \"\")@ (and is therefore not a unit of @\<$\>@).
empty :: Doc
empty = Empty
-- | The document @(char c)@ contains the literal character @c@. The
-- character shouldn't be a newline (@'\n'@), the function 'line'
-- should be used for line breaks.
char :: Char -> Doc
char '\n' = line
char c = Char c
-- | The document @(text s)@ contains the literal string @s@. The
-- string shouldn't contain any newline (@'\n'@) characters. If the
-- string contains newline characters, the function 'string' should be
-- used.
text :: String -> Doc
text "" = Empty
text s = Text (length s) s
-- | The @line@ document advances to the next line and indents to the
-- current nesting level. Document @line@ behaves like @(text \" \")@
-- if the line break is undone by 'group'.
line :: Doc
line = Line False
-- | The @linebreak@ document advances to the next line and indents to
-- the current nesting level. Document @linebreak@ behaves like
-- 'empty' if the line break is undone by 'group'.
linebreak :: Doc
linebreak = Line True
beside x y = Cat x y
-- | The document @(nest i x)@ renders document @x@ with the current
-- indentation level increased by i (See also 'hang', 'align' and
-- 'indent').
--
-- > nest 2 (text "hello" <$> text "world") <$> text "!"
--
-- outputs as:
--
-- @
-- hello
-- world
-- !
-- @
nest :: Int -> Doc -> Doc
nest i x = Nest i x
column, nesting :: (Int -> Doc) -> Doc
column f = Column f
nesting f = Nesting f
-- | The @group@ combinator is used to specify alternative
-- layouts. The document @(group x)@ undoes all line breaks in
-- document @x@. The resulting line is added to the current line if
-- that fits the page. Otherwise, the document @x@ is rendered without
-- any changes.
group :: Doc -> Doc
group x = Union (flatten x) x
flatten :: Doc -> Doc
flatten (Cat x y) = Cat (flatten x) (flatten y)
flatten (Nest i x) = Nest i (flatten x)
flatten (Line break) = if break then Empty else Text 1 " "
flatten (Union x y) = flatten x
flatten (Column f) = Column (flatten . f)
flatten (Nesting f) = Nesting (flatten . f)
flatten other = other --Empty,Char,Text
-----------------------------------------------------------
-- Renderers
-----------------------------------------------------------
-----------------------------------------------------------
-- renderPretty: the default pretty printing algorithm
-----------------------------------------------------------
-- list of indentation/document pairs; saves an indirection over [(Int,Doc)]
data Docs = Nil
| Cons !Int Doc Docs
-- | This is the default pretty printer which is used by 'show',
-- 'putDoc' and 'hPutDoc'. @(renderPretty ribbonfrac width x)@ renders
-- document @x@ with a page width of @width@ and a ribbon width of
-- @(ribbonfrac * width)@ characters. The ribbon width is the maximal
-- amount of non-indentation characters on a line. The parameter
-- @ribbonfrac@ should be between @0.0@ and @1.0@. If it is lower or
-- higher, the ribbon width will be 0 or @width@ respectively.
renderPretty :: Float -> Int -> Doc -> SimpleDoc
renderPretty rfrac w x
= best 0 0 (Cons 0 x Nil)
where
-- r :: the ribbon width in characters
r = max 0 (min w (round (fromIntegral w * rfrac)))
-- best :: n = indentation of current line
-- k = current column
-- (ie. (k >= n) && (k - n == count of inserted characters)
best n k Nil = SEmpty
best n k (Cons i d ds)
= case d of
Empty -> best n k ds
Char c -> let k' = k+1 in seq k' (SChar c (best n k' ds))
Text l s -> let k' = k+l in seq k' (SText l s (best n k' ds))
Line _ -> SLine i (best i i ds)
Cat x y -> best n k (Cons i x (Cons i y ds))
Nest j x -> let i' = i+j in seq i' (best n k (Cons i' x ds))
Union x y -> nicest n k (best n k (Cons i x ds))
(best n k (Cons i y ds))
Column f -> best n k (Cons i (f k) ds)
Nesting f -> best n k (Cons i (f i) ds)
--nicest :: r = ribbon width, w = page width,
-- n = indentation of current line, k = current column
-- x and y, the (simple) documents to chose from.
-- precondition: first lines of x are longer than the first lines of y.
nicest n k x y | fits width x = x
| otherwise = y
where
width = min (w - k) (r - k + n)
fits w x | w < 0 = False
fits w SEmpty = True
fits w (SChar c x) = fits (w - 1) x
fits w (SText l s x) = fits (w - l) x
fits w (SLine i x) = True
-----------------------------------------------------------
-- renderCompact: renders documents without indentation
-- fast and fewer characters output, good for machines
-----------------------------------------------------------
-- | @(renderCompact x)@ renders document @x@ without adding any
-- indentation. Since no \'pretty\' printing is involved, this
-- renderer is very fast. The resulting output contains fewer
-- characters than a pretty printed version and can be used for output
-- that is read by other programs.
renderCompact :: Doc -> SimpleDoc
renderCompact x
= scan 0 [x]
where
scan k [] = SEmpty
scan k (d:ds) = case d of
Empty -> scan k ds
Char c -> let k' = k+1 in seq k' (SChar c (scan k' ds))
Text l s -> let k' = k+l in seq k' (SText l s (scan k' ds))
Line _ -> SLine 0 (scan 0 ds)
Cat x y -> scan k (x:y:ds)
Nest j x -> scan k (x:ds)
Union x y -> scan k (y:ds)
Column f -> scan k (f k:ds)
Nesting f -> scan k (f 0:ds)
-----------------------------------------------------------
-- Displayers: displayS and displayIO
-----------------------------------------------------------
-- | @(displayS simpleDoc)@ takes the output @simpleDoc@ from a
-- rendering function and transforms it to a 'ShowS' type (for use in
-- the 'Show' class).
--
-- > showWidth :: Int -> Doc -> String
-- > showWidth w x = displayS (renderPretty 0.4 w x) ""
displayS :: SimpleDoc -> ShowS
displayS SEmpty = id
displayS (SChar c x) = showChar c . displayS x
displayS (SText l s x) = showString s . displayS x
displayS (SLine i x) = showString ('\n':indentation i) . displayS x
-- | @(displayIO handle simpleDoc)@ writes @simpleDoc@ to the file
-- handle @handle@. This function is used for example by 'hPutDoc':
--
-- > hPutDoc handle doc = displayIO handle (renderPretty 0.4 100 doc)
displayIO :: Handle -> SimpleDoc -> IO ()
displayIO handle simpleDoc
= display simpleDoc
where
display SEmpty = return ()
display (SChar c x) = do{ hPutChar handle c; display x}
display (SText l s x) = do{ hPutStr handle s; display x}
display (SLine i x) = do{ hPutStr handle ('\n':indentation i); display x}
-----------------------------------------------------------
-- default pretty printers: show, putDoc and hPutDoc
-----------------------------------------------------------
instance Show Doc where
showsPrec d doc = displayS (renderPretty 0.4 80 doc)
-- | The action @(putDoc doc)@ pretty prints document @doc@ to the
-- standard output, with a page width of 100 characters and a ribbon
-- width of 40 characters.
--
-- > main :: IO ()
-- > main = do{ putDoc (text "hello" <+> text "world") }
--
-- Which would output
--
-- @
-- hello world
-- @
putDoc :: Doc -> IO ()
putDoc doc = hPutDoc stdout doc
-- | @(hPutDoc handle doc)@ pretty prints document @doc@ to the file
-- handle @handle@ with a page width of 100 characters and a ribbon
-- width of 40 characters.
--
-- > main = do{ handle <- openFile "MyFile" WriteMode
-- > ; hPutDoc handle (vcat (map text
-- > ["vertical","text"]))
-- > ; hClose handle
-- > }
hPutDoc :: Handle -> Doc -> IO ()
hPutDoc handle doc = displayIO handle (renderPretty 0.4 80 doc)
-----------------------------------------------------------
-- insert spaces
-- "indentation" used to insert tabs but tabs seem to cause
-- more trouble than they solve :-)
-----------------------------------------------------------
spaces n | n <= 0 = ""
| otherwise = replicate n ' '
indentation n = spaces n
--indentation n | n >= 8 = '\t' : indentation (n-8)
-- | otherwise = spaces n
-- LocalWords: PPrint combinators Wadler Wadler's encloseSep
|
osa1/language-lua
|
src/Text/PrettyPrint/Leijen.hs
|
Haskell
|
bsd-3-clause
| 31,470
|
module Karamaan.Opaleye.Operators (operatorName) where
-- TODO vv I put this take 5 in here because the query strings were getting
-- too long and postgres was complaining that it was truncating them.
-- This is really just a temporary fix, because I'd like to keep the
-- possibility of long names but postprocess the PrimQuery to shorten
-- them before sending them to postgres.
operatorName :: String -> String -> String -> String
operatorName left opName right = concat [t left, "_", opName, "_", t right]
where t = take 5
|
karamaan/karamaan-opaleye
|
Karamaan/Opaleye/Operators.hs
|
Haskell
|
bsd-3-clause
| 530
|
{- | This module provides the /Simplify RHS/ processor.
@
|- <simp(S#) / simp(W#) + W, Q, T#> :f
------------------------------------------
|- <S# / W# + W, Q, T#> :f
@
, where @simp(R#)@ removes @ri@ from right-hand sides @c_n(r_1,...,r_n)@ if no instance of @ri@ can be rewritten, ie. if
there is no outgoing edge @i@.
-}
module Tct.Trs.Processor.DP.DPGraph.SimplifyRHS
( simplifyRHSDeclaration
, simplifyRHS
) where
import qualified Data.List as L (partition)
import Data.Maybe (fromMaybe)
import qualified Data.Rewriting.Rule as R (Rule (..))
import qualified Data.Rewriting.Term as R
import qualified Tct.Core.Common.Pretty as PP
import qualified Tct.Core.Common.Xml as Xml
import qualified Tct.Core.Data as T
import Tct.Common.ProofCombinators
import Tct.Trs.Data
import Tct.Trs.Data.DependencyGraph
import qualified Tct.Trs.Data.Problem as Prob
import qualified Tct.Trs.Data.Signature as Sig
import qualified Tct.Trs.Data.Symbol as Symb
import qualified Tct.Trs.Data.Rules as RS
data SimplifyRHS = SimplifyRHS deriving Show
data SimplifyRHSProof
= SimplifyRHSProof
{ wdg_ :: DG F V
, simplified_ :: [R.Rule F V] }
| SimplifyRHSFail
deriving Show
instance T.Processor SimplifyRHS where
type ProofObject SimplifyRHS = ApplicationProof SimplifyRHSProof
type In SimplifyRHS = Trs
type Out SimplifyRHS = Trs
execute SimplifyRHS prob =
maybe simpRHS (\s -> T.abortWith (Inapplicable s :: ApplicationProof SimplifyRHSProof)) (Prob.isDTProblem' prob)
where
simpRHS
| null simplified = T.abortWith (Applicable SimplifyRHSFail)
| otherwise = T.succeedWith1 (Applicable proof) T.fromId nprob
where
wdg = Prob.dependencyGraph prob
-- TODO: MS: this is not optimal;
-- I assumed that all my rhs have compound symbols after the DP transformation; this is not true after the
-- decomposeDG transformation. We can now have rhs of length one which are removed (in practice they are
-- probably already removed before); ie which should be replaced with fresh compound symbols. Either make
-- sure that we have the appropriate format or introduce fresh compound symbols here
elims = [ (isStrict cn, (r, elimRule n r)) | (n,cn) <- lnodes wdg, let r = theRule cn ]
elimRule n (R.Rule l (R.Fun f rs))
| not (Symb.isCompoundFun f) = Nothing
-- | not (Prob.isCompoundf f) = error $ "SimplifyRHS.elim: not a compound symbol: " ++ show f
| otherwise = if length rs' == length rs then Nothing else Just $ R.Rule l (R.Fun f rs')
where
rs' = [ ri | (i,ri) <- zip [1..] rs, i `elem` succs]
succs = [ j | (_,_,j) <- lsuccessors wdg n]
elimRule _ _ = Nothing
(stricts,weaks) = L.partition fst elims
toTrs rs = RS.fromList [ r | (_,(r1, mr2)) <- rs, let r = r1 `fromMaybe` mr2]
simplified = [ r | (_,(_, Just r)) <- elims ]
nprob = Prob.sanitiseDPGraph $ prob
{ Prob.strictDPs = toTrs stricts
, Prob.weakDPs = toTrs weaks
, Prob.signature = foldr updateCompound (Prob.signature prob) simplified }
where
updateCompound (R.Rule _ (R.Fun f rs)) acc = Sig.setArity (length rs) f acc
updateCompound _ acc = acc
proof = SimplifyRHSProof
{ wdg_ = wdg
, simplified_ = simplified }
--- * instances ------------------------------------------------------------------------------------------------------
simplifyRHSDeclaration :: T.Declaration ('[] T.:-> TrsStrategy)
simplifyRHSDeclaration = T.declare "simplifyRHS" desc () (T.Apply SimplifyRHS) where
desc =
[ "Simplify right hand sides of dependency pairs by removing marked subterms "
, "whose root symbols are undefined."
, "Only applicable if the strict component is empty." ]
-- | Simplifies right-hand sides of dependency pairs.
-- Removes r_i from right-hand side @c_n(r_1,...,r_n)@ if no instance of
-- r_i can be rewritten.
--
-- Only applicable on DP-problems as obtained by 'dependencyPairs' or 'dependencyTuples'. Also
-- not applicable when @strictTrs prob \= RS.empty@.
simplifyRHS :: TrsStrategy
simplifyRHS = T.declFun simplifyRHSDeclaration
--- * proofdata ------------------------------------------------------------------------------------------------------
instance PP.Pretty SimplifyRHSProof where
pretty SimplifyRHSFail = PP.text "No rule was simplified."
pretty p@SimplifyRHSProof{} = PP.vcat
[ PP.text "Consider the dependency graph"
, PP.indent 2 $ PP.pretty (wdg_ p)
, PP.text "Due to missing edges in the depndency graph, the right-hand sides of following rules could be simplified:"
, PP.indent 2 $ PP.pretty (RS.fromList $ simplified_ p) ]
instance Xml.Xml SimplifyRHSProof where
toXml SimplifyRHSFail = Xml.elt "simpRHS" []
toXml p@SimplifyRHSProof{} = Xml.elt "simpRHS"
[ Xml.toXml (wdg_ p)
, Xml.elt "simplified" $ map Xml.toXml (simplified_ p) ]
|
ComputationWithBoundedResources/tct-trs
|
src/Tct/Trs/Processor/DP/DPGraph/SimplifyRHS.hs
|
Haskell
|
bsd-3-clause
| 5,310
|
{- |
Module : Ptk.Journal
Description : `ptk journal` command tree for the puffy toolkit
Copyright : 2014, Peter Harpending
License : BSD3
Maintainer : Peter Harpending <pharpend2@gmail.com>
Stability : experimental
Portability : Linux
-}
module Ptk.Journal (jTree, journalTree, journalHelp) where
import Data.Traversable
import PuffyTools.Journal
import Ptk.Journal.AddEntry
import Ptk.Journal.Cat
import Ptk.Journal.List
import Ptk.Journal.ListEntries
import Ptk.Journal.New
import System.Console.Argument
import System.Console.Command
import System.Console.Program
subCommandList = [ journalAddEntryTree
, journalAETree
, journalCatTree
, journalListTree
, journalLsTree
, journalListEntriesTree
, journalLeTree
, journalNewTree
, journalHelpTree
]
journalTree :: Commands IO
journalTree = Node journalCommand subCommandList
where
journalCommand = Command "journal" "Do things with Journals" journalHelp
jTree = Node jCommand subCommandList
where jCommand = Command "j" "Same as journal." journalHelp
journalHelpTree = Node (Command "help" "Show help for the journal module" journalHelp) []
journalHelp = io $ showUsage journalTree
|
pharpend/puffytools
|
ptk/Ptk/Journal.hs
|
Haskell
|
bsd-3-clause
| 1,424
|
module Ivory.BSP.STM32.Peripheral.UART
( module Ivory.BSP.STM32.Peripheral.UART.Peripheral
, module Ivory.BSP.STM32.Peripheral.UART.Regs
, module Ivory.BSP.STM32.Peripheral.UART.Types
) where
import Ivory.BSP.STM32.Peripheral.UART.Peripheral
import Ivory.BSP.STM32.Peripheral.UART.Regs
import Ivory.BSP.STM32.Peripheral.UART.Types
|
GaloisInc/ivory-tower-stm32
|
ivory-bsp-stm32/src/Ivory/BSP/STM32/Peripheral/UART.hs
|
Haskell
|
bsd-3-clause
| 342
|
module Main where
-- This is a Haskell translation of the official GLFW quick example
-- found at <http://www.glfw.org/docs/3.0/quick.html#quick_example>
-- using the GLFW-b library, version 1.x
-- I tried hard to keep the same structure so that it is simple
-- enough to go back and forth between the C version and the
-- Haskell one, while preserving some usual haskell tricks
-- If you have any comment, bug report, or any other kind of
-- feedback, feel free to shoot me an email at
-- alpmestan at gmail dot com
import Control.Monad (unless, when)
import Graphics.Rendering.OpenGL
import qualified Graphics.UI.GLFW as G
import System.Exit
import System.IO
-- tiny utility functions, in the same spirit as 'maybe' or 'either'
-- makes the code a wee bit cleaner
bool :: Bool -> a -> a -> a
bool b falseRes trueRes = if b then trueRes else falseRes
unless' :: Monad m => m Bool -> m () -> m ()
unless' action falseAction = do
b <- action
unless b falseAction
maybe' :: Maybe a -> b -> (a -> b) -> b
maybe' m nothingRes f = case m of
Nothing -> nothingRes
Just x -> f x
-- type ErrorCallback = Error -> String -> IO ()
errorCallback :: G.ErrorCallback
errorCallback err description = hPutStrLn stderr description
keyCallback :: G.KeyCallback
keyCallback window key scancode action mods = when (key == G.Key'Escape && action == G.KeyState'Pressed) $
G.setWindowShouldClose window True
main :: IO ()
main = do
G.setErrorCallback (Just errorCallback)
successfulInit <- G.init
-- if init failed, we exit the program
bool successfulInit exitFailure $ do
mw <- G.createWindow 640 480 "Simple example, haskell style" Nothing Nothing
maybe' mw (G.terminate >> exitFailure) $ \window -> do
G.makeContextCurrent mw
G.setKeyCallback window (Just keyCallback)
mainLoop window
G.destroyWindow window
G.terminate
exitSuccess
mainLoop :: G.Window -> IO ()
mainLoop w = unless' (G.windowShouldClose w) $ do
(width, height) <- G.getFramebufferSize w
let ratio = fromIntegral width / fromIntegral height
viewport $= (Position 0 0, Size (fromIntegral width) (fromIntegral height))
clear [ColorBuffer]
matrixMode $= Projection
loadIdentity
ortho (negate ratio) ratio (negate 1.0) 1.0 1.0 (negate 1.0)
matrixMode $= Modelview 0
loadIdentity
-- this is bad, but keeps the logic of the original example I guess
Just t <- G.getTime
rotate ((realToFrac t) * 50) $ (Vector3 0 0 1 :: Vector3 GLdouble)
renderPrimitive Triangles $ do
color (Color3 1 0 0 :: Color3 GLdouble)
vertex (Vertex3 (negate 0.6) (negate 0.4) 0 :: Vertex3 GLdouble)
color (Color3 0 1 0 :: Color3 GLdouble)
vertex (Vertex3 0.6 (negate 0.4) 0 :: Vertex3 GLdouble)
color (Color3 0 0 1 :: Color3 GLdouble)
vertex (Vertex3 0 0.6 0 :: Vertex3 GLdouble)
G.swapBuffers w
G.pollEvents
mainLoop w
|
alpmestan/glfw-b-quick-example
|
Main.hs
|
Haskell
|
bsd-3-clause
| 3,002
|
module T where -- this shits me
import Sample
test_sanity = True
|
peteg/TBC
|
Sample/Tests/00_loadable.hs
|
Haskell
|
bsd-3-clause
| 64
|
module Graph where
import Control.Monad
import Control.Monad.State
import Index
import Verilog
import Data.Graph.Inductive
import Data.Graph.Inductive.Query.DFS
import Data.Graph.Inductive.Dot
import Data.Graph.Inductive.NodeMap
import Data.List
import Control.Arrow
import Debug.Trace
import System.Random
import qualified Data.Map as M
import qualified Data.Set as S
-- import qualified Data.IntMap as M
-- | The graph that models the circuit after Nand Synthesis Model
type G = Gr Val Bool
type Ctx = Context Val Bool
val :: G -> Node -> Val
val g n = v
where
Just v = lab g n
type GState a = StateT G IdxState a
--type VG = Gr (NT, Bool) Bool
-- | Converts a graph to a GraphViz format
showGraph g = showDot $ fglToDot $ gmap (\(is, n, v, os) -> (is, n, (n, v), os)) g
-- | Creates all the nodes of the Graph
--wireNodes :: Verilog -> Ctx
--wireNodes v = [([], n, (), []) | n <- names v]
-- | Embeds all the wires in the graphs as disconnected nodes
startGraph :: G
startGraph = mkGraph [(0, ValZero), (1, ValOne)] []
addNode :: (Val, Node) -> GState Int
addNode (v, n) = do
g <- get
unless (gelem n g) $
modify $ insNode (n,v)
return n
newWire :: GState Int
newWire = do
n <- lift newIdx
addNode (Wire "<extra>", n)
getWire :: Val -> GState Int
getWire w = do
n <- lift $ getIdx w
addNode (w, n)
addEdge :: Bool -> (Node, Node) -> GState ()
addEdge b (n1, n2) =
modify $ insEdge (n1, n2, b)
initGraph :: Verilog -> GState ()
initGraph v = do
mapM_ getWire (map Input $ _inputs v)
trues = repeat True
falses = repeat False
-- | Embeds a Function in the graph.
embedF :: Function -> GState ()
embedF f@(Fun op os is) =
--trace ("Embedding function" ++ show f) $
case op of
And -> embedAnd is o
Nand -> embedNand is o
Or -> embedOr is o
Nor -> embedNor is o
Xor -> embedXor is o
Xnor -> embedXnor is o
Buf -> embedBuf i os
Not -> embedNot i os
where [i] = is
[o] = os
-- | Negates an Adjacency list
negateA :: Adj Bool -> Adj Bool
negateA = map (first not)
-- | Negates all output edges of a ctx
negateCtx (is, n, nv, os) = (is, n, nv, negateA os)
-- | Negates all output edges of a node
{-
negateV :: Node -> GState ()
negateV n = do
g <- get
let (mc, g') = match n g
case mc of
Just ctx -> put $ negateCtx ctx & g'
Nothing -> error "could not find vertex in negateV"
-}
-- | Should insert a few edges in the graph.
embedBuf :: Val -> [Val] -> GState ()
embedBuf iw ows = do
i <- getWire iw
os <- mapM getWire ows
embedBuf' i os
embedBuf' :: Node -> [Node] -> GState ()
embedBuf' i os = do
modify $ insEdges [(i,o, True) | o <- os]
-- | Inserts a Not function in the graph.
-- | Does this by negating all the outputs of a current vertex.
embedNot :: Val -> [Val] -> GState ()
embedNot iw ows = do
i <- getWire iw
os <- mapM getWire ows
modify $ insEdges [(i,o, False) | o <- os]
-- | Inserts an And function into the graph
embedAnd :: [Val] -> Val -> GState ()
embedAnd iws ow = do
is <- mapM getWire iws
o <- getWire ow
embedAnd' is o
embedAnd' :: [Node] -> Node -> GState ()
embedAnd' is o =
modify $ insEdges [(i,o, True) | i <- is]
-- | Inserts a Nand function into the graph
embedNand :: [Val] -> Val -> GState ()
embedNand iws ow = do
is <- mapM getWire iws
o <- getWire ow
embedNand' is o
embedNand' :: [Node] -> Node -> GState ()
embedNand' is o = do
n <- newWire
modify $ insEdge (n, o, False)
embedAnd' is n
-- | Inserts an Or function into the graph
embedOr :: [Val] -> Val -> GState ()
embedOr iws ow = do
is <- mapM getWire iws
o <- getWire ow
embedOr' is o
embedOr' :: [Node] -> Node -> GState ()
embedOr' is o = do
n <- newWire
modify $ insEdge (n, o, False)
embedNor' is n
-- | Inserts a Nor function into the graph
embedNor :: [Val] -> Val -> GState ()
embedNor iws ow = do
is <- mapM getWire iws
o <- getWire ow
embedNor' is o
embedNor' :: [Node] -> Node -> GState ()
embedNor' is o =
modify $ insEdges [(i, o, False) | i <- is]
-- | Inserts a Xor function into the graph
embedXor :: [Val] -> Val -> GState ()
embedXor iws ow = do
is <- mapM getWire iws
o <- getWire ow
embedXor' is o
embedXor' :: [Node] -> Node -> GState ()
embedXor' [i1, i2] o = do
n1 <- newWire
n2 <- newWire
embedAnd' [n1, n2] o
embedNand' [i1, i2] n1
embedOr' [i1, i2] n2
embedXor' (i1:i2:is) o = do
n <- newWire
embedXor' (n:is) o
embedXor' [i1, i2] n
-- | Inserts a Xnor gate into the graph
embedXnor iws ow = do
is <- mapM getWire iws
o <- getWire ow
n <- newWire
modify $ insEdge (n, o, False)
embedXor' is n
-- | Replaces a node with only one input and one output with an edge.
fixSingleNode :: Int -> G -> G
fixSingleNode n g = case match n g of
(Just ctx, g') -> case ctx of
([(vi, ni)], _, _, [(vo, no)]) -> insEdge (ni, no, not $ vi /= vo) g'
_ -> g
(Nothing, _) -> error "Could not match context in fixSingleNode"
-- | Cleans up graph after adding extra single nodes
fixSingleNodes g = --trace ("fix singles ")
g'
where g' = foldr fixSingleNode g (nodes g)
makeGraphV :: [Verilog] -> G
makeGraphV vs = g
where
(g, _, _, _) = makeGraphV' vs
makeGraphV' :: [Verilog] -> (G, M.Map Val Int, M.Map Val Int, M.Map String Int)
makeGraphV' vs =
runIdx $ flip evalStateT startGraph $ do
mapM_ initGraph vs
mapM_ makeGraphV1 vs
fixSingleNodes <$> get
makeGraphV1 :: Verilog -> GState G
makeGraphV1 v = do
inputs <- mapM getWire (map Input $ _inputs v)
wires <- mapM getWire (map Wire $ _inputs v)
mapM_ (addEdge True) $ zip inputs wires
mapM embedF $ reverse $ _functions v
wire_outs <- mapM getWire (map Wire $ _outputs v)
outs <- mapM getWire (map Output $ _outputs v)
mapM_ (addEdge True) $ zip wire_outs outs
lift resetIdx
get
--fixSingleNodes $
--trace "Finished Embeddeding all functinos" $
-- | Calculates the nodes without input edges
isInput (_, Input _) = True
isInput _ = False
getInputs :: G -> [Node]
getInputs g = [n |
(n, v) <- labNodes g
, isInput (n, v) ]
mybfs :: G -> [Node]
mybfs = topsort
-- | Calculates the nodes without output edges
isOutput (_, Output _) = True
isOutput _ = False
getOutputs :: G -> [Node]
getOutputs g = [n |
(n, v) <- labNodes g
, isOutput (n, v) ]
-- | Renumber the nodes according solely to their inputs,
-- | so nodes with the same inputs will have the same id
-- | regardless of the previous.
{-
mybfs :: Gr a b -> [Int]
mybfs g | isEmpty g = []
| otherwise = inputs g ++ (mybfs $ delNodes (inputs g) g)
-}
-- | simulates the circuit's behavior.
-- | Receives the graph of the circuit as input and a list of inputs, in order.
-- | produces the outputs, in order.
{-
simulate1 :: Context () Bool -> M.IntMap Bool -> M.IntMap Bool
simulate1 = undefined
{-
simulate1 ([], n, (), _) m = case M.lookup n m of
Just v -> m
Nothing -> error "Value for n should have been set."
simulate1 (is, n, (), _) m = M.insert n v m
where
v = and $ [m M.! i /= vi | (vi, i) <- is]
-}
simulate :: [(Int, Bool)] -> G -> [(Int, Bool)]
simulate = undefined
{-
simulate input_values g = [(o, m' M.! o) | o <- outputs g]
where
m = M.fromList input_values
m' = ufold simulate1 m g
-}
randomSimulateIO :: G -> IO [(Int, Bool)]
randomSimulateIO g = do
let is = inputs g
rs <- replicateM (length is) randomIO
return $ simulate (zip is rs) g
contexts :: G -> [Ctx]
contexts g = map (context g) (topsort g) --ufold (:) [] g
removeStuckAt0 :: [Int] -> G -> G
removeStuckAt0 = undefined -- TODO
-}
|
wuerges/vlsi_verification
|
src/Graph.hs
|
Haskell
|
bsd-3-clause
| 7,727
|
{-# LANGUAGE OverloadedStrings, CPP #-}
module Main where
#ifndef TLS
import Control.Monad (when)
#endif
import Data.Version (showVersion)
import Network.Wai.Application.Classic hiding ((</>), (+++))
import System.Directory (getCurrentDirectory)
import System.Environment (getArgs, getEnvironment)
import System.Exit (exitFailure)
import System.FilePath (addTrailingPathSeparator, isAbsolute, normalise, (</>))
import System.IO
import Program.Mighty
import Server
import Paths_mighttpd2 as P
----------------------------------------------------------------
programName :: String
programName = "Mighttpd"
programVersion :: String
programVersion = showVersion P.version
----------------------------------------------------------------
main :: IO ()
main = do
(opt,route) <- getOptRoute
checkTLS opt
let reportFile = reportFileName opt
debug = opt_debug_mode opt
rpt <- initReporter debug reportFile >>= checkReporter reportFile
let run = server opt rpt route
if debug then run else background opt run
where
getOptRoute = getArgs >>= eachCase
svrnm = programName ++ "/" ++ programVersion
eachCase args
| n == 0 = do
root <- amIrootUser
let opt | root = (defaultOption svrnm) { opt_port = 80 }
| otherwise = defaultOption svrnm
env <- getEnvironment
let port = maybe (opt_port opt) read $ lookup "PORT" env
opt' = opt { opt_port = port }
dir <- getCurrentDirectory
let dst = fromString . addTrailingPathSeparator $ dir
route = [Block ["*"] [RouteFile "/" dst]]
return (opt', route)
| n == 2 = do
let config_file = args !! 0
routing_file <- getAbsoluteFile (args !! 1)
opt <- parseOption config_file svrnm
route <- parseRoute routing_file defaultDomain defaultPort
let opt' = opt {opt_routing_file = Just routing_file}
return (opt',route)
| otherwise = do
hPutStrLn stderr "Usage: mighty"
hPutStrLn stderr " mighty config_file routing_file"
exitFailure
where
n = length args
getAbsoluteFile file
| isAbsolute file = return file
| otherwise = do
dir <- getCurrentDirectory
return $ dir </> normalise file
checkReporter _ (Right rpt) = return rpt
checkReporter reportFile (Left e) = do
hPutStrLn stderr $ reportFile ++ " is not writable"
hPrint stderr e
exitFailure
#ifdef TLS
checkTLS _ = return ()
#else
checkTLS opt = when (opt_service opt > 1) $ do
hPutStrLn stderr "This mighty does not support TLS"
exitFailure
#endif
----------------------------------------------------------------
background :: Option -> IO () -> IO ()
background opt svr = do
putStrLn $ "Serving on port " ++ show port ++ " and detaching this terminal..."
putStrLn $ "(If errors occur, they will be written in \"" ++ reportFileName opt ++ "\".)"
hFlush stdout
daemonize svr
where
port = opt_port opt
reportFileName :: Option -> FilePath
reportFileName opt
| port == 80 = rfile
| otherwise = rfile ++ show port
where
rfile = opt_report_file opt
port = opt_port opt
|
mietek/mighttpd2
|
src/Mighty.hs
|
Haskell
|
bsd-3-clause
| 3,290
|
{-# LANGUAGE OverloadedStrings #-}
module Webcrank.Internal.ETag where
import Control.Applicative
import Data.Attoparsec.ByteString.Char8 (parseOnly, string)
import Data.ByteString (ByteString)
import Webcrank.Internal.Parsers
import Webcrank.Internal.Types
-- | Compares two @ETag@s for equality, only considering them equal if
-- they are both strong and byte-for-byte identical.
strongComparison :: ETag -> ETag -> Bool
strongComparison e1 e2 = case (e1, e2) of
(StrongETag v1, StrongETag v2) -> v1 == v2
_ -> False
-- | Compares two @ETag@s for equality, considering them equal whether or
-- not either of them is weak.
weakComparison :: ETag -> ETag -> Bool
weakComparison e1 e2 = opaqueTag e1 == opaqueTag e2
opaqueTag :: ETag -> ByteString
opaqueTag e = case e of StrongETag v -> v; WeakETag v -> v
parseETags :: ByteString -> [ETag]
parseETags = either (const[]) id . parseOnly (csl1 etagP) where
etagP = weakP <|> strongP
weakP = WeakETag <$> (string "W/" *> quotedStringP)
strongP = StrongETag <$> quotedStringP
|
webcrank/webcrank.hs
|
src/Webcrank/Internal/ETag.hs
|
Haskell
|
bsd-3-clause
| 1,040
|
module System.TellMe where
import Control.Monad (void)
import Data.Default (Default(..))
import Graphics.UI.Gtk
data Position = Top | Bottom
deriving (Show, Eq)
data Config = Config { screenNumber :: Int
, monitorNumber :: Int
, barHeight :: Int
, barPosition :: Position
, widgetSpacing :: Int
, startWidgets :: [Widget]
, endWidgets :: [Widget]
}
instance Default Config where
def = Config { screenNumber = 0
, monitorNumber = 0
, barHeight = 25
, barPosition = Bottom
, widgetSpacing = 10
, startWidgets = []
, endWidgets = []
}
setSize :: Config -> Window -> IO ()
setSize cfg window = do
screen <- windowGetScreen window
Rectangle x y w h <- screenGetMonitorGeometry screen $ monitorNumber cfg
let yoff = case barPosition cfg of
Top -> 0
Bottom -> h - barHeight cfg
windowMove window x $ y + yoff
windowSetGeometryHints window
(Nothing :: Maybe Widget)
(Just (w, barHeight cfg))
(Just (w, barHeight cfg))
Nothing
Nothing
Nothing
tellme :: Config -> IO ()
tellme cfg = do
void initGUI
Just disp <- displayGetDefault
screen <- displayGetScreen disp $ screenNumber cfg
window <- windowNew
widgetSetName window "TellMe"
windowSetTypeHint window WindowTypeHintDock
windowSetScreen window screen
setSize cfg window
void $ on screen screenMonitorsChanged $ setSize cfg window
box <- hBoxNew False $ widgetSpacing cfg
containerAdd window box
let addWidgets method widget = do
widgetSetSizeRequest widget (-1) $ barHeight cfg
method box widget PackNatural 0
mapM_ (addWidgets boxPackStart) $ startWidgets cfg
mapM_ (addWidgets boxPackEnd) $ endWidgets cfg
widgetShowAll window
mainGUI
|
izuk/tellme
|
src/System/TellMe.hs
|
Haskell
|
bsd-3-clause
| 2,067
|
-- | This module defines messages from player to debugger
module IMsg
(
IMsg(..),
AMF(..),
AMFValue(..),
amfUndecoratedName,
nextIMessage
)
where
import Data.Word (Word8, Word16, Word32)
import Data.ByteString (ByteString, pack)
import qualified Data.ByteString.Char8 as BSChar
import qualified Data.Iteratee as I
import Data.Iteratee (Iteratee, Endian(LSB), endianRead4, endianRead2)
import Control.Monad (replicateM, when)
-- * Interface
-- | Messages sent by player
data IMsg
-- | 00 or 00
= IMsgMenuState Word32 Word32
-- | 03 or 03
| IMsgCreateAnonymObject Word32
-- | 05 or 05
| IMsgTrace String
-- | 0A or 10
| IMsgSetField Word32 ByteString [Word8]
-- | 0B or 11
| IMsgDeleteField Word32 ByteString
-- | 0C or 12
| IMsgMovieAttr ByteString ByteString
-- | 0E or 14
| IMsgSwdFileEntry Word32 Word32 ByteString ByteString Word32
-- | 0F or 15
| IMsgAskBreakpoints
-- | 10 or 16
| IMsgBreakHit Word16 Word16 Word32 ByteString
-- | 11 or 17
| IMsgBreak
-- | 12 or 18
| IMsgSetLocalVars Word32
-- | 13 or 19
| IMsgBreakpoints [(Word16, Word16)]
-- | 14 or 20
| IMsgNumSwdFileEntry Word32 Word32
-- | 19 or 25
| IMsgProcessTag
-- | 1A or 26
| IMsgVersion Word32 Word8
-- | 1B or 27
| IMsgBreakHitEx Word16 Word16 [(Word16, Word16, Word32, String)]
-- | 1C or 28
| IMsgSetField2 Word32 ByteString [Word8]
-- | 1E or 30
| IMsgGetField AMF [AMF]
-- | 1F or 31
| IMsgFunctionFrame Word32 Word32 AMF [AMF]
-- | 20 or 32
| IMsgDebuggerOption ByteString ByteString
-- | 24 or 36
| IMsgException Word32 String [Word8]
-- | All other
| IMsgUnknown Word32 [Word8]
deriving Show
-- | Represents Action Message Format entry
data AMF = AMF {
amfParent :: Word32,
amfName :: String,
amfFlags :: Word32,
amfValue :: AMFValue
} deriving Show
-- | Some objects (e.g. private members) could be decorated
amfUndecoratedName :: AMF -> String
amfUndecoratedName = reverse . takeWhile (/= ':') . reverse . amfName
-- | Represents AMF value
data AMFValue = AMFDouble Double
| AMFBool Bool
| AMFString String
| AMFObject Word32 Word32 Word16 Word16 String
| AMFNull
| AMFUndefined
| AMFTrails
deriving Show
-- | Read next message from player
nextIMessage :: Monad m => Iteratee ByteString m IMsg
nextIMessage = do
len <- endianRead4 e_
idi <- endianRead4 e_
case idi of
00 -> iterMenuState len
03 -> iterCreateAnonymObject len
05 -> iterTrace len
10 -> iterSetField len
11 -> iterDeleteField len
12 -> iterMovieAttr len
14 -> iterSwdFileEntry len
15 -> iterAskBreakpoints len
16 -> iterBreakHit len
17 -> iterBreak len
18 -> iterSetLocalVars len
19 -> iterBreakpoints len
20 -> iterNumSwdFileEntry len
25 -> iterProcessTag len
26 -> iterVersion len
27 -> iterBreakHitEx len
28 -> iterSetField2 len
30 -> iterGetField len
31 -> iterFunctionFrame len
32 -> iterDebuggerOption len
36 -> iterException len
_ -> iterUnknown idi len
-- * Internals
-- ** Iteratees to parse messages
iterGetField :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterGetField len = do
(amf, ln) <- takeAMF
children <- takeChildren (fromIntegral len - ln) []
return $ IMsgGetField amf children
iterDebuggerOption :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterDebuggerOption len = do
(op, ol) <- takeStr
(val, vl) <- takeStr
when (fromIntegral len /= ol + vl) (fail "iterDebuggerOption: wrong size")
return $ IMsgDebuggerOption op val
iterFunctionFrame :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterFunctionFrame len = do
depth <- endianRead4 e_
when (depth /= 0) (fail "iterFunctionFrame: depth != 0, not implemented")
addr <- endianRead4 e_
(amf, ln) <- takeAMF
children <- takeChildren (fromIntegral len - 4 - 4 - ln) []
return $ IMsgFunctionFrame depth addr amf children
takeChildren :: Monad m => Int -> [AMF] -> Iteratee ByteString m [AMF]
takeChildren 0 res = return $ reverse res
takeChildren l res = do
when (l < 0) (fail "iterFunctionFrame: wrong size")
(amf, vl) <- takeAMF
takeChildren (fromIntegral l - vl) (amf : res)
iterException :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterException len = do
arg1 <- endianRead4 e_
(ex, ln) <- takeStr
arg3 <- replicateM (fromIntegral len - 4 - ln) I.head
return $ IMsgException arg1 (bs2s ex) arg3
iterTrace :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterTrace len = do
(msg, ln) <- takeStr
when (len /= fromIntegral ln) (fail "iterTrace: wrong length")
return $ IMsgTrace $ bs2s msg
iterProcessTag :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterProcessTag len = do
when (len /= 0) (fail "iterProcessTag: wrong length")
return IMsgProcessTag
iterDeleteField :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterDeleteField len = do
addr <- endianRead4 e_
(name, ln) <- takeStr
when (len /= fromIntegral ln + 4) (fail "iterDeleteField: wrong length")
return $ IMsgDeleteField addr name
iterSetField :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterSetField len = do
addr <- endianRead4 e_
(name, ln) <- takeStr
amf <- replicateM (fromIntegral len - 4 - ln) I.head
return $ IMsgSetField addr name amf
iterSetField2 :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterSetField2 len = do
addr <- endianRead4 e_
(name, ln) <- takeStr
amf <- replicateM (fromIntegral len - 4 - ln) I.head
return $ IMsgSetField2 addr name amf
iterMenuState :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterMenuState len = do
when (len /= 8) (fail "iterMenuState: wrong length")
arg1 <- endianRead4 e_
arg2 <- endianRead4 e_
return $ IMsgMenuState arg1 arg2
iterCreateAnonymObject :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterCreateAnonymObject len = do
when (len /= 4) (fail "iterCreateAnonymObject: wrong length")
addr <- endianRead4 e_
return $ IMsgCreateAnonymObject addr
iterSetLocalVars :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterSetLocalVars len = do
when (len /= 4) (fail "iterSetLocalVars: wrong length")
addr <- endianRead4 e_
return $ IMsgSetLocalVars addr
iterBreak :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterBreak len = do
when (len /= 0) (fail "iterBreak: wrong length")
return IMsgBreak
-- XXX: Check length
iterBreakHitEx :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterBreakHitEx _ = do
fileId <- endianRead2 e_
line <- endianRead2 e_
depth <- endianRead4 e_
stack <- replicateM (fromIntegral depth) iterFrame
return $ IMsgBreakHitEx fileId line stack
where
iterFrame = do
fileId <- endianRead2 e_
line <- endianRead2 e_
addr <- endianRead4 e_
(entry, _) <- takeStr
return (fileId, line, addr, bs2s entry)
iterBreakHit :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterBreakHit len = do
fileId <- endianRead2 e_
line <- endianRead2 e_
addr <- endianRead4 e_
(function, ln) <- takeStr
when (len /= fromIntegral ln + 8) (fail "iterBreakHit: wrong length")
return $ IMsgBreakHit fileId line addr function
iterAskBreakpoints :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterAskBreakpoints len = do
when (len /= 0) (fail "iterAskBreakpoints: wrong length")
return IMsgAskBreakpoints
-- XXX: Check length
iterBreakpoints :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterBreakpoints _ = do
count <- endianRead4 e_
l <- replicateM (fromIntegral count) iter'
return $ IMsgBreakpoints l
where
iter' = do
fileId <- endianRead2 e_
line <- endianRead2 e_
return (fileId, line)
iterSwdFileEntry :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterSwdFileEntry len = do
fileId <- endianRead4 e_
unIndex <- endianRead4 e_
(name, ln1) <- takeStr
(source, ln2) <- takeStr
swfIndex <- endianRead4 e_
when (len /= fromIntegral (ln1 + ln2) + 12)
(fail "iterSwdFileEntry: wrong length")
return $ IMsgSwdFileEntry fileId unIndex name source swfIndex
iterUnknown :: Monad m => Word32 -> Word32 -> Iteratee ByteString m IMsg
iterUnknown idi len = do
dat <- replicateM (fromIntegral len) I.head
return $ IMsgUnknown idi dat
iterVersion :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterVersion len = do
when (len /= 5) (fail "iterVersion: wrong length")
major <- endianRead4 e_
minor <- I.head
return $ IMsgVersion major minor
iterMovieAttr :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterMovieAttr len = do
(name, ln1) <- takeStr
(value, ln2) <- takeStr
when (len /= fromIntegral (ln1 + ln2)) (fail "iterMovieAttr: wrong length")
return $ IMsgMovieAttr name value
iterNumSwdFileEntry :: Monad m => Word32 -> Iteratee ByteString m IMsg
iterNumSwdFileEntry len = do
when (len /= 8) (fail "iterNumSwdFileEntry: wrong length")
num <- endianRead4 e_
index <- endianRead4 e_
return $ IMsgNumSwdFileEntry num index
-- ** Utilities
e_ :: Endian
e_ = LSB
-- | Read zero terminated string
-- returns string and number of bytes read
takeStr :: Monad m => Iteratee ByteString m (ByteString, Int)
takeStr = takeStr' [] 0
where
takeStr' :: Monad m =>
[Word8] -> Int -> Iteratee ByteString m (ByteString, Int)
takeStr' cs len = do
c <- I.head
if c == 0
then return . flip (,) (len + 1) . pack . reverse $ cs
else takeStr' (c:cs) (len + 1)
-- | Read AMF
takeAMF :: Monad m => Iteratee ByteString m (AMF, Int)
takeAMF = do
parent <- endianRead4 e_
(name, nl) <- takeStr
vtype <- endianRead2 e_
flags <- endianRead4 e_
(value, vl) <- takeAMFValue vtype
return (AMF parent (bs2s name) flags value, 4 + nl + 2 + 4 + vl)
-- | Read AMF value
takeAMFValue :: Monad m => Word16 -> Iteratee ByteString m (AMFValue, Int)
takeAMFValue 0 = do
(str, ln) <- takeStr
return (AMFDouble . read . bs2s $ str, ln)
takeAMFValue 1 = do
v <- I.head
return (AMFBool (v /= 0), 1)
takeAMFValue 2 = do
(str, ln) <- takeStr
return (AMFString (bs2s str), ln)
takeAMFValue 3 = do
oid <- endianRead4 e_
tp <- endianRead4 e_
isF <- endianRead2 e_
r <- endianRead2 e_
(typeName, tl) <- takeStr
return (AMFObject oid tp isF r (bs2s typeName), 4 + 4 + 2 + 2 + tl)
takeAMFValue 5 = return (AMFNull, 0)
takeAMFValue 6 = return (AMFUndefined, 0)
takeAMFValue 19 = return (AMFTrails, 0)
takeAMFValue tp = fail $ "takeAMFValue: not implemented: " ++ show tp
-- | ByteString to String
bs2s :: ByteString -> String
bs2s = BSChar.unpack
|
Yuras/hfd
|
src/IMsg.hs
|
Haskell
|
bsd-3-clause
| 10,504
|
-- Given N,M, this runs N copies of an M-deep pipeline1.
|
rrnewton/Haskell-CnC
|
examples/empty_task_topologies/par_pipelines.hs
|
Haskell
|
bsd-3-clause
| 59
|
module Homework7.Tree where
import Data.Monoid
data Tree v a = Leaf v a | Branch v (Tree v a) (Tree v a)
tag :: Monoid v => Tree v a -> v
tag (Leaf _ _) = mempty
tag (Branch _ x y) = tag x <> tag y
branch :: Monoid v => Tree v a -> Tree v a -> Tree v a
branch x y = Branch (tag x <> tag y) x y
search :: Monoid v => (v -> Bool) -> Tree v a -> Maybe a
search p t
| p $ tag t = Just $ go mempty p t
| otherwise = Nothing
where
go i p (Leaf _ a) = a
go i p (Branch _ l r)
| p (i <> tag l) = go i p l
| otherwise = go (i <> tag l) p r
|
gabluc/CIS194-Solutions
|
src/Homework7/Tree.hs
|
Haskell
|
bsd-3-clause
| 561
|
{-# LANGUAGE TupleSections, OverloadedStrings #-}
module Himpress.Compile
(compile)
where
import Himpress.Transitions
import Data.Lenses (fetch)
import Data.Monoid
import Data.List (mapAccumL)
import Data.Text (pack)
import Data.Map (fromList)
import Text.Blaze (Html,toHtml)
-- Protoslides - ie. slides with a bunch of text and un-composed transitions.
type PSlide = ([Html],[Transition])
emptyPSlide = ([],[])
splitIntoPSlides::[Element]->[PSlide]
splitIntoPSlides [] = []
splitIntoPSlides (l:ls) = out $ foldl split' (l,emptyPSlide `addToTuple` l,[]) ls
where split' (prev,slide,acc) new
| fusible prev new = (new, addToTuple slide new, acc)
| otherwise = (new, emptyPSlide `addToTuple` new, reverseTuple slide : acc)
-- Reverse the last slide, then the entire presentation.
out (_,l,x) = reverse $ reverseTuple l :x
reverseTuple (a,b) = (reverse a,reverse b)
addToTuple (a,b) = either ((,b) . (:a)) ((a,) . (:b))
-- Text , Text are on the same slide
-- Text, Transition are never
-- Transition, Transition are, iff the second is composable
-- Transition, Text are always
fusible (Left _) (Left _) = True
fusible (Left _) (Right _) = False
fusible (Right _) (Left _) = True
fusible (Right _) (Right t) = composes t
composes = either snd snd
-- Now we may compose all of the transitions on each slide, and get a set of
-- native attributes + a presentation state for each slide
-- This is then converted to a set of native attributes, and sent off for formating.
compose::(Int,Int)->[Transition]->(Native,PState)
compose size = foldl append (mempty,mempty)
where append st = either (combineNative st . fst) (combineChange st . fst)
combineNative (nat,st) new = (nat `mappend` new,st)
combineChange (nat,st) new = (nat, updateState size new st)
toNative::PState->Native
toNative p = Native {classes = mempty, attrs = fromList [
("data-scale", str scale p)
,("data-x", str x p)
,("data-y", str y p)
,("data-z", str z p)
]}
where str lens = pack . show . flip fetch lens
compile::(Int,Int)->[Element]->[Slide]
compile size = snd . mapAccumL buildSlide mempty . splitIntoPSlides
where buildSlide st (body,trans) = let (nat,pstate) = compose size trans
st' = st `mappend` pstate
in (st',(nat `mappend` toNative st', toHtml body))
|
matthewSorensen/himpress
|
Himpress/Compile.hs
|
Haskell
|
bsd-3-clause
| 2,685
|
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE OverloadedStrings #-}
module Pact.MockDb where
import Pact.Types.Runtime
import Data.Aeson
import Data.String
import Data.Default
import Pact.Interpreter
rc :: a -> Method e a
rc = const . return
newtype MockRead =
MockRead (forall k v . (IsString k,FromJSON v) =>
Domain k v -> k -> Method () (Maybe v))
instance Default MockRead where def = MockRead (\_t _k -> rc Nothing)
newtype MockKeys =
MockKeys (forall k v . (IsString k,AsString k) => Domain k v -> Method () [k])
instance Default MockKeys where def = MockKeys (\_t -> rc [])
newtype MockTxIds =
MockTxIds (TableName -> TxId -> Method () [TxId])
instance Default MockTxIds where def = MockTxIds (\_t _i -> rc [])
newtype MockGetUserTableInfo =
MockGetUserTableInfo (TableName -> Method () ModuleName)
instance Default MockGetUserTableInfo where def = MockGetUserTableInfo (\_t -> rc "")
newtype MockCommitTx =
MockCommitTx (Method () [TxLog Value])
instance Default MockCommitTx where def = MockCommitTx (rc [])
newtype MockGetTxLog =
MockGetTxLog (forall k v . (IsString k,FromJSON v) =>
Domain k v -> TxId -> Method () [TxLog v])
instance Default MockGetTxLog where def = MockGetTxLog (\_t _i -> rc [])
data MockDb = MockDb {
mockRead :: MockRead,
mockKeys :: MockKeys,
mockTxIds :: MockTxIds,
mockGetUserTableInfo :: MockGetUserTableInfo,
mockCommitTx :: MockCommitTx,
mockGetTxLog :: MockGetTxLog
}
instance Default MockDb where def = MockDb def def def def def def
pactdb :: MockDb -> PactDb ()
pactdb (MockDb (MockRead r) (MockKeys ks) (MockTxIds tids) (MockGetUserTableInfo uti)
(MockCommitTx c) (MockGetTxLog gt)) = PactDb {
_readRow = r
,
_writeRow = \_wt _t _k _v -> rc ()
,
_keys = ks
,
_txids = tids
,
_createUserTable = \_t _m -> rc ()
,
_getUserTableInfo = uti
,
_beginTx = \_t -> rc Nothing
,
_commitTx = c
,
_rollbackTx = rc ()
,
_getTxLog = gt
}
mkMockEnv :: MockDb -> IO (PactDbEnv ())
mkMockEnv m = mkPactDbEnv (pactdb m) ()
|
kadena-io/pact
|
src-ghc/Pact/MockDb.hs
|
Haskell
|
bsd-3-clause
| 2,063
|
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE RankNTypes #-}
-- | Network-related logic that's mostly methods and dialogs between
-- nodes. Also see "Pos.Chain.Block.Network.Retrieval" for retrieval worker
-- loop logic.
module Pos.Network.Block.Logic
(
BlockNetLogicException (..)
, triggerRecovery
, handleBlocks
, handleUnsolicitedHeader
) where
import Universum
import Control.Concurrent.STM (isFullTBQueue, readTVar, writeTBQueue,
writeTVar)
import Control.Exception (IOException)
import Control.Exception.Safe (Exception (..))
import qualified Data.List.NonEmpty as NE
import qualified Data.Map.Strict as M
import Formatting (bprint, build, sformat, shown, stext, (%))
import qualified Formatting.Buildable as B
import Serokell.Util.Text (listJson)
import qualified System.Metrics.Gauge as Metrics
import Pos.Chain.Block (ApplyBlocksException, Block, BlockHeader,
Blund, HasHeaderHash (..), HeaderHash, LastKnownHeaderTag,
blockHeader, gbHeader, headerHashG, prevBlockL)
import Pos.Chain.Genesis as Genesis (Config (..), configEpochSlots)
import Pos.Chain.Txp (TxpConfiguration)
import Pos.Core (SlotCount, isMoreDifficult)
import Pos.Core.Chrono (NE, NewestFirst (..), OldestFirst (..),
_NewestFirst, _OldestFirst)
import Pos.Core.Conc (forConcurrently)
import Pos.Core.Exception (cardanoExceptionFromException,
cardanoExceptionToException)
import Pos.Core.JsonLog (CanJsonLog (..))
import Pos.Core.Reporting (HasMisbehaviorMetrics (..),
MisbehaviorMetrics (..))
import Pos.Core.Slotting (MonadSlots (getCurrentSlot))
import Pos.Crypto (shortHashF)
import Pos.DB.Block (ClassifyHeaderRes (..), classifyNewHeader,
lcaWithMainChain, verifyAndApplyBlocks)
import qualified Pos.DB.Block as L
import qualified Pos.DB.Block as DB
import Pos.DB.GState.Lock (Priority (..), modifyStateLock)
import Pos.Infra.Communication.Protocol (NodeId)
import Pos.Infra.Diffusion.Types (Diffusion)
import qualified Pos.Infra.Diffusion.Types as Diffusion
import Pos.Infra.Recovery.Info (recoveryInProgress)
import Pos.Infra.Util.JsonLog.Events (MemPoolModifyReason (..),
jlAdoptedBlock)
import Pos.Network.Block.RetrievalQueue (BlockRetrievalQueue,
BlockRetrievalQueueTag, BlockRetrievalTask (..))
import Pos.Network.Block.WorkMode (BlockWorkMode)
import Pos.Util (buildListBounds, multilineBounds, _neLast)
import Pos.Util.AssertMode (inAssertMode)
import Pos.Util.Util (lensOf)
import Pos.Util.Wlog (logDebug, logInfo, logWarning)
----------------------------------------------------------------------------
-- Exceptions
----------------------------------------------------------------------------
-- FIXME this same thing is defined in full diffusion layer.
-- Must finish the proper factoring. There should be no 'Block.Network'
-- in cardano-sl-block; it should just use the Diffusion and Logic interfaces.
data BlockNetLogicException
= DialogUnexpected Text
-- ^ Node's response in any network/block related logic was
-- unexpected.
| BlockNetLogicInternal Text
-- ^ We don't expect this to happen. Most probably it's internal
-- logic error.
deriving (Show)
instance B.Buildable BlockNetLogicException where
build e = bprint ("BlockNetLogicException: "%shown) e
instance Exception BlockNetLogicException where
toException = cardanoExceptionToException
fromException = cardanoExceptionFromException
displayException = toString . pretty
----------------------------------------------------------------------------
-- Recovery
----------------------------------------------------------------------------
-- | Start recovery based on established communication. βStarting recoveryβ
-- means simply sending all our neighbors a 'MsgGetHeaders' message (see
-- 'requestTip'), so sometimes 'triggerRecovery' is used simply to ask for
-- tips.
--
-- Note that when recovery is in progress (see 'recoveryInProgress'),
-- 'triggerRecovery' does nothing. It's okay because when recovery is in
-- progress and 'ncRecoveryHeader' is full, we'll be requesting blocks anyway
-- and until we're finished we shouldn't be asking for new blocks.
triggerRecovery
:: ( BlockWorkMode ctx m
)
=> Genesis.Config -> Diffusion m -> m ()
triggerRecovery genesisConfig diffusion = unlessM (recoveryInProgress $ configEpochSlots genesisConfig) $ do
logDebug "Recovery triggered, requesting tips from neighbors"
-- The 'catch' here is for an exception when trying to enqueue the request.
-- In 'requestTipsAndProcess', IO exceptions are caught, for each
-- individual request per-peer. Those are not re-thrown.
void requestTipsAndProcess `catch`
\(e :: SomeException) -> do
logDebug ("Error happened in triggerRecovery: " <> show e)
throwM e
logDebug "Finished requesting tips for recovery"
where
requestTipsAndProcess = do
requestsMap <- Diffusion.requestTip diffusion
forConcurrently (M.toList requestsMap) $ \it@(nodeId, _) -> waitAndProcessOne it `catch`
-- Catch and squelch IOExceptions so that one failed request to one
-- particlar peer does not stop the others.
\(e :: IOException) ->
logDebug $ sformat ("Error requesting tip from "%shown%": "%shown) nodeId e
waitAndProcessOne (nodeId, mbh) = do
-- 'mbh' is an 'm' term that returns when the header has been
-- downloaded.
bh <- mbh
-- I know, it's not unsolicited. TODO rename.
handleUnsolicitedHeader genesisConfig bh nodeId
----------------------------------------------------------------------------
-- Headers processing
----------------------------------------------------------------------------
handleUnsolicitedHeader
:: BlockWorkMode ctx m
=> Genesis.Config
-> BlockHeader
-> NodeId
-> m ()
handleUnsolicitedHeader genesisConfig header nodeId = do
logDebug $ sformat
("handleUnsolicitedHeader: single header was propagated, processing:\n"
%build) header
classificationRes <- classifyNewHeader genesisConfig header
-- TODO: should we set 'To' hash to hash of header or leave it unlimited?
case classificationRes of
CHContinues -> do
logDebug $ sformat continuesFormat hHash
addHeaderToBlockRequestQueue nodeId header True
CHAlternative -> do
logDebug $ sformat alternativeFormat hHash
addHeaderToBlockRequestQueue nodeId header False
CHUseless reason -> logDebug $ sformat uselessFormat hHash reason
CHInvalid reason -> do
logWarning $ sformat invalidFormat hHash reason
pass -- TODO: ban node for sending invalid block.
where
hHash = headerHash header
continuesFormat =
"Header " %shortHashF %
" is a good continuation of our chain, will process"
alternativeFormat =
"Header " %shortHashF %
" potentially represents good alternative chain, will process"
uselessFormat =
"Header " %shortHashF%" is useless for the following reason: " %stext
invalidFormat =
"handleUnsolicitedHeader: header " %shortHashF%
" is invalid for the following reason: " %stext
----------------------------------------------------------------------------
-- Putting things into request queue
----------------------------------------------------------------------------
-- | Given a valid blockheader and nodeid, this function will put them into
-- download queue and they will be processed later.
addHeaderToBlockRequestQueue
:: forall ctx m.
(BlockWorkMode ctx m)
=> NodeId
-> BlockHeader
-> Bool -- ^ Was the block classified as chain continuation?
-> m ()
addHeaderToBlockRequestQueue nodeId header continues = do
let hHash = headerHash header
logDebug $ sformat ("addToBlockRequestQueue, : "%shortHashF) hHash
queue <- view (lensOf @BlockRetrievalQueueTag)
lastKnownH <- view (lensOf @LastKnownHeaderTag)
added <- atomically $ do
updateLastKnownHeader lastKnownH header
addTaskToBlockRequestQueue nodeId queue $
BlockRetrievalTask { brtHeader = header, brtContinues = continues }
if added
then logDebug $ sformat ("Added headers to block request queue: nodeId="%build%
", header="%build)
nodeId hHash
else logWarning $ sformat ("Failed to add headers from "%build%
" to block retrieval queue: queue is full")
nodeId
addTaskToBlockRequestQueue
:: NodeId
-> BlockRetrievalQueue
-> BlockRetrievalTask
-> STM Bool
addTaskToBlockRequestQueue nodeId queue task = do
ifM (isFullTBQueue queue)
(pure False)
(True <$ writeTBQueue queue (nodeId, task))
updateLastKnownHeader
:: TVar (Maybe BlockHeader)
-> BlockHeader
-> STM ()
updateLastKnownHeader lastKnownH header = do
oldV <- readTVar lastKnownH
let needUpdate = maybe True (header `isMoreDifficult`) oldV
when needUpdate $ writeTVar lastKnownH (Just header)
----------------------------------------------------------------------------
-- Handling blocks
----------------------------------------------------------------------------
-- | Carefully apply blocks that came from the network.
handleBlocks
:: forall ctx m .
( BlockWorkMode ctx m
, HasMisbehaviorMetrics ctx
)
=> Genesis.Config
-> TxpConfiguration
-> OldestFirst NE Block
-> Diffusion m
-> m ()
handleBlocks genesisConfig txpConfig blocks diffusion = do
logDebug "handleBlocks: processing"
inAssertMode $ logInfo $
sformat ("Processing sequence of blocks: " % buildListBounds % "...") $
getOldestFirst $ map headerHash blocks
maybe onNoLca handleBlocksWithLca =<<
lcaWithMainChain (map (view blockHeader) blocks)
inAssertMode $ logDebug $ "Finished processing sequence of blocks"
where
onNoLca = logWarning $
"Sequence of blocks can't be processed, because there is no LCA. " <>
"Probably rollback happened in parallel"
handleBlocksWithLca :: HeaderHash -> m ()
handleBlocksWithLca lcaHash = do
logDebug $ sformat ("Handling block w/ LCA, which is "%shortHashF) lcaHash
-- Head blund in result is the youngest one.
toRollback <- DB.loadBlundsFromTipWhile (configGenesisHash genesisConfig)
$ \blk -> headerHash blk /= lcaHash
maybe (applyWithoutRollback genesisConfig txpConfig diffusion blocks)
(applyWithRollback genesisConfig txpConfig diffusion blocks lcaHash)
(_NewestFirst nonEmpty toRollback)
applyWithoutRollback
:: forall ctx m.
( BlockWorkMode ctx m
, HasMisbehaviorMetrics ctx
)
=> Genesis.Config
-> TxpConfiguration
-> Diffusion m
-> OldestFirst NE Block
-> m ()
applyWithoutRollback genesisConfig txpConfig diffusion blocks = do
logInfo . sformat ("Trying to apply blocks w/o rollback. " % multilineBounds 6)
. getOldestFirst . map (view blockHeader) $ blocks
modifyStateLock HighPriority ApplyBlock applyWithoutRollbackDo >>= \case
Left (pretty -> err) ->
onFailedVerifyBlocks (getOldestFirst blocks) err
Right newTip -> do
when (newTip /= newestTip) $
logWarning $ sformat
("Only blocks up to "%shortHashF%" were applied, "%
"newer were considered invalid")
newTip
let toRelay =
fromMaybe (error "Listeners#applyWithoutRollback is broken") $
find (\b -> headerHash b == newTip) blocks
prefix = blocks
& _OldestFirst %~ NE.takeWhile ((/= newTip) . headerHash)
& map (view blockHeader)
applied = NE.fromList $
getOldestFirst prefix <> one (toRelay ^. blockHeader)
relayBlock epochSlots diffusion toRelay
logInfo $ blocksAppliedMsg applied
for_ blocks $ jsonLog . jlAdoptedBlock
where
epochSlots = configEpochSlots genesisConfig
newestTip = blocks ^. _OldestFirst . _neLast . headerHashG
applyWithoutRollbackDo
:: HeaderHash -> m (HeaderHash, Either ApplyBlocksException HeaderHash)
applyWithoutRollbackDo curTip = do
logInfo "Verifying and applying blocks..."
curSlot <- getCurrentSlot epochSlots
res <- fmap fst <$> verifyAndApplyBlocks genesisConfig txpConfig curSlot False blocks
logInfo "Verifying and applying blocks done"
let newTip = either (const curTip) identity res
pure (newTip, res)
applyWithRollback
:: ( BlockWorkMode ctx m
, HasMisbehaviorMetrics ctx
)
=> Genesis.Config
-> TxpConfiguration
-> Diffusion m
-> OldestFirst NE Block
-> HeaderHash
-> NewestFirst NE Blund
-> m ()
applyWithRollback genesisConfig txpConfig diffusion toApply lca toRollback = do
logInfo . sformat ("Trying to apply blocks w/o rollback. " % multilineBounds 6)
. getOldestFirst . map (view blockHeader) $ toApply
logInfo $ sformat ("Blocks to rollback "%listJson) toRollbackHashes
res <- modifyStateLock HighPriority ApplyBlockWithRollback $ \curTip -> do
res <- L.applyWithRollback genesisConfig txpConfig toRollback toApplyAfterLca
pure (either (const curTip) identity res, res)
case res of
Left err ->
logWarning $ "Couldn't apply blocks with rollback: " <> pretty err
Right newTip -> do
logDebug $ sformat
("Finished applying blocks w/ rollback, relaying new tip: "%shortHashF)
newTip
reportRollback
logInfo $ blocksRolledBackMsg (getNewestFirst toRollback)
logInfo $ blocksAppliedMsg (getOldestFirst toApply)
for_ (getOldestFirst toApply) $ jsonLog . jlAdoptedBlock
relayBlock (configEpochSlots genesisConfig) diffusion
$ toApply ^. _OldestFirst . _neLast
where
toRollbackHashes = fmap headerHash toRollback
reportRollback = do
let rollbackDepth = length toRollback
-- Commit rollback value to EKG
whenJustM (view misbehaviorMetrics) $ liftIO .
flip Metrics.set (fromIntegral rollbackDepth) . _mmRollbacks
panicBrokenLca = error "applyWithRollback: nothing after LCA :<"
toApplyAfterLca =
OldestFirst $
fromMaybe panicBrokenLca $ nonEmpty $
NE.dropWhile ((lca /=) . (^. prevBlockL)) $
getOldestFirst $ toApply
relayBlock
:: forall ctx m.
(BlockWorkMode ctx m)
=> SlotCount -> Diffusion m -> Block -> m ()
relayBlock _ _ (Left _) = logDebug "Not relaying Genesis block"
relayBlock epochSlots diffusion (Right mainBlk) = do
recoveryInProgress epochSlots >>= \case
True -> logDebug "Not relaying block in recovery mode"
False -> do
logDebug $ sformat ("Calling announceBlock for "%shortHashF%".")
(mainBlk ^. gbHeader . headerHashG)
void $ Diffusion.announceBlockHeader diffusion $ mainBlk ^. gbHeader
----------------------------------------------------------------------------
-- Common logging / logic sink points
----------------------------------------------------------------------------
-- TODO: ban node for it!
onFailedVerifyBlocks
:: forall ctx m .
BlockWorkMode ctx m
=> NonEmpty Block -> Text -> m ()
onFailedVerifyBlocks blocks err = do
logWarning $ sformat ("Failed to verify blocks: "%stext%"\n blocks = "%listJson)
err (fmap headerHash blocks)
throwM $ DialogUnexpected err
blocksAppliedMsg
:: forall a.
HasHeaderHash a
=> NonEmpty a -> Text
blocksAppliedMsg (block :| []) =
sformat ("Block has been adopted "%shortHashF) (headerHash block)
blocksAppliedMsg blocks =
sformat ("Blocks have been adopted: "%listJson) (fmap (headerHash @a) blocks)
blocksRolledBackMsg
:: forall a.
HasHeaderHash a
=> NonEmpty a -> Text
blocksRolledBackMsg =
sformat ("Blocks have been rolled back: "%listJson) . fmap (headerHash @a)
|
input-output-hk/pos-haskell-prototype
|
lib/src/Pos/Network/Block/Logic.hs
|
Haskell
|
mit
| 16,755
|
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DuplicateRecordFields #-}
module Language.LSP.Types.CodeLens where
import Data.Aeson
import Data.Aeson.TH
import Language.LSP.Types.Command
import Language.LSP.Types.Location
import Language.LSP.Types.Progress
import Language.LSP.Types.TextDocument
import Language.LSP.Types.Utils
-- -------------------------------------
data CodeLensClientCapabilities =
CodeLensClientCapabilities
{ -- | Whether code lens supports dynamic registration.
_dynamicRegistration :: Maybe Bool
} deriving (Show, Read, Eq)
deriveJSON lspOptions ''CodeLensClientCapabilities
-- -------------------------------------
makeExtendingDatatype "CodeLensOptions" [''WorkDoneProgressOptions]
[ ("_resolveProvider", [t| Maybe Bool |] )]
deriveJSON lspOptions ''CodeLensOptions
makeExtendingDatatype "CodeLensRegistrationOptions"
[ ''TextDocumentRegistrationOptions
, ''CodeLensOptions
] []
deriveJSON lspOptions ''CodeLensRegistrationOptions
-- -------------------------------------
makeExtendingDatatype "CodeLensParams"
[ ''WorkDoneProgressParams,
''PartialResultParams
]
[("_textDocument", [t|TextDocumentIdentifier|])]
deriveJSON lspOptions ''CodeLensParams
-- -------------------------------------
-- | A code lens represents a command that should be shown along with source
-- text, like the number of references, a way to run tests, etc.
--
-- A code lens is _unresolved_ when no command is associated to it. For
-- performance reasons the creation of a code lens and resolving should be done
-- in two stages.
data CodeLens =
CodeLens
{ -- | The range in which this code lens is valid. Should only span a single line.
_range :: Range
, -- | The command this code lens represents.
_command :: Maybe Command
, -- | A data entry field that is preserved on a code lens item between
-- a code lens and a code lens resolve request.
_xdata :: Maybe Value
} deriving (Read,Show,Eq)
deriveJSON lspOptions ''CodeLens
|
alanz/haskell-lsp
|
lsp-types/src/Language/LSP/Types/CodeLens.hs
|
Haskell
|
mit
| 2,015
|
------------------------------------------------------------------------
-- |
-- Module : ALife.Realtra.GeneratePopulation
-- Copyright : (c) Amy de BuitlΓ©ir 2012-2014
-- License : BSD-style
-- Maintainer : amy@nualeargais.ie
-- Stability : experimental
-- Portability : portable
--
-- ???
--
------------------------------------------------------------------------
{-# LANGUAGE TypeFamilies #-}
import ALife.Creatur (agentId)
import ALife.Realtra.Wain (Astronomer, randomAstronomer, summarise,
initialPopulationSize, initialPopulationClassifierSizeRange,
initialPopulationDeciderSizeRange, universe)
import ALife.Creatur.Wain (adjustEnergy)
import ALife.Creatur.Wain.Pretty (pretty)
import ALife.Creatur.Wain.Statistics (Statistic, stats)
import qualified ALife.Realtra.Config as Config
import ALife.Creatur.Universe (Universe, Agent, writeToLog, store)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Random (evalRandIO)
import Control.Monad.Random.Class (getRandomR)
import Control.Monad.State.Lazy (StateT, evalStateT)
names :: [String]
names = map (("Founder" ++) . show)
[1..(initialPopulationSize Config.config)]
introduceRandomAgent
:: (Universe u, Agent u ~ Astronomer)
=> String -> StateT u IO [Statistic]
introduceRandomAgent name = do
classifierSize
<- liftIO . evalRandIO $
getRandomR (initialPopulationClassifierSizeRange Config.config)
deciderSize
<- liftIO . evalRandIO $
getRandomR (initialPopulationDeciderSizeRange Config.config)
-- Make the first generation a little hungry so they start learning
-- immediately.
agent
<- fmap (adjustEnergy 0.8) . liftIO $
evalRandIO ( randomAstronomer name Config.config classifierSize
deciderSize )
writeToLog $ "GeneratePopulation: Created " ++ agentId agent
writeToLog $ "GeneratePopulation: Stats " ++ pretty (stats agent)
store agent
return (stats agent)
introduceRandomAgents
:: (Universe u, Agent u ~ Astronomer)
=> [String] -> StateT u IO ()
introduceRandomAgents ns = do
xs <- mapM introduceRandomAgent ns
summarise xs
main :: IO ()
main = do
print names
evalStateT (introduceRandomAgents names) (universe Config.config)
|
mhwombat/creatur-realtra.OLD
|
src/ALife/Realtra/GeneratePopulation.hs
|
Haskell
|
bsd-3-clause
| 2,239
|
module Network.AMQP.Protocol where
import Control.Monad
import Data.Binary
import Data.Binary.Get
import Data.Binary.Put
import qualified Data.ByteString.Lazy.Char8 as BL
import Network.AMQP.Types
import Network.AMQP.Generated
--True if a content (contentheader and possibly contentbody) will follow the method
hasContent :: FramePayload -> Bool
hasContent (MethodPayload (Basic_get_ok _ _ _ _ _)) = True
hasContent (MethodPayload (Basic_deliver _ _ _ _ _)) = True
hasContent (MethodPayload (Basic_return _ _ _ _)) = True
hasContent _ = False
data Frame = Frame ChannelID FramePayload --channel, payload
deriving Show
instance Binary Frame where
get = do
fType <- getWord8
channel <- get :: Get ChannelID
payloadSize <- get :: Get PayloadSize
payload <- getPayload fType payloadSize :: Get FramePayload
0xCE <- getWord8 --frame end
return $ Frame channel payload
put (Frame chan payload) = do
putWord8 $ frameType payload
put chan
let buf = runPut $ putPayload payload
put ((fromIntegral $ BL.length buf)::PayloadSize)
putLazyByteString buf
putWord8 0xCE
-- gets the size of the frame
-- the bytestring should be at least 7 bytes long, otherwise this method will fail
peekFrameSize :: BL.ByteString -> PayloadSize
peekFrameSize = runGet f
where
f = do
void $ getWord8 -- 1 byte
void $ (get :: Get ChannelID) -- 2 bytes
get :: Get PayloadSize -- 4 bytes
data FramePayload =
MethodPayload MethodPayload
| ContentHeaderPayload ShortInt ShortInt LongLongInt ContentHeaderProperties --classID, weight, bodySize, propertyFields
| ContentBodyPayload BL.ByteString
| HeartbeatPayload
deriving Show
frameType :: FramePayload -> Word8
frameType (MethodPayload _) = 1
frameType (ContentHeaderPayload _ _ _ _) = 2
frameType (ContentBodyPayload _) = 3
frameType HeartbeatPayload = 8
getPayload :: Word8 -> PayloadSize -> Get FramePayload
getPayload 1 _ = do --METHOD FRAME
payLoad <- get :: Get MethodPayload
return (MethodPayload payLoad)
getPayload 2 _ = do --content header frame
classID <- get :: Get ShortInt
weight <- get :: Get ShortInt
bodySize <- get :: Get LongLongInt
props <- getContentHeaderProperties classID
return (ContentHeaderPayload classID weight bodySize props)
getPayload 3 payloadSize = do --content body frame
payload <- getLazyByteString $ fromIntegral payloadSize
return (ContentBodyPayload payload)
getPayload 8 payloadSize = do
-- ignoring the actual payload, but still need to read the bytes from the network buffer
_ <- getLazyByteString $ fromIntegral payloadSize
return HeartbeatPayload
getPayload n _ = error ("Unknown frame payload: " ++ show n)
putPayload :: FramePayload -> Put
putPayload (MethodPayload payload) = put payload
putPayload (ContentHeaderPayload classID weight bodySize p) = do
put classID
put weight
put bodySize
putContentHeaderProperties p
putPayload (ContentBodyPayload payload) = putLazyByteString payload
putPayload HeartbeatPayload = putLazyByteString BL.empty
|
bitemyapp/amqp
|
Network/AMQP/Protocol.hs
|
Haskell
|
bsd-3-clause
| 3,169
|
module Vectorise.Type.TyConDecl (
vectTyConDecls
) where
import GhcPrelude
import Vectorise.Type.Type
import Vectorise.Monad
import Vectorise.Env( GlobalEnv( global_fam_inst_env ) )
import BuildTyCl( TcMethInfo, buildClass, buildDataCon, newTyConRepName )
import OccName
import Class
import Type
import TyCon
import DataCon
import DynFlags
import BasicTypes( DefMethSpec(..) )
import SrcLoc( SrcSpan, noSrcSpan )
import Var
import Name
import Outputable
import Util
import Control.Monad
-- |Vectorise some (possibly recursively defined) type constructors.
--
vectTyConDecls :: [TyCon] -> VM [TyCon]
vectTyConDecls tcs = fixV $ \tcs' ->
do { names' <- mapM (mkLocalisedName mkVectTyConOcc . tyConName) tcs
; mapM_ (uncurry (uncurry defTyConName)) (tcs `zip` names' `zipLazy` tcs')
; zipWithM vectTyConDecl tcs names'
}
-- |Vectorise a single type constructor.
--
vectTyConDecl :: TyCon -> Name -> VM TyCon
vectTyConDecl tycon name'
-- Type constructor representing a type class
| Just cls <- tyConClass_maybe tycon
= do { unless (null $ classATs cls) $
do dflags <- getDynFlags
cantVectorise dflags "Associated types are not yet supported" (ppr cls)
-- vectorise superclass constraint (types)
; theta' <- mapM vectType (classSCTheta cls)
-- vectorise method selectors
; let opItems = classOpItems cls
Just datacon = tyConSingleDataCon_maybe tycon
argTys = dataConRepArgTys datacon -- all selector types
opTys = drop (length argTys - length opItems) argTys -- only method types
; methods' <- sequence [ vectMethod id meth ty | ((id, meth), ty) <- zip opItems opTys]
-- construct the vectorised class (this also creates the class type constructors and its
-- data constructor)
--
-- NB: 'buildClass' attaches new quantifiers and dictionaries to the method types
; cls' <- liftDs $
buildClass
name' -- new name: "V:Class"
(tyConBinders tycon) -- keep original kind
(map (const Nominal) (tyConRoles tycon)) -- all role are N for safety
(snd . classTvsFds $ cls) -- keep the original functional dependencies
(Just (
theta', -- superclasses
[], -- no associated types (for the moment)
methods', -- method info
(classMinimalDef cls))) -- Inherit minimal complete definition from cls
-- the original dictionary constructor must map to the vectorised one
; let tycon' = classTyCon cls'
Just datacon = tyConSingleDataCon_maybe tycon
Just datacon' = tyConSingleDataCon_maybe tycon'
; defDataCon datacon datacon'
-- the original superclass and methods selectors must map to the vectorised ones
; let selIds = classAllSelIds cls
selIds' = classAllSelIds cls'
; zipWithM_ defGlobalVar selIds selIds'
-- return the type constructor of the vectorised class
; return tycon'
}
-- Regular algebraic type constructor β for now, Haskell 2011-style only
| isAlgTyCon tycon
= do { unless (all isVanillaDataCon (tyConDataCons tycon)) $
do dflags <- getDynFlags
cantVectorise dflags "Currently only Haskell 2011 datatypes are supported" (ppr tycon)
-- vectorise the data constructor of the class tycon
; rhs' <- vectAlgTyConRhs tycon (algTyConRhs tycon)
-- keep the original GADT flags
; let gadt_flag = isGadtSyntaxTyCon tycon
-- build the vectorised type constructor
; tc_rep_name <- mkDerivedName mkTyConRepOcc name'
; return $ mkAlgTyCon
name' -- new name
(tyConBinders tycon)
(tyConResKind tycon) -- keep original kind
(map (const Nominal) (tyConRoles tycon)) -- all roles are N for safety
Nothing
[] -- no stupid theta
rhs' -- new constructor defs
(VanillaAlgTyCon tc_rep_name)
gadt_flag -- whether in GADT syntax
}
-- some other crazy thing that we don't handle
| otherwise
= do dflags <- getDynFlags
cantVectorise dflags "Can't vectorise exotic type constructor" (ppr tycon)
-- |Vectorise a class method. (Don't enter it into the vectorisation map yet.)
--
vectMethod :: Id -> DefMethInfo -> Type -> VM TcMethInfo
vectMethod id defMeth ty
= do { -- Vectorise the method type.
; ty' <- vectType ty
-- Create a name for the vectorised method.
; id' <- mkVectId id ty'
; return (Var.varName id', ty', defMethSpecOfDefMeth defMeth)
}
-- | Convert a `DefMethInfo` to a `DefMethSpec`, which discards the name field in
-- the `DefMeth` constructor of the `DefMeth`.
defMethSpecOfDefMeth :: DefMethInfo -> Maybe (DefMethSpec (SrcSpan, Type))
defMethSpecOfDefMeth Nothing = Nothing
defMethSpecOfDefMeth (Just (_, VanillaDM)) = Just VanillaDM
defMethSpecOfDefMeth (Just (_, GenericDM ty)) = Just (GenericDM (noSrcSpan, ty))
-- |Vectorise the RHS of an algebraic type.
--
vectAlgTyConRhs :: TyCon -> AlgTyConRhs -> VM AlgTyConRhs
vectAlgTyConRhs tc (AbstractTyCon {})
= do dflags <- getDynFlags
cantVectorise dflags "Can't vectorise imported abstract type" (ppr tc)
vectAlgTyConRhs _tc (DataTyCon { data_cons = data_cons
, data_cons_size = data_cons_size
, is_enum = is_enum
})
= do { data_cons' <- mapM vectDataCon data_cons
; zipWithM_ defDataCon data_cons data_cons'
; return $ DataTyCon { data_cons = data_cons'
, data_cons_size = data_cons_size
, is_enum = is_enum
}
}
vectAlgTyConRhs tc (TupleTyCon { data_con = con })
= vectAlgTyConRhs tc (mkDataTyConRhs [con])
-- I'm not certain this is what you want to do for tuples,
-- but it's the behaviour we had before I refactored the
-- representation of AlgTyConRhs to add tuples
vectAlgTyConRhs tc (SumTyCon { data_cons = cons
, data_cons_size = data_cons_size })
= -- FIXME (osa): I'm pretty sure this is broken.. TupleTyCon case is probably
-- also broken when the tuple is unboxed.
vectAlgTyConRhs tc (DataTyCon { data_cons = cons
, data_cons_size = data_cons_size
, is_enum = all (((==) 0) . dataConRepArity) cons })
vectAlgTyConRhs tc (NewTyCon {})
= do dflags <- getDynFlags
cantVectorise dflags noNewtypeErr (ppr tc)
where
noNewtypeErr = "Vectorisation of newtypes not supported yet; please use a 'data' declaration"
-- |Vectorise a data constructor by vectorising its argument and return types..
--
vectDataCon :: DataCon -> VM DataCon
vectDataCon dc
| not . null $ ex_tvs
= do dflags <- getDynFlags
cantVectorise dflags "Can't vectorise constructor with existential type variables yet" (ppr dc)
| not . null $ eq_spec
= do dflags <- getDynFlags
cantVectorise dflags "Can't vectorise constructor with equality context yet" (ppr dc)
| not . null $ dataConFieldLabels dc
= do dflags <- getDynFlags
cantVectorise dflags "Can't vectorise constructor with labelled fields yet" (ppr dc)
| not . null $ theta
= do dflags <- getDynFlags
cantVectorise dflags "Can't vectorise constructor with constraint context yet" (ppr dc)
| otherwise
= do { name' <- mkLocalisedName mkVectDataConOcc name
; tycon' <- vectTyCon tycon
; arg_tys <- mapM vectType rep_arg_tys
; let ret_ty = mkFamilyTyConApp tycon' (mkTyVarTys univ_tvs)
; fam_envs <- readGEnv global_fam_inst_env
; rep_nm <- liftDs $ newTyConRepName name'
; let tag_map = mkTyConTagMap tycon'
; liftDs $ buildDataCon fam_envs
name'
(dataConIsInfix dc) -- infix if the original is
rep_nm
(dataConSrcBangs dc) -- strictness as original constructor
(Just $ dataConImplBangs dc)
[] -- no labelled fields for now
univ_tvs -- universally quantified vars
[] -- no existential tvs for now
user_bndrs
[] -- no equalities for now
[] -- no context for now
arg_tys -- argument types
ret_ty -- return type
tycon' -- representation tycon
tag_map
}
where
name = dataConName dc
rep_arg_tys = dataConRepArgTys dc
tycon = dataConTyCon dc
(univ_tvs, ex_tvs, eq_spec, theta, _arg_tys, _res_ty) = dataConFullSig dc
user_bndrs = dataConUserTyVarBinders dc
|
shlevy/ghc
|
compiler/vectorise/Vectorise/Type/TyConDecl.hs
|
Haskell
|
bsd-3-clause
| 9,526
|
import Util
import Timing
import Randomish
import System.Environment
import Control.Exception
import qualified TreeLookupVectorised as TD
import qualified Data.Array.Parallel.PArray as P
import qualified Data.Vector.Unboxed as V
main
= do args <- getArgs
case args of
[alg, count] -> run alg (read count)
_ -> usage
run "vectorised" count
= do let arr = P.fromListPA [0 .. count - 1]
arr `seq` return ()
(arrResult, tElapsed)
<- time
$ let arr' = TD.treeLookupPA arr arr
in P.nfPA arr' `seq` return arr'
print $ P.lengthPA arrResult
putStr $ prettyTime tElapsed
run _ _
= usage
usage = putStr $ unlines
[ "usage: indices <algorithm> <count>\n"
, " algorithm one of " ++ show ["vectorised"]
, ""]
|
mainland/dph
|
dph-lifted-vseg/examples/treeLookup/Main.hs
|
Haskell
|
bsd-3-clause
| 908
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE helpset PUBLIC "-//Sun Microsystems Inc.//DTD JavaHelp HelpSet Version 2.0//EN" "http://java.sun.com/products/javahelp/helpset_2_0.dtd">
<helpset version="2.0" xml:lang="ms-MY">
<title>Getting started Guide</title>
<maps>
<homeID>top</homeID>
<mapref location="map.jhm"/>
</maps>
<view>
<name>TOC</name>
<label>Contents</label>
<type>org.zaproxy.zap.extension.help.ZapTocView</type>
<data>toc.xml</data>
</view>
<view>
<name>Index</name>
<label>Index</label>
<type>javax.help.IndexView</type>
<data>index.xml</data>
</view>
<view>
<name>Search</name>
<label>Search</label>
<type>javax.help.SearchView</type>
<data engine="com.sun.java.help.search.DefaultSearchEngine">
JavaHelpSearch
</data>
</view>
<view>
<name>Favorites</name>
<label>Favorites</label>
<type>javax.help.FavoritesView</type>
</view>
</helpset>
|
rnehra01/zap-extensions
|
src/org/zaproxy/zap/extension/gettingStarted/resources/help_ms_MY/helpset_ms_MY.hs
|
Haskell
|
apache-2.0
| 967
|
{- System.Directory without its conflicting isSymbolicLink
-
- Copyright 2016 Joey Hess <id@joeyh.name>
-
- License: BSD-2-clause
-}
-- Disable warnings because only some versions of System.Directory export
-- isSymbolicLink.
{-# OPTIONS_GHC -fno-warn-tabs -w #-}
module Utility.SystemDirectory (
module System.Directory
) where
import System.Directory hiding (isSymbolicLink, getFileSize)
|
ArchiveTeam/glowing-computing-machine
|
src/Utility/SystemDirectory.hs
|
Haskell
|
bsd-2-clause
| 399
|
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
module StaticFiles where
import Yesod.Static
staticFiles "static"
|
danse/haskellers
|
StaticFiles.hs
|
Haskell
|
bsd-2-clause
| 136
|
data TestType = Foo | Bar
hello :: String
hello = 'H':"ello World!"
-- ziguser = zig
zig :: [TestType]
zig = (Foo:zag)
zag :: [TestType]
zag = Bar:zig
main = print hello
|
themattchan/tandoori
|
input/input-2.hs
|
Haskell
|
bsd-3-clause
| 197
|
module DependencyTest
( tests
)
where
import Test.HUnit
import Data.Graph.Inductive.Graph ( Graph(..) )
import Database.Schema.Migrations.Dependencies
import Common
tests :: [Test]
tests = depGraphTests ++ dependencyTests
type DepGraphTestCase = ([TestDependable], Either String (DependencyGraph TestDependable))
depGraphTestCases :: [DepGraphTestCase]
depGraphTestCases = [ ( []
, Right $ DG [] [] empty
)
, ( [first, second]
, Right $ DG [(first,1),(second,2)]
[("first",1),("second",2)] (mkGraph [(1, "first"), (2, "second")]
[(2, 1, "first -> second")])
)
, ( [cycleFirst, cycleSecond]
, Left "Invalid dependency graph; cycle detected")
]
where
first = TD "first" []
second = TD "second" ["first"]
cycleFirst = TD "first" ["second"]
cycleSecond = TD "second" ["first"]
depGraphTests :: [Test]
depGraphTests = map mkDepGraphTest depGraphTestCases
mkDepGraphTest :: DepGraphTestCase -> Test
mkDepGraphTest (input, expected) = expected ~=? mkDepGraph input
data Direction = Forward | Reverse deriving (Show)
type DependencyTestCase = ([TestDependable], String, Direction, [String])
dependencyTestCases :: [DependencyTestCase]
dependencyTestCases = [ ([TD "first" []], "first", Forward, [])
, ([TD "first" []], "first", Reverse, [])
, ([TD "first" ["second"], TD "second" []], "first", Forward, ["second"])
, ([TD "first" ["second"], TD "second" []], "second", Reverse, ["first"])
, ([TD "first" ["second"], TD "second" ["third"], TD "third" []], "first", Forward, ["third", "second"])
, ([TD "first" ["second"], TD "second" ["third"], TD "third" [], TD "fourth" ["third"]]
, "first", Forward, ["third", "second"])
, ([TD "first" [], TD "second" ["first"]]
, "first", Reverse, ["second"])
, ([TD "first" [], TD "second" ["first"], TD "third" ["second"]]
, "first", Reverse, ["third", "second"])
, ([TD "first" [], TD "second" ["first"], TD "third" ["second"], TD "fourth" ["second"]]
, "first", Reverse, ["fourth", "third", "second"])
, ([ TD "first" ["second"], TD "second" ["third"], TD "third" ["fourth"]
, TD "second" ["fifth"], TD "fifth" ["third"], TD "fourth" []]
, "fourth", Reverse, ["first", "second", "fifth", "third"])
, ([ TD "first" ["second"], TD "second" ["third", "fifth"], TD "third" ["fourth"]
, TD "fifth" ["third"], TD "fourth" []]
, "first", Forward, ["fourth", "third", "fifth", "second"])
]
fromRight :: Either a b -> b
fromRight (Left _) = error "Got a Left value"
fromRight (Right v) = v
mkDependencyTest :: DependencyTestCase -> Test
mkDependencyTest testCase@(deps, a, dir, expected) =
let f = case dir of
Forward -> dependencies
Reverse -> reverseDependencies
in (show testCase) ~: expected ~=? f (fromRight $ mkDepGraph deps) a
dependencyTests :: [Test]
dependencyTests = map mkDependencyTest dependencyTestCases
|
nathankot/dbmigrations
|
test/DependencyTest.hs
|
Haskell
|
bsd-3-clause
| 3,528
|
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
module T8455 where
ty = [t| 5 |]
|
sdiehl/ghc
|
testsuite/tests/quotes/T8455.hs
|
Haskell
|
bsd-3-clause
| 105
|
{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998
Utilities for desugaring
This module exports some utility functions of no great interest.
-}
{-# LANGUAGE CPP #-}
-- | Utility functions for constructing Core syntax, principally for desugaring
module DsUtils (
EquationInfo(..),
firstPat, shiftEqns,
MatchResult(..), CanItFail(..), CaseAlt(..),
cantFailMatchResult, alwaysFailMatchResult,
extractMatchResult, combineMatchResults,
adjustMatchResult, adjustMatchResultDs,
mkCoLetMatchResult, mkViewMatchResult, mkGuardedMatchResult,
matchCanFail, mkEvalMatchResult,
mkCoPrimCaseMatchResult, mkCoAlgCaseMatchResult, mkCoSynCaseMatchResult,
wrapBind, wrapBinds,
mkErrorAppDs, mkCoreAppDs, mkCoreAppsDs, mkCastDs,
seqVar,
-- LHs tuples
mkLHsVarPatTup, mkLHsPatTup, mkVanillaTuplePat,
mkBigLHsVarTupId, mkBigLHsTupId, mkBigLHsVarPatTupId, mkBigLHsPatTupId,
mkSelectorBinds,
selectSimpleMatchVarL, selectMatchVars, selectMatchVar,
mkOptTickBox, mkBinaryTickBox, decideBangHood
) where
#include "HsVersions.h"
import {-# SOURCE #-} Match ( matchSimply )
import HsSyn
import TcHsSyn
import TcType( tcSplitTyConApp )
import CoreSyn
import DsMonad
import {-# SOURCE #-} DsExpr ( dsLExpr )
import CoreUtils
import MkCore
import MkId
import Id
import Literal
import TyCon
-- import ConLike
import DataCon
import PatSyn
import Type
import Coercion
import TysPrim
import TysWiredIn
import BasicTypes
import ConLike
import UniqSet
import UniqSupply
import Module
import PrelNames
import Outputable
import SrcLoc
import Util
import DynFlags
import FastString
import qualified GHC.LanguageExtensions as LangExt
import TcEvidence
import Control.Monad ( zipWithM )
{-
************************************************************************
* *
\subsection{ Selecting match variables}
* *
************************************************************************
We're about to match against some patterns. We want to make some
@Ids@ to use as match variables. If a pattern has an @Id@ readily at
hand, which should indeed be bound to the pattern as a whole, then use it;
otherwise, make one up.
-}
selectSimpleMatchVarL :: LPat Id -> DsM Id
selectSimpleMatchVarL pat = selectMatchVar (unLoc pat)
-- (selectMatchVars ps tys) chooses variables of type tys
-- to use for matching ps against. If the pattern is a variable,
-- we try to use that, to save inventing lots of fresh variables.
--
-- OLD, but interesting note:
-- But even if it is a variable, its type might not match. Consider
-- data T a where
-- T1 :: Int -> T Int
-- T2 :: a -> T a
--
-- f :: T a -> a -> Int
-- f (T1 i) (x::Int) = x
-- f (T2 i) (y::a) = 0
-- Then we must not choose (x::Int) as the matching variable!
-- And nowadays we won't, because the (x::Int) will be wrapped in a CoPat
selectMatchVars :: [Pat Id] -> DsM [Id]
selectMatchVars ps = mapM selectMatchVar ps
selectMatchVar :: Pat Id -> DsM Id
selectMatchVar (BangPat pat) = selectMatchVar (unLoc pat)
selectMatchVar (LazyPat pat) = selectMatchVar (unLoc pat)
selectMatchVar (ParPat pat) = selectMatchVar (unLoc pat)
selectMatchVar (VarPat var) = return (localiseId (unLoc var))
-- Note [Localise pattern binders]
selectMatchVar (AsPat var _) = return (unLoc var)
selectMatchVar other_pat = newSysLocalDs (hsPatType other_pat)
-- OK, better make up one...
{-
Note [Localise pattern binders]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider module M where
[Just a] = e
After renaming it looks like
module M where
[Just M.a] = e
We don't generalise, since it's a pattern binding, monomorphic, etc,
so after desugaring we may get something like
M.a = case e of (v:_) ->
case v of Just M.a -> M.a
Notice the "M.a" in the pattern; after all, it was in the original
pattern. However, after optimisation those pattern binders can become
let-binders, and then end up floated to top level. They have a
different *unique* by then (the simplifier is good about maintaining
proper scoping), but it's BAD to have two top-level bindings with the
External Name M.a, because that turns into two linker symbols for M.a.
It's quite rare for this to actually *happen* -- the only case I know
of is tc003 compiled with the 'hpc' way -- but that only makes it
all the more annoying.
To avoid this, we craftily call 'localiseId' in the desugarer, which
simply turns the External Name for the Id into an Internal one, but
doesn't change the unique. So the desugarer produces this:
M.a{r8} = case e of (v:_) ->
case v of Just a{r8} -> M.a{r8}
The unique is still 'r8', but the binding site in the pattern
is now an Internal Name. Now the simplifier's usual mechanisms
will propagate that Name to all the occurrence sites, as well as
un-shadowing it, so we'll get
M.a{r8} = case e of (v:_) ->
case v of Just a{s77} -> a{s77}
In fact, even CoreSubst.simplOptExpr will do this, and simpleOptExpr
runs on the output of the desugarer, so all is well by the end of
the desugaring pass.
************************************************************************
* *
* type synonym EquationInfo and access functions for its pieces *
* *
************************************************************************
\subsection[EquationInfo-synonym]{@EquationInfo@: a useful synonym}
The ``equation info'' used by @match@ is relatively complicated and
worthy of a type synonym and a few handy functions.
-}
firstPat :: EquationInfo -> Pat Id
firstPat eqn = ASSERT( notNull (eqn_pats eqn) ) head (eqn_pats eqn)
shiftEqns :: [EquationInfo] -> [EquationInfo]
-- Drop the first pattern in each equation
shiftEqns eqns = [ eqn { eqn_pats = tail (eqn_pats eqn) } | eqn <- eqns ]
-- Functions on MatchResults
matchCanFail :: MatchResult -> Bool
matchCanFail (MatchResult CanFail _) = True
matchCanFail (MatchResult CantFail _) = False
alwaysFailMatchResult :: MatchResult
alwaysFailMatchResult = MatchResult CanFail (\fail -> return fail)
cantFailMatchResult :: CoreExpr -> MatchResult
cantFailMatchResult expr = MatchResult CantFail (\_ -> return expr)
extractMatchResult :: MatchResult -> CoreExpr -> DsM CoreExpr
extractMatchResult (MatchResult CantFail match_fn) _
= match_fn (error "It can't fail!")
extractMatchResult (MatchResult CanFail match_fn) fail_expr = do
(fail_bind, if_it_fails) <- mkFailurePair fail_expr
body <- match_fn if_it_fails
return (mkCoreLet fail_bind body)
combineMatchResults :: MatchResult -> MatchResult -> MatchResult
combineMatchResults (MatchResult CanFail body_fn1)
(MatchResult can_it_fail2 body_fn2)
= MatchResult can_it_fail2 body_fn
where
body_fn fail = do body2 <- body_fn2 fail
(fail_bind, duplicatable_expr) <- mkFailurePair body2
body1 <- body_fn1 duplicatable_expr
return (Let fail_bind body1)
combineMatchResults match_result1@(MatchResult CantFail _) _
= match_result1
adjustMatchResult :: DsWrapper -> MatchResult -> MatchResult
adjustMatchResult encl_fn (MatchResult can_it_fail body_fn)
= MatchResult can_it_fail (\fail -> encl_fn <$> body_fn fail)
adjustMatchResultDs :: (CoreExpr -> DsM CoreExpr) -> MatchResult -> MatchResult
adjustMatchResultDs encl_fn (MatchResult can_it_fail body_fn)
= MatchResult can_it_fail (\fail -> encl_fn =<< body_fn fail)
wrapBinds :: [(Var,Var)] -> CoreExpr -> CoreExpr
wrapBinds [] e = e
wrapBinds ((new,old):prs) e = wrapBind new old (wrapBinds prs e)
wrapBind :: Var -> Var -> CoreExpr -> CoreExpr
wrapBind new old body -- NB: this function must deal with term
| new==old = body -- variables, type variables or coercion variables
| otherwise = Let (NonRec new (varToCoreExpr old)) body
seqVar :: Var -> CoreExpr -> CoreExpr
seqVar var body = Case (Var var) var (exprType body)
[(DEFAULT, [], body)]
mkCoLetMatchResult :: CoreBind -> MatchResult -> MatchResult
mkCoLetMatchResult bind = adjustMatchResult (mkCoreLet bind)
-- (mkViewMatchResult var' viewExpr mr) makes the expression
-- let var' = viewExpr in mr
mkViewMatchResult :: Id -> CoreExpr -> MatchResult -> MatchResult
mkViewMatchResult var' viewExpr =
adjustMatchResult (mkCoreLet (NonRec var' viewExpr))
mkEvalMatchResult :: Id -> Type -> MatchResult -> MatchResult
mkEvalMatchResult var ty
= adjustMatchResult (\e -> Case (Var var) var ty [(DEFAULT, [], e)])
mkGuardedMatchResult :: CoreExpr -> MatchResult -> MatchResult
mkGuardedMatchResult pred_expr (MatchResult _ body_fn)
= MatchResult CanFail (\fail -> do body <- body_fn fail
return (mkIfThenElse pred_expr body fail))
mkCoPrimCaseMatchResult :: Id -- Scrutinee
-> Type -- Type of the case
-> [(Literal, MatchResult)] -- Alternatives
-> MatchResult -- Literals are all unlifted
mkCoPrimCaseMatchResult var ty match_alts
= MatchResult CanFail mk_case
where
mk_case fail = do
alts <- mapM (mk_alt fail) sorted_alts
return (Case (Var var) var ty ((DEFAULT, [], fail) : alts))
sorted_alts = sortWith fst match_alts -- Right order for a Case
mk_alt fail (lit, MatchResult _ body_fn)
= ASSERT( not (litIsLifted lit) )
do body <- body_fn fail
return (LitAlt lit, [], body)
data CaseAlt a = MkCaseAlt{ alt_pat :: a,
alt_bndrs :: [Var],
alt_wrapper :: HsWrapper,
alt_result :: MatchResult }
mkCoAlgCaseMatchResult
:: DynFlags
-> Id -- Scrutinee
-> Type -- Type of exp
-> [CaseAlt DataCon] -- Alternatives (bndrs *include* tyvars, dicts)
-> MatchResult
mkCoAlgCaseMatchResult dflags var ty match_alts
| isNewtype -- Newtype case; use a let
= ASSERT( null (tail match_alts) && null (tail arg_ids1) )
mkCoLetMatchResult (NonRec arg_id1 newtype_rhs) match_result1
| isPArrFakeAlts match_alts
= MatchResult CanFail $ mkPArrCase dflags var ty (sort_alts match_alts)
| otherwise
= mkDataConCase var ty match_alts
where
isNewtype = isNewTyCon (dataConTyCon (alt_pat alt1))
-- [Interesting: because of GADTs, we can't rely on the type of
-- the scrutinised Id to be sufficiently refined to have a TyCon in it]
alt1@MkCaseAlt{ alt_bndrs = arg_ids1, alt_result = match_result1 }
= ASSERT( notNull match_alts ) head match_alts
-- Stuff for newtype
arg_id1 = ASSERT( notNull arg_ids1 ) head arg_ids1
var_ty = idType var
(tc, ty_args) = tcSplitTyConApp var_ty -- Don't look through newtypes
-- (not that splitTyConApp does, these days)
newtype_rhs = unwrapNewTypeBody tc ty_args (Var var)
--- Stuff for parallel arrays
--
-- Concerning `isPArrFakeAlts':
--
-- * it is *not* sufficient to just check the type of the type
-- constructor, as we have to be careful not to confuse the real
-- representation of parallel arrays with the fake constructors;
-- moreover, a list of alternatives must not mix fake and real
-- constructors (this is checked earlier on)
--
-- FIXME: We actually go through the whole list and make sure that
-- either all or none of the constructors are fake parallel
-- array constructors. This is to spot equations that mix fake
-- constructors with the real representation defined in
-- `PrelPArr'. It would be nicer to spot this situation
-- earlier and raise a proper error message, but it can really
-- only happen in `PrelPArr' anyway.
--
isPArrFakeAlts :: [CaseAlt DataCon] -> Bool
isPArrFakeAlts [alt] = isPArrFakeCon (alt_pat alt)
isPArrFakeAlts (alt:alts) =
case (isPArrFakeCon (alt_pat alt), isPArrFakeAlts alts) of
(True , True ) -> True
(False, False) -> False
_ -> panic "DsUtils: you may not mix `[:...:]' with `PArr' patterns"
isPArrFakeAlts [] = panic "DsUtils: unexpectedly found an empty list of PArr fake alternatives"
mkCoSynCaseMatchResult :: Id -> Type -> CaseAlt PatSyn -> MatchResult
mkCoSynCaseMatchResult var ty alt = MatchResult CanFail $ mkPatSynCase var ty alt
sort_alts :: [CaseAlt DataCon] -> [CaseAlt DataCon]
sort_alts = sortWith (dataConTag . alt_pat)
mkPatSynCase :: Id -> Type -> CaseAlt PatSyn -> CoreExpr -> DsM CoreExpr
mkPatSynCase var ty alt fail = do
matcher <- dsLExpr $ mkLHsWrap wrapper $
nlHsTyApp matcher [getRuntimeRep "mkPatSynCase" ty, ty]
let MatchResult _ mkCont = match_result
cont <- mkCoreLams bndrs <$> mkCont fail
return $ mkCoreAppsDs (text "patsyn" <+> ppr var) matcher [Var var, ensure_unstrict cont, Lam voidArgId fail]
where
MkCaseAlt{ alt_pat = psyn,
alt_bndrs = bndrs,
alt_wrapper = wrapper,
alt_result = match_result} = alt
(matcher, needs_void_lam) = patSynMatcher psyn
-- See Note [Matchers and builders for pattern synonyms] in PatSyns
-- on these extra Void# arguments
ensure_unstrict cont | needs_void_lam = Lam voidArgId cont
| otherwise = cont
mkDataConCase :: Id -> Type -> [CaseAlt DataCon] -> MatchResult
mkDataConCase _ _ [] = panic "mkDataConCase: no alternatives"
mkDataConCase var ty alts@(alt1:_) = MatchResult fail_flag mk_case
where
con1 = alt_pat alt1
tycon = dataConTyCon con1
data_cons = tyConDataCons tycon
match_results = map alt_result alts
sorted_alts :: [CaseAlt DataCon]
sorted_alts = sort_alts alts
var_ty = idType var
(_, ty_args) = tcSplitTyConApp var_ty -- Don't look through newtypes
-- (not that splitTyConApp does, these days)
mk_case :: CoreExpr -> DsM CoreExpr
mk_case fail = do
alts <- mapM (mk_alt fail) sorted_alts
return $ mkWildCase (Var var) (idType var) ty (mk_default fail ++ alts)
mk_alt :: CoreExpr -> CaseAlt DataCon -> DsM CoreAlt
mk_alt fail MkCaseAlt{ alt_pat = con,
alt_bndrs = args,
alt_result = MatchResult _ body_fn }
= do { body <- body_fn fail
; case dataConBoxer con of {
Nothing -> return (DataAlt con, args, body) ;
Just (DCB boxer) ->
do { us <- newUniqueSupply
; let (rep_ids, binds) = initUs_ us (boxer ty_args args)
; return (DataAlt con, rep_ids, mkLets binds body) } } }
mk_default :: CoreExpr -> [CoreAlt]
mk_default fail | exhaustive_case = []
| otherwise = [(DEFAULT, [], fail)]
fail_flag :: CanItFail
fail_flag | exhaustive_case
= foldr orFail CantFail [can_it_fail | MatchResult can_it_fail _ <- match_results]
| otherwise
= CanFail
mentioned_constructors = mkUniqSet $ map alt_pat alts
un_mentioned_constructors
= mkUniqSet data_cons `minusUniqSet` mentioned_constructors
exhaustive_case = isEmptyUniqSet un_mentioned_constructors
--- Stuff for parallel arrays
--
-- * the following is to desugar cases over fake constructors for
-- parallel arrays, which are introduced by `tidy1' in the `PArrPat'
-- case
--
mkPArrCase :: DynFlags -> Id -> Type -> [CaseAlt DataCon] -> CoreExpr -> DsM CoreExpr
mkPArrCase dflags var ty sorted_alts fail = do
lengthP <- dsDPHBuiltin lengthPVar
alt <- unboxAlt
return (mkWildCase (len lengthP) intTy ty [alt])
where
elemTy = case splitTyConApp (idType var) of
(_, [elemTy]) -> elemTy
_ -> panic panicMsg
panicMsg = "DsUtils.mkCoAlgCaseMatchResult: not a parallel array?"
len lengthP = mkApps (Var lengthP) [Type elemTy, Var var]
--
unboxAlt = do
l <- newSysLocalDs intPrimTy
indexP <- dsDPHBuiltin indexPVar
alts <- mapM (mkAlt indexP) sorted_alts
return (DataAlt intDataCon, [l], mkWildCase (Var l) intPrimTy ty (dft : alts))
where
dft = (DEFAULT, [], fail)
--
-- each alternative matches one array length (corresponding to one
-- fake array constructor), so the match is on a literal; each
-- alternative's body is extended by a local binding for each
-- constructor argument, which are bound to array elements starting
-- with the first
--
mkAlt indexP alt@MkCaseAlt{alt_result = MatchResult _ bodyFun} = do
body <- bodyFun fail
return (LitAlt lit, [], mkCoreLets binds body)
where
lit = MachInt $ toInteger (dataConSourceArity (alt_pat alt))
binds = [NonRec arg (indexExpr i) | (i, arg) <- zip [1..] (alt_bndrs alt)]
--
indexExpr i = mkApps (Var indexP) [Type elemTy, Var var, mkIntExpr dflags i]
{-
************************************************************************
* *
\subsection{Desugarer's versions of some Core functions}
* *
************************************************************************
-}
mkErrorAppDs :: Id -- The error function
-> Type -- Type to which it should be applied
-> SDoc -- The error message string to pass
-> DsM CoreExpr
mkErrorAppDs err_id ty msg = do
src_loc <- getSrcSpanDs
dflags <- getDynFlags
let
full_msg = showSDoc dflags (hcat [ppr src_loc, vbar, msg])
core_msg = Lit (mkMachString full_msg)
-- mkMachString returns a result of type String#
return (mkApps (Var err_id) [Type (getRuntimeRep "mkErrorAppDs" ty), Type ty, core_msg])
{-
'mkCoreAppDs' and 'mkCoreAppsDs' hand the special-case desugaring of 'seq'.
Note [Desugaring seq (1)] cf Trac #1031
~~~~~~~~~~~~~~~~~~~~~~~~~
f x y = x `seq` (y `seq` (# x,y #))
The [CoreSyn let/app invariant] means that, other things being equal, because
the argument to the outer 'seq' has an unlifted type, we'll use call-by-value thus:
f x y = case (y `seq` (# x,y #)) of v -> x `seq` v
But that is bad for two reasons:
(a) we now evaluate y before x, and
(b) we can't bind v to an unboxed pair
Seq is very, very special! So we recognise it right here, and desugar to
case x of _ -> case y of _ -> (# x,y #)
Note [Desugaring seq (2)] cf Trac #2273
~~~~~~~~~~~~~~~~~~~~~~~~~
Consider
let chp = case b of { True -> fst x; False -> 0 }
in chp `seq` ...chp...
Here the seq is designed to plug the space leak of retaining (snd x)
for too long.
If we rely on the ordinary inlining of seq, we'll get
let chp = case b of { True -> fst x; False -> 0 }
case chp of _ { I# -> ...chp... }
But since chp is cheap, and the case is an alluring contet, we'll
inline chp into the case scrutinee. Now there is only one use of chp,
so we'll inline a second copy. Alas, we've now ruined the purpose of
the seq, by re-introducing the space leak:
case (case b of {True -> fst x; False -> 0}) of
I# _ -> ...case b of {True -> fst x; False -> 0}...
We can try to avoid doing this by ensuring that the binder-swap in the
case happens, so we get his at an early stage:
case chp of chp2 { I# -> ...chp2... }
But this is fragile. The real culprit is the source program. Perhaps we
should have said explicitly
let !chp2 = chp in ...chp2...
But that's painful. So the code here does a little hack to make seq
more robust: a saturated application of 'seq' is turned *directly* into
the case expression, thus:
x `seq` e2 ==> case x of x -> e2 -- Note shadowing!
e1 `seq` e2 ==> case x of _ -> e2
So we desugar our example to:
let chp = case b of { True -> fst x; False -> 0 }
case chp of chp { I# -> ...chp... }
And now all is well.
The reason it's a hack is because if you define mySeq=seq, the hack
won't work on mySeq.
Note [Desugaring seq (3)] cf Trac #2409
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The isLocalId ensures that we don't turn
True `seq` e
into
case True of True { ... }
which stupidly tries to bind the datacon 'True'.
-}
mkCoreAppDs :: SDoc -> CoreExpr -> CoreExpr -> CoreExpr
mkCoreAppDs _ (Var f `App` Type ty1 `App` Type ty2 `App` arg1) arg2
| f `hasKey` seqIdKey -- Note [Desugaring seq (1), (2)]
= Case arg1 case_bndr ty2 [(DEFAULT,[],arg2)]
where
case_bndr = case arg1 of
Var v1 | isLocalId v1 -> v1 -- Note [Desugaring seq (2) and (3)]
_ -> mkWildValBinder ty1
mkCoreAppDs s fun arg = mkCoreApp s fun arg -- The rest is done in MkCore
mkCoreAppsDs :: SDoc -> CoreExpr -> [CoreExpr] -> CoreExpr
mkCoreAppsDs s fun args = foldl (mkCoreAppDs s) fun args
mkCastDs :: CoreExpr -> Coercion -> CoreExpr
-- We define a desugarer-specific version of CoreUtils.mkCast,
-- because in the immediate output of the desugarer, we can have
-- apparently-mis-matched coercions: E.g.
-- let a = b
-- in (x :: a) |> (co :: b ~ Int)
-- Lint know about type-bindings for let and does not complain
-- So here we do not make the assertion checks that we make in
-- CoreUtils.mkCast; and we do less peephole optimisation too
mkCastDs e co | isReflCo co = e
| otherwise = Cast e co
{-
************************************************************************
* *
Tuples and selector bindings
* *
************************************************************************
This is used in various places to do with lazy patterns.
For each binder $b$ in the pattern, we create a binding:
\begin{verbatim}
b = case v of pat' -> b'
\end{verbatim}
where @pat'@ is @pat@ with each binder @b@ cloned into @b'@.
ToDo: making these bindings should really depend on whether there's
much work to be done per binding. If the pattern is complex, it
should be de-mangled once, into a tuple (and then selected from).
Otherwise the demangling can be in-line in the bindings (as here).
Boring! Boring! One error message per binder. The above ToDo is
even more helpful. Something very similar happens for pattern-bound
expressions.
Note [mkSelectorBinds]
~~~~~~~~~~~~~~~~~~~~~~
mkSelectorBinds is used to desugar a pattern binding {p = e},
in a binding group:
let { ...; p = e; ... } in body
where p binds x,y (this list of binders can be empty).
There are two cases.
General case (A).
In the general case we generate these bindings (A)
{ t = case e of p -> (x,y)
; x = case t of (x,y) -> x
; y = case t of (x,y) -> y }
and we return 't' as the variable to force if the pattern
is strict. So with -XStrict or an outermost-bang-pattern, the binding
let p = e in body
would turn into
let { t = case e of p -> (x,y)
; x = case t of (x,y) -> x
; y = case t of (x,y) -> y }
in t `seq` t
Special case (B).
For a pattern that is essentially just a tuple:
* A product type, so cannot fail
* Only one level, so that
- generating multiple matches is fine
- seq'ing it evaluates the same as matching it
Then instead we generate
{ v = e
; x = case v of p -> x
; y = case v of p -> y }
with 'v' as the variable to force
Examples:
* !(_, (_, a)) = e
==>
t = case e of (_, (_, a)) -> Unit a
a = case t of Unit a -> a
Note that
- Forcing 't' will force the pattern to match fully;
e.g. will diverge if (snd e) is bottom
- But 'a' itself is not forced; it is wrapped in a one-tuple
(see Note [One-tuples] in TysWiredIn)
* !(Just x) = e
==>
t = case e of Just x -> Unit x
x = case t of Unit x -> x
Again, forcing 't' will fail if 'e' yields Nothing.
Note that even though this is rather general, the special cases
work out well:
* One binder, not -XStrict:
let Just (Just v) = e in body
==>
let t = case e of Just (Just v) -> Unit v
v = case t of Unit v -> v
in body
==>
let v = case (case e of Just (Just v) -> Unit v) of
Unit v -> v
in body
==>
let v = case e of Just (Just v) -> v
in body
* Non-recursive, -XStrict
let p = e in body
==>
let { t = case e of p -> (x,y)
; x = case t of (x,y) -> x
; y = case t of (x,y) -> x }
in t `seq` body
==> {inline seq, float x,y bindings inwards}
let t = case e of p -> (x,y) in
case t of t' ->
let { x = case t' of (x,y) -> x
; y = case t' of (x,y) -> x } in
body
==> {inline t, do case of case}
case e of p ->
let t = (x,y) in
let { x = case t' of (x,y) -> x
; y = case t' of (x,y) -> x } in
body
==> {case-cancellation, drop dead code}
case e of p -> body
* Special case (B) is there to avoid fruitlessly taking the tuple
apart and rebuilding it. For example, consider
{ K x y = e }
where K is a product constructor. Then general case (A) does:
{ t = case e of K x y -> (x,y)
; x = case t of (x,y) -> x
; y = case t of (x,y) -> y }
In the lazy case we can't optimise out this fruitless taking apart
and rebuilding. Instead (B) builds
{ v = e
; x = case v of K x y -> x
; y = case v of K x y -> y }
which is better.
-}
mkSelectorBinds :: [[Tickish Id]] -- ^ ticks to add, possibly
-> LPat Id -- ^ The pattern
-> CoreExpr -- ^ Expression to which the pattern is bound
-> DsM (Id,[(Id,CoreExpr)])
-- ^ Id the rhs is bound to, for desugaring strict
-- binds (see Note [Desugar Strict binds] in DsBinds)
-- and all the desugared binds
mkSelectorBinds ticks pat val_expr
| is_simple_lpat pat -- Special case (B)
= do { let pat_ty = hsLPatType pat
; val_var <- newSysLocalDs pat_ty
; let mk_bind scrut_var tick bndr_var
-- (mk_bind sv bv) generates bv = case sv of { pat -> bv }
-- Remember, 'pat' binds 'bv'
= do { rhs_expr <- matchSimply (Var scrut_var) PatBindRhs pat
(Var bndr_var)
(Var bndr_var) -- Neat hack
-- Neat hack: since 'pat' can't fail, the
-- "fail-expr" passed to matchSimply is not
-- used. But it /is/ used for its type, and for
-- that bndr_var is just the ticket.
; return (bndr_var, mkOptTickBox tick rhs_expr) }
; binds <- zipWithM (mk_bind val_var) ticks' binders
; return ( val_var, (val_var, val_expr) : binds) }
| otherwise
= do { tuple_var <- newSysLocalDs tuple_ty
; error_expr <- mkErrorAppDs iRREFUT_PAT_ERROR_ID tuple_ty (ppr pat)
; tuple_expr <- matchSimply val_expr PatBindRhs pat
local_tuple error_expr
; let mk_tup_bind tick binder
= (binder, mkOptTickBox tick $
mkTupleSelector1 local_binders binder
tuple_var (Var tuple_var))
tup_binds = zipWith mk_tup_bind ticks' binders
; return (tuple_var, (tuple_var, tuple_expr) : tup_binds) }
where
binders = collectPatBinders pat
ticks' = ticks ++ repeat []
local_binders = map localiseId binders -- See Note [Localise pattern binders]
local_tuple = mkBigCoreVarTup1 binders
tuple_ty = exprType local_tuple
is_simple_lpat :: LPat a -> Bool
is_simple_lpat p = is_simple_pat (unLoc p)
is_simple_pat :: Pat a -> Bool
is_simple_pat (VarPat _) = True
is_simple_pat (ParPat p) = is_simple_lpat p
is_simple_pat (TuplePat ps Boxed _) = all is_triv_lpat ps
is_simple_pat (ConPatOut { pat_con = con
, pat_args = ps}) = is_simple_con_pat con ps
is_simple_pat _ = False
is_simple_con_pat :: Located ConLike -> HsConPatDetails a -> Bool
is_simple_con_pat con args
= case con of
L _ (RealDataCon con) -> isProductTyCon (dataConTyCon con)
&& all is_triv_lpat (hsConPatArgs args)
L _ (PatSynCon {}) -> False
is_triv_lpat :: LPat a -> Bool
is_triv_lpat p = is_triv_pat (unLoc p)
is_triv_pat :: Pat a -> Bool
is_triv_pat (VarPat _) = True
is_triv_pat (WildPat _) = True
is_triv_pat (ParPat p) = is_triv_lpat p
is_triv_pat _ = False
{- *********************************************************************
* *
Creating big tuples and their types for full Haskell expressions.
They work over *Ids*, and create tuples replete with their types,
which is whey they are not in HsUtils.
* *
********************************************************************* -}
mkLHsPatTup :: [LPat Id] -> LPat Id
mkLHsPatTup [] = noLoc $ mkVanillaTuplePat [] Boxed
mkLHsPatTup [lpat] = lpat
mkLHsPatTup lpats = L (getLoc (head lpats)) $
mkVanillaTuplePat lpats Boxed
mkLHsVarPatTup :: [Id] -> LPat Id
mkLHsVarPatTup bs = mkLHsPatTup (map nlVarPat bs)
mkVanillaTuplePat :: [OutPat Id] -> Boxity -> Pat Id
-- A vanilla tuple pattern simply gets its type from its sub-patterns
mkVanillaTuplePat pats box = TuplePat pats box (map hsLPatType pats)
-- The Big equivalents for the source tuple expressions
mkBigLHsVarTupId :: [Id] -> LHsExpr Id
mkBigLHsVarTupId ids = mkBigLHsTupId (map nlHsVar ids)
mkBigLHsTupId :: [LHsExpr Id] -> LHsExpr Id
mkBigLHsTupId = mkChunkified mkLHsTupleExpr
-- The Big equivalents for the source tuple patterns
mkBigLHsVarPatTupId :: [Id] -> LPat Id
mkBigLHsVarPatTupId bs = mkBigLHsPatTupId (map nlVarPat bs)
mkBigLHsPatTupId :: [LPat Id] -> LPat Id
mkBigLHsPatTupId = mkChunkified mkLHsPatTup
{-
************************************************************************
* *
Code for pattern-matching and other failures
* *
************************************************************************
Generally, we handle pattern matching failure like this: let-bind a
fail-variable, and use that variable if the thing fails:
\begin{verbatim}
let fail.33 = error "Help"
in
case x of
p1 -> ...
p2 -> fail.33
p3 -> fail.33
p4 -> ...
\end{verbatim}
Then
\begin{itemize}
\item
If the case can't fail, then there'll be no mention of @fail.33@, and the
simplifier will later discard it.
\item
If it can fail in only one way, then the simplifier will inline it.
\item
Only if it is used more than once will the let-binding remain.
\end{itemize}
There's a problem when the result of the case expression is of
unboxed type. Then the type of @fail.33@ is unboxed too, and
there is every chance that someone will change the let into a case:
\begin{verbatim}
case error "Help" of
fail.33 -> case ....
\end{verbatim}
which is of course utterly wrong. Rather than drop the condition that
only boxed types can be let-bound, we just turn the fail into a function
for the primitive case:
\begin{verbatim}
let fail.33 :: Void -> Int#
fail.33 = \_ -> error "Help"
in
case x of
p1 -> ...
p2 -> fail.33 void
p3 -> fail.33 void
p4 -> ...
\end{verbatim}
Now @fail.33@ is a function, so it can be let-bound.
-}
mkFailurePair :: CoreExpr -- Result type of the whole case expression
-> DsM (CoreBind, -- Binds the newly-created fail variable
-- to \ _ -> expression
CoreExpr) -- Fail variable applied to realWorld#
-- See Note [Failure thunks and CPR]
mkFailurePair expr
= do { fail_fun_var <- newFailLocalDs (voidPrimTy `mkFunTy` ty)
; fail_fun_arg <- newSysLocalDs voidPrimTy
; let real_arg = setOneShotLambda fail_fun_arg
; return (NonRec fail_fun_var (Lam real_arg expr),
App (Var fail_fun_var) (Var voidPrimId)) }
where
ty = exprType expr
{-
Note [Failure thunks and CPR]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When we make a failure point we ensure that it
does not look like a thunk. Example:
let fail = \rw -> error "urk"
in case x of
[] -> fail realWorld#
(y:ys) -> case ys of
[] -> fail realWorld#
(z:zs) -> (y,z)
Reason: we know that a failure point is always a "join point" and is
entered at most once. Adding a dummy 'realWorld' token argument makes
it clear that sharing is not an issue. And that in turn makes it more
CPR-friendly. This matters a lot: if you don't get it right, you lose
the tail call property. For example, see Trac #3403.
************************************************************************
* *
Ticks
* *
********************************************************************* -}
mkOptTickBox :: [Tickish Id] -> CoreExpr -> CoreExpr
mkOptTickBox = flip (foldr Tick)
mkBinaryTickBox :: Int -> Int -> CoreExpr -> DsM CoreExpr
mkBinaryTickBox ixT ixF e = do
uq <- newUnique
this_mod <- getModule
let bndr1 = mkSysLocal (fsLit "t1") uq boolTy
let
falseBox = Tick (HpcTick this_mod ixF) (Var falseDataConId)
trueBox = Tick (HpcTick this_mod ixT) (Var trueDataConId)
--
return $ Case e bndr1 boolTy
[ (DataAlt falseDataCon, [], falseBox)
, (DataAlt trueDataCon, [], trueBox)
]
-- *******************************************************************
-- | Remove any bang from a pattern and say if it is a strict bind,
-- also make irrefutable patterns ordinary patterns if -XStrict.
--
-- Examples:
-- ~pat => False, pat -- when -XStrict
-- -- even if pat = ~pat'
-- ~pat => False, ~pat -- without -XStrict
-- ~(~pat) => False, ~pat -- when -XStrict
-- pat => True, pat -- when -XStrict
-- !pat => True, pat -- always
decideBangHood :: DynFlags
-> LPat id -- ^ Original pattern
-> LPat id -- Pattern with bang if necessary
decideBangHood dflags lpat
= go lpat
where
xstrict = xopt LangExt.Strict dflags
go lp@(L l p)
= case p of
ParPat p -> L l (ParPat (go p))
LazyPat lp' | xstrict -> lp'
BangPat _ -> lp
_ | xstrict -> L l (BangPat lp)
| otherwise -> lp
|
tjakway/ghcjvm
|
compiler/deSugar/DsUtils.hs
|
Haskell
|
bsd-3-clause
| 35,951
|
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeOperators #-}
module T14038 where
import Data.Kind (Type)
data family Sing (a :: k)
data instance Sing (z :: [a]) where
SNil :: Sing '[]
SCons :: Sing x -> Sing xs -> Sing (x:xs)
data TyFun :: Type -> Type -> Type
type a ~> b = TyFun a b -> Type
infixr 0 ~>
type family Apply (f :: k1 ~> k2) (x :: k1) :: k2
type a @@ b = Apply a b
infixl 9 @@
data FunArrow = (:->) -- ^ '(->)'
| (:~>) -- ^ '(~>)'
class FunType (arr :: FunArrow) where
type Fun (k1 :: Type) arr (k2 :: Type) :: Type
class FunType arr => AppType (arr :: FunArrow) where
type App k1 arr k2 (f :: Fun k1 arr k2) (x :: k1) :: k2
type FunApp arr = (FunType arr, AppType arr)
instance FunType (:->) where
type Fun k1 (:->) k2 = k1 -> k2
instance AppType (:->) where
type App k1 (:->) k2 (f :: k1 -> k2) x = f x
instance FunType (:~>) where
type Fun k1 (:~>) k2 = k1 ~> k2
instance AppType (:~>) where
type App k1 (:~>) k2 (f :: k1 ~> k2) x = f @@ x
infixr 0 -?>
type (-?>) (k1 :: Type) (k2 :: Type) (arr :: FunArrow) = Fun k1 arr k2
elimList :: forall (a :: Type) (p :: [a] -> Type) (l :: [a]).
Sing l
-> p '[]
-> (forall (x :: a) (xs :: [a]). Sing x -> Sing xs -> p xs -> p (x:xs))
-> p l
elimList = elimListPoly @(:->)
elimListTyFun :: forall (a :: Type) (p :: [a] ~> Type) (l :: [a]).
Sing l
-> p @@ '[]
-> (forall (x :: a) (xs :: [a]). Sing x -> Sing xs -> p @@ xs -> p @@ (x:xs))
-> p @@ l
elimListTyFun = elimListPoly @(:~>) @_ @p
elimListPoly :: forall (arr :: FunArrow) (a :: Type) (p :: ([a] -?> Type) arr) (l :: [a]).
FunApp arr
=> Sing l
-> App [a] arr Type p '[]
-> (forall (x :: a) (xs :: [a]). Sing x -> Sing xs -> App [a] arr Type p xs -> App [a] arr Type p (x:xs))
-> App [a] arr Type p l
elimListPoly SNil pNil _ = pNil
elimListPoly (SCons x (xs :: Sing xs)) pNil pCons = pCons x xs (elimListPoly @arr @a @p @xs xs pNil pCons)
|
sdiehl/ghc
|
testsuite/tests/dependent/should_compile/T14038.hs
|
Haskell
|
bsd-3-clause
| 2,379
|
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeApplications #-}
--------------- The original bug report --------------
--
-- See T12734a for a smaller version
module T12734 where
import Prelude
import Control.Applicative
import Control.Monad.Fix
import Control.Monad.Trans.Identity
import Control.Monad.Trans.Class
import Control.Monad.IO.Class
data A
data B
data Net
data Type
data Layer4 t l
data TermStore
-- Helpers: Stack
data Stack layers (t :: * -> *) where
SLayer :: t l -> Stack ls t -> Stack (l ': ls) t
SNull :: Stack '[] t
instance ( Constructor m (t l)
, Constructor m (Stack ls t)) => Constructor m (Stack (l ': ls) t)
instance Monad m => Constructor m (Stack '[] t)
-- Helpers: Expr
newtype Expr t layers = Expr (TermStack t layers)
type TermStack t layers = Stack layers (Layer4 (Expr t layers))
-- Helpers: Funny typeclass
class Monad m => Constructor m t
instance ( Monad m, expr ~ Expr t layers, Constructor m (TermStack t layers)
) => Constructor m (Layer4 expr Type)
-- HERE IS A FUNNY BEHAVIOR: the commented line raises context reduction stack overflow
test_gr :: ( Constructor m (TermStack t layers), Inferable A layers m, Inferable B t m
, bind ~ Expr t layers
-- ) => m (Expr t layers)
) => m bind
test_gr = undefined
-- Explicit information about a type which could be inferred
class Monad m => Inferable (cls :: *) (t :: k) m | cls m -> t
newtype KnownTypex (cls :: *) (t :: k) (m :: * -> *) (a :: *) = KnownTypex (IdentityT m a) deriving (Show, Functor, Monad, MonadIO, MonadFix, MonadTrans, Applicative, Alternative)
instance {-# OVERLAPPABLE #-} (t ~ t', Monad m) => Inferable cls t (KnownTypex cls t' m)
instance {-# OVERLAPPABLE #-} (Inferable cls t n, MonadTrans m, Monad (m n)) => Inferable cls t (m n)
runInferenceTx :: forall cls t m a. KnownTypex cls t m a -> m a
runInferenceTx = undefined
-- running it
test_ghc_err :: (MonadIO m, MonadFix m)
=> m (Expr Net '[Type])
test_ghc_err = runInferenceTx @B @Net
$ runInferenceTx @A @'[Type]
$ (test_gr)
|
ezyang/ghc
|
testsuite/tests/typecheck/should_compile/T12734.hs
|
Haskell
|
bsd-3-clause
| 2,628
|
{-# LANGUAGE GADTs, RankNTypes #-}
-- Test pattern bindings, existentials, and higher rank
module T12427a where
data T where
T1 :: a -> ((forall b. [b]->[b]) -> Int) -> T
T2 :: ((forall b. [b]->[b]) -> Int) -> T
-- Inference
-- Worked in 7.10 (probably wrongly)
-- Failed in 8.0.1
-- Fails in 8.2 because v is polymorphic
-- and the T1 pattern match binds existentials,
-- and hence bumps levels
h11 y = case y of T1 _ v -> v
-- Worked in 7.10 (probably wrongly)
-- Failed in 8.0.1
-- Succeeds in 8.2 because the pattern match has
-- no existentials, so it doesn't matter than
-- v is polymorphic
h12 y = case y of T2 v -> v
-- Inference
-- Same results as for h11 and h12 resp
T1 _ x1 = undefined
T2 x2 = undefined
-- Works in all versions
h2 :: T -> (forall b. [b] -> [b]) -> Int
h2 y = case y of T1 _ v -> v
-- Checking
-- Fails in 7.10 (head exploded)
-- Fails in 8.0.1 (ditto)
-- Succeeds in 8.2
x3 :: (forall a. a->a) -> Int
T1 _ x3 = undefined
|
ezyang/ghc
|
testsuite/tests/typecheck/should_compile/T12427a.hs
|
Haskell
|
bsd-3-clause
| 976
|
-- -----------------------------------------------------------------------------
--
-- (c) The University of Glasgow 2012
--
-- Monadic streams
--
-- -----------------------------------------------------------------------------
{-# LANGUAGE CPP #-}
module Stream (
Stream(..), yield, liftIO,
collect, fromList,
Stream.map, Stream.mapM, Stream.mapAccumL
) where
import Control.Monad
#if __GLASGOW_HASKELL__ < 709
import Control.Applicative
#endif
-- |
-- @Stream m a b@ is a computation in some Monad @m@ that delivers a sequence
-- of elements of type @a@ followed by a result of type @b@.
--
-- More concretely, a value of type @Stream m a b@ can be run using @runStream@
-- in the Monad @m@, and it delivers either
--
-- * the final result: @Left b@, or
-- * @Right (a,str)@, where @a@ is the next element in the stream, and @str@
-- is a computation to get the rest of the stream.
--
-- Stream is itself a Monad, and provides an operation 'yield' that
-- produces a new element of the stream. This makes it convenient to turn
-- existing monadic computations into streams.
--
-- The idea is that Stream is useful for making a monadic computation
-- that produces values from time to time. This can be used for
-- knitting together two complex monadic operations, so that the
-- producer does not have to produce all its values before the
-- consumer starts consuming them. We make the producer into a
-- Stream, and the consumer pulls on the stream each time it wants a
-- new value.
--
newtype Stream m a b = Stream { runStream :: m (Either b (a, Stream m a b)) }
instance Monad f => Functor (Stream f a) where
fmap = liftM
instance Monad m => Applicative (Stream m a) where
pure = return
(<*>) = ap
instance Monad m => Monad (Stream m a) where
return a = Stream (return (Left a))
Stream m >>= k = Stream $ do
r <- m
case r of
Left b -> runStream (k b)
Right (a,str) -> return (Right (a, str >>= k))
yield :: Monad m => a -> Stream m a ()
yield a = Stream (return (Right (a, return ())))
liftIO :: IO a -> Stream IO b a
liftIO io = Stream $ io >>= return . Left
-- | Turn a Stream into an ordinary list, by demanding all the elements.
collect :: Monad m => Stream m a () -> m [a]
collect str = go str []
where
go str acc = do
r <- runStream str
case r of
Left () -> return (reverse acc)
Right (a, str') -> go str' (a:acc)
-- | Turn a list into a 'Stream', by yielding each element in turn.
fromList :: Monad m => [a] -> Stream m a ()
fromList = mapM_ yield
-- | Apply a function to each element of a 'Stream', lazily
map :: Monad m => (a -> b) -> Stream m a x -> Stream m b x
map f str = Stream $ do
r <- runStream str
case r of
Left x -> return (Left x)
Right (a, str') -> return (Right (f a, Stream.map f str'))
-- | Apply a monadic operation to each element of a 'Stream', lazily
mapM :: Monad m => (a -> m b) -> Stream m a x -> Stream m b x
mapM f str = Stream $ do
r <- runStream str
case r of
Left x -> return (Left x)
Right (a, str') -> do
b <- f a
return (Right (b, Stream.mapM f str'))
-- | analog of the list-based 'mapAccumL' on Streams. This is a simple
-- way to map over a Stream while carrying some state around.
mapAccumL :: Monad m => (c -> a -> m (c,b)) -> c -> Stream m a ()
-> Stream m b c
mapAccumL f c str = Stream $ do
r <- runStream str
case r of
Left () -> return (Left c)
Right (a, str') -> do
(c',b) <- f c a
return (Right (b, mapAccumL f c' str'))
|
urbanslug/ghc
|
compiler/utils/Stream.hs
|
Haskell
|
bsd-3-clause
| 3,608
|
-- Test grouping with both a using and a by clause
{-# OPTIONS_GHC -XMonadComprehensions -XTransformListComp #-}
module Main where
import Data.List(groupBy)
import GHC.Exts(the)
groupRuns :: Eq b => (a -> b) -> [a] -> [[a]]
groupRuns f = groupBy (\x y -> f x == f y)
main = putStrLn (show output)
where
output = [ (the x, product y)
| x <- ([1, 1, 1, 2, 2, 1, 3])
, y <- [4..6]
, then group by x using groupRuns ]
|
siddhanathan/ghc
|
testsuite/tests/deSugar/should_run/mc06.hs
|
Haskell
|
bsd-3-clause
| 464
|
{-# LANGUAGE PackageImports #-}
module Rocketfuel.Input (
keyIsPressed,
readMouse,
Click (..),
MouseStatus (..),
updateCommand
) where
import "GLFW-b" Graphics.UI.GLFW as GLFW
import Control.Monad
import Rocketfuel.Types
type Coords = (Integer, Integer)
data MouseStatus = Clicked | Released
data Click = Click { _xy :: (Double, Double),
_status :: MouseStatus }
-- Main updater
updateCommand :: Click -> Maybe Command -> Maybe Command
updateCommand click comm = readClick click . cleanCommand $ comm
where
cleanCommand :: Maybe Command -> Maybe Command
cleanCommand (Just (DragAndDrop (Just _) (Just _))) = Nothing
cleanCommand other = other
readClick :: Click -> Maybe Command -> Maybe Command
readClick (Click (x, y) status) com =
if legit coords then updateCommand' status
else undragIfNeeded status
where
coords = cellFromCoord x y
updateCommand' :: MouseStatus -> Maybe Command
updateCommand' Clicked = dragIfNeeded coords com
updateCommand' Released = dropIfNeeded coords com
undragIfNeeded Released = Nothing
undragIfNeeded Clicked = com
-- Signal input management
--
readMouse :: Window -> (Click -> IO b) -> IO b
readMouse window sink = do
pollEvents
mousePos <- getCursorPos window
mouseIsPressed <- checkMouseButtonStatus window MouseButtonState'Pressed
if mouseIsPressed then sink $ Click mousePos Clicked
else sink $ Click mousePos Released
checkMouseButtonStatus :: Window -> MouseButtonState -> IO Bool
checkMouseButtonStatus win st = liftM (st ==) (getMouseButton win MouseButton'1)
keyIsPressed :: Window -> Key -> IO Bool
keyIsPressed win key = isPress `fmap` GLFW.getKey win key
isPress :: KeyState -> Bool
isPress KeyState'Pressed = True
isPress KeyState'Repeating = True
isPress _ = False
-- Logic for drag'n'drop
-- Called when the mouse is down.
-- If there is no given tile being dragged,
-- given a mouse input expressed in x and y,
-- check if it fits in the grid; if so, start
-- dragging this tile by modifying GameContext.
dragIfNeeded :: Coords -> Maybe Command -> Maybe Command
dragIfNeeded coords Nothing = Just $ DragAndDrop (Just coords) Nothing
dragIfNeeded _ c = c
dropIfNeeded :: Coords -> Maybe Command -> Maybe Command
dropIfNeeded coords (Just (DragAndDrop (Just p) Nothing))
= Just $ DragAndDrop (Just p) (Just coords)
dropIfNeeded _ c = c
-- Given a x, y index, check it can be a position on the grid.
legit :: (Num a, Ord a) => (a, a) -> Bool
legit (x, y) = x < 8 && x >= 0 && y < 8 && y >= 0
-- Given a global mouse input expressed in gloss viewport,
-- convert it to a 0,0 coord system on the grid.
cellFromCoord :: Double -> Double -> Coords
cellFromCoord x y = (floor (x / 32.0), abs $ floor (y / 32.0))
|
Raveline/Rocketfuel
|
src/Rocketfuel/Input.hs
|
Haskell
|
mit
| 2,938
|
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}
module Impl.TVar1 where
import Control.Concurrent.STM
import Control.Monad.IO.Class
import Control.Monad.Trans.Either
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as Map
import Data.Proxy
import Data.Time
import Network.Wai.Handler.Warp (run)
import Servant.API
import Servant.Server
import API
data Environment
= Environment
{ serverFortuneMap :: TVar (HashMap Email (TVar Fortune))
}
runApp :: IO ()
runApp = do
env <- createEnvironment
run 8080 $ serve (Proxy :: Proxy API)
( createPlayer env
:<|> tradeFortunes env)
createEnvironment :: IO Environment
createEnvironment = do
fm <- newTVarIO Map.empty
return $ Environment fm
createPlayer :: Environment -> NewPlayer -> EitherT ServantErr IO ()
createPlayer env player = do
now <- liftIO getCurrentTime
liftIO $ atomically $ do
f <- newTVar (Fortune (npFortune player) now)
m <- readTVar (serverFortuneMap env)
let m' = Map.insert (npEmail player) f m
writeTVar (serverFortuneMap env) m'
tradeFortunes :: Environment -> Email -> Email -> EitherT ServantErr IO FortunePair
tradeFortunes env from to = do
res <- liftIO $ atomically $ do
m <- readTVar (serverFortuneMap env)
let mgivenv = Map.lookup from m
mreceivedv = Map.lookup to m
case (mgivenv, mreceivedv) of
(Just givenv, Just receivedv) -> do
given <- readTVar givenv
received <- readTVar receivedv
writeTVar givenv received
writeTVar receivedv given
return $ Just $ FortunePair given received
_ -> return Nothing
case res of
Just v -> return v
Nothing -> left err404
|
AndrewRademacher/whimsy
|
src/Impl/TVar1.hs
|
Haskell
|
mit
| 2,001
|
module Main where
import Control.Monad (forever)
import Data.Char (toLower)
import Data.List (intersperse, nub)
import Data.Maybe (isJust)
import System.Exit (exitSuccess)
import System.Random (randomRIO)
main :: IO ()
main = do
word <- randomWord'
let puzzle = freshPuzzle (fmap toLower word)
runGame puzzle
type WordList = [String]
minWordLength :: Int
minWordLength = 5
maxWordLength :: Int
maxWordLength = 9
allWords :: IO WordList
allWords = do
dict <- readFile "data/dict.txt"
return (lines dict)
gameWords :: IO WordList
gameWords = do
words <- allWords
return (filter gameLength words)
where
gameLength word =
let l = length word
in l > minWordLength && l < maxWordLength
randomWord :: WordList -> IO String
randomWord words = do
randomIndex <- randomRIO (0, length words)
return $ words !! randomIndex
randomWord' :: IO String
randomWord' = gameWords >>= randomWord
data Puzzle = Puzzle String [Maybe Char] [Char]
instance Show Puzzle where
show (Puzzle _ discovered guessed) =
(intersperse ' ' $ fmap renderPuzzleChar discovered)
++ " Guessed so far: "
++ guessed
freshPuzzle :: String -> Puzzle
freshPuzzle word = Puzzle word (map (const Nothing) word) []
charInWord :: Puzzle -> Char -> Bool
charInWord (Puzzle word _ _) c =
c `elem` word
alreadyGuessed :: Puzzle -> Char -> Bool
alreadyGuessed (Puzzle _ _ guessed) c =
c `elem` guessed
renderPuzzleChar :: Maybe Char -> Char
renderPuzzleChar Nothing = '_'
renderPuzzleChar (Just c) = c
fillInCharacter :: Puzzle -> Char -> Puzzle
fillInCharacter (Puzzle word filledInSoFar guessed) c =
Puzzle word newFilledInSoFar (c : guessed)
where
zipper :: Char -> Char -> Maybe Char -> Maybe Char
zipper guessed wordChar guessChar =
if wordChar == guessed
then Just wordChar
else guessChar
newFilledInSoFar =
zipWith (zipper c) word filledInSoFar
handleGuess :: Puzzle -> Char -> IO Puzzle
handleGuess puzzle guess = do
putStrLn $ "Your guess was: " ++ [guess]
case (charInWord puzzle guess, alreadyGuessed puzzle guess) of
(_, True) -> do
putStrLn "You already guessed that letter."
return puzzle
(True, _) -> do
putStrLn "This character was in the word."
return (fillInCharacter puzzle guess)
(False, _) -> do
putStrLn "This character was not in the word."
putStrLn ""
return (fillInCharacter puzzle guess)
gameOver :: Puzzle -> IO ()
gameOver p@(Puzzle word _ _) =
if tooManyGuesses p then
do
putStrLn "You lost!"
putStrLn $ "The word was: " ++ word
exitSuccess
else return ()
tooManyGuesses :: Puzzle -> Bool
tooManyGuesses (Puzzle _ filledInSoFar guessed) =
length guessed - length uniqueFilledIn > 7
where
filledIn = filter isJust filledInSoFar
uniqueFilledIn = nub filledIn
gameWin :: Puzzle -> IO ()
gameWin (Puzzle _ filledInSoFar _) =
if all isJust filledInSoFar then
do
putStrLn "You win!"
exitSuccess
else return ()
runGame :: Puzzle -> IO ()
runGame puzzle = forever $ do
gameOver puzzle
gameWin puzzle
putStrLn $ "Current puzzle is: " ++ show puzzle
putStr "Guess a letter: "
guess <- getLine
case guess of
[c] -> handleGuess puzzle c >>= runGame
_ -> putStrLn "Your guess must be a single character."
|
mikegehard/haskellBookExercises
|
hangman/src/Main.hs
|
Haskell
|
mit
| 3,624
|
module Main where
import Prelude hiding (catch)
import Control.Exception ( AsyncException(..), catch )
import Control.Monad.Error
import Data.Version
import Data.List
import System.IO
import System.Environment
import System.Directory (getHomeDirectory)
import System.FilePath ((</>))
import System.Console.Haskeline hiding (handle, catch, throwTo)
import System.Console.GetOpt
import System.Exit (ExitCode (..), exitWith, exitFailure)
import Language.Egison
import Language.Egison.Util
import qualified Paths_egison_tutorial as P
main :: IO ()
main = do args <- getArgs
let (actions, nonOpts, _) = getOpt Permute options args
let opts = foldl (flip id) defaultOptions actions
case opts of
Options {optShowSections = True} -> putStrLn $ show tutorial
Options {optSection = Just sn, optSubSection = Just ssn} -> do
let sn' = (read sn) :: Int
let ssn' = (read ssn) :: Int
let ret = case tutorial of
Tutorial ss ->
if 0 < sn' && sn' <= length ss
then case nth sn' ss of
Section _ cs ->
if 0 < ssn' && ssn' <= length cs
then showContent $ nth ssn' cs
else "error: content out of range"
else "error: section out of range"
putStrLn ret
Options {optShowHelp = True} -> printHelp
Options {optShowVersion = True} -> printVersionNumber
Options {optPrompt = prompt} -> do
env <- initialEnv
case nonOpts of
[] -> showBanner >> repl env prompt
_ -> printHelp
data Options = Options {
optShowVersion :: Bool,
optShowHelp :: Bool,
optPrompt :: String,
optShowSections :: Bool,
optSection :: Maybe String,
optSubSection :: Maybe String
}
defaultOptions :: Options
defaultOptions = Options {
optShowVersion = False,
optShowHelp = False,
optPrompt = "> ",
optShowSections = False,
optSection = Nothing,
optSubSection = Nothing
}
options :: [OptDescr (Options -> Options)]
options = [
Option ['v', 'V'] ["version"]
(NoArg (\opts -> opts {optShowVersion = True}))
"show version number",
Option ['h', '?'] ["help"]
(NoArg (\opts -> opts {optShowHelp = True}))
"show usage information",
Option ['p'] ["prompt"]
(ReqArg (\prompt opts -> opts {optPrompt = prompt})
"String")
"set prompt string",
Option ['l'] ["list"]
(NoArg (\opts -> opts {optShowSections = True}))
"show section list",
Option ['s'] ["section"]
(ReqArg (\sn opts -> opts {optSection = Just sn})
"String")
"set section number",
Option ['c'] ["subsection"]
(ReqArg (\ssn opts -> opts {optSubSection = Just ssn})
"String")
"set subsection number"
]
printHelp :: IO ()
printHelp = do
putStrLn "Usage: egison-tutorial [options]"
putStrLn ""
putStrLn "Options:"
putStrLn " --help Display this information"
putStrLn " --version Display egison version information"
putStrLn " --prompt string Set prompt of the interpreter"
putStrLn ""
exitWith ExitSuccess
printVersionNumber :: IO ()
printVersionNumber = do
putStrLn $ showVersion P.version
exitWith ExitSuccess
showBanner :: IO ()
showBanner = do
putStrLn $ "Egison Tutorial Version " ++ showVersion P.version ++ " (C) 2013-2014 Satoshi Egi"
putStrLn $ "Welcome to Egison Tutorial!"
putStrLn $ "** Information **"
putStrLn $ "We can use a \'Tab\' key to complete keywords on the interpreter."
putStrLn $ "If we type a \'Tab\' key after a closed parenthesis, the next closed parenthesis will be completed."
putStrLn $ "*****************"
showFinishMessage :: IO ()
showFinishMessage = do
putStrLn $ "You have finished this section."
putStrLn $ "Thank you!"
showByebyeMessage :: IO ()
showByebyeMessage = do
putStrLn $ "Leaving Egison Tutorial.\nByebye."
yesOrNo :: String -> IO Bool
yesOrNo question = do
putStr $ question
putStr $ " (Y/n): "
hFlush stdout
input <- getLine
case input of
[] -> return True
('y':_) -> return True
('Y':_) -> return True
('n':_) -> return False
('N':_) -> return False
_ -> yesOrNo question
nth n = head . drop (n - 1)
selectSection :: Tutorial -> IO Section
selectSection tutorial@(Tutorial sections) = do
putStrLn $ take 30 $ repeat '='
putStrLn $ "List of sections in the tutorial."
putStrLn $ show tutorial
putStrLn $ take 30 $ repeat '='
putStrLn $ "Choose a section to learn."
n <- getNumber (length sections)
return $ nth n sections
getNumber :: Int -> IO Int
getNumber n = do
putStr $ "(1-" ++ show n ++ "): "
hFlush stdout
input <- getLine
case input of
('1':_) -> return 1
('2':_) -> return 2
('3':_) -> return 3
('4':_) -> return 4
-- ('5':_) -> return 5
-- ('6':_) -> return 6
-- ('7':_) -> return 7
-- ('9':_) -> return 9
_ -> do
putStrLn "Invalid input!"
getNumber n
repl :: Env -> String -> IO ()
repl env prompt = do
section <- selectSection tutorial
case section of
Section _ cs -> loop env cs True
where
settings :: MonadIO m => FilePath -> Settings m
settings home = setComplete completeEgison $ defaultSettings { historyFile = Just (home </> ".egison_history") }
loop :: Env -> [Content] -> Bool -> IO ()
loop env [] _ = do
-- liftIO $ showFinishMessage
liftIO $ repl env prompt
loop env (content:contents) b = (do
if b
then liftIO $ putStrLn $ show content
else return ()
home <- getHomeDirectory
input <- liftIO $ runInputT (settings home) $ getEgisonExprOrNewLine prompt
case input of
Left Nothing -> do
b <- yesOrNo "Do you want to quit?"
if b
then return ()
else do
b <- yesOrNo "Do you want to procced next?"
if b
then loop env contents True
else loop env (content:contents) False
Left (Just "") -> do
b <- yesOrNo "Do you want to procced next?"
if b
then loop env contents True
else loop env (content:contents) False
Right (topExpr, _) -> do
result <- liftIO $ runEgisonTopExpr env topExpr
case result of
Left err -> do
liftIO $ putStrLn $ show err
loop env (content:contents) False
Right env' -> loop env' (content:contents) False)
`catch`
(\e -> case e of
UserInterrupt -> putStrLn "" >> loop env (content:contents) False
StackOverflow -> putStrLn "Stack over flow!" >> loop env (content:contents) False
HeapOverflow -> putStrLn "Heap over flow!" >> loop env (content:contents) False
_ -> putStrLn "error!" >> loop env (content:contents) False
)
data Tutorial = Tutorial [Section]
-- |title and contents
data Section = Section String [Content]
-- |explanation, examples, and exercises
data Content = Content String [String] [String]
instance Show Tutorial where
show = showTutorial
instance Show Section where
show = showSection
instance Show Content where
show = showContent
showTutorial :: Tutorial -> String
showTutorial (Tutorial sections) =
let n = length sections in
intercalate "\n" $ map (\(n, section) -> show n ++ ": " ++ show section) $ zip [1..n] sections
showSection :: Section -> String
showSection (Section title _) = title
showContent :: Content -> String
showContent (Content msg examples exercises) =
"====================\n" ++
msg ++ "\n" ++
(case examples of
[] -> ""
_ -> "\nExamples:\n" ++ (intercalate "\n" (map (\example -> " " ++ example) examples)) ++ "\n") ++
(case exercises of
[] -> ""
_ -> "\nExercises:\n" ++ (intercalate "\n" (map (\exercise -> " " ++ exercise) exercises)) ++ "\n") ++
"===================="
tutorial :: Tutorial
tutorial = Tutorial
[Section "Calculate numbers (10 minutes)"
[
Content "We can do arithmetic operations with '+', '-', '*', '/', 'modulo' and 'power'."
["(+ 1 2)", "(- 30 15)", "(* 10 20)", "(/ 20 5)", "(modulo 17 4)", "(power 2 10)"]
[],
Content "We can write nested expressions."
["(+ (* 10 20) 2)", "(/ (* 10 20) (+ 10 20))"]
["Try to calculate '(100 - 1) * (100 + 1)'."],
Content "We are supporting rational numbers."
["(+ 2/3 1/5)", "(/ 42 84)"]
[],
Content "We are supporting floats, too."
["(+ 10.2 1.3)", "(* 10.2 1.3)"]
[],
Content "We can convert a rational number to a float number with 'rtof'."
["(rtof 1/5)", "(rtof 1/100)"]
[],
Content "We can handle collections of numbers.\nWe construct collections with '{}'."
["{}", "{10}", "{1 2 3 4 5}"]
[],
Content "We can decompose a collection using the 'car' and 'cdr' function."
["(car {1 2 3 4 5})", "(cdr {1 2 3 4 5})", "(car (cdr {1 2 3 4 5}))"]
["Try to extract the third element of the collection '{1 2 3 4 5}' with 'car' and 'cdr'."],
Content "With the 'take' function, we can extract a head part of a collection.'."
["(take 0 {1 2 3 4 5})", "(take 3 {1 2 3 4 5})"]
[],
Content "We can handle infinite lists.\nFor example, 'nats' and 'primes' are an infinite list that contains all natural numbers and prime numbers respectively.\nTry to extract a head part from them."
["(take 10 nats)", "(take 30 nats)", "(take 10 primes)", "(take 30 primes)"]
["What is the 100th prime number."],
Content "We can create a \"partial\" function using '$' as an argument."
["((* $ 2) 10)", "((modulo $ 3) 10)"]
[],
Content "With the 'map' function, we can operate each element of the collection at once."
["(take 100 (map (* $ 2) nats))", "(take 100 (map (modulo $ 3) nats))"]
[],
Content "With the 'foldl' function, we can gather together all elements of the collection using an operator you like."
["(foldl + 0 {1 2 3 4 5})", "(foldl * 1 {1 2 3 4 5})"]
["Try to get the sum of from 1 to 100?"],
Content "Try to create a sequence of numbers '{1 1/2 1/3 1/4 ... 1/100}'."
[]
[],
Content "Try to calculate '1 + 1/2 + 1/3 + 1/4 + ... + 1/100'.\nRemember that we can convert a rational number to a float number with 'rtof'."
["(rtof 2/3)"]
[],
Content "Try to calculate '1 + (1/2)^2 + (1/3)^2 + (1/4)^2 + ... + (1/100)^2'.\nIn fact, '1 + (1/2)^2 + (1/3)^2 + (1/4)^2 + ...' converges to '(/ (power pi 2) 6)'."
[]
[],
Content "This is the end of this section.\nPlease play freely or proceed to the next section.\nThank you for enjoying our tutorial!"
[]
[]
],
Section "Basics of functional programming (10 minutes)"
[
Content "We can bind a value to a variable with a 'define' expression.\nWe can easily get the value we bound to a variable."
["(define $x 10)", "x", "(define $y (+ 1 x))", "y"]
[],
Content "We support recursive definitions. It enables us to define an collection with infinite elements."
["(define $ones {1 @ones})", "(take 100 ones)", "(define $nats {1 @(map (+ $ 1) nats)})", "(take 100 nats)", "(define $odds {1 @(map (+ $ 2) odds)})", "(take 100 odds)"]
["Try to define the infinite list of even numbers that is like {2 4 6 8 10 ...}."],
Content "We can create a function with a 'lambda' expression. Let's define functions and test them."
["(define $increment (lambda [$x] (+ x 1)))", "(increment 10)", "(define $multiply (lambda [$x $y] (* x y)))", "(multiply 10 20)", "(define $sum (lambda [$n] (foldl + 0 (take n nats))))", "(sum 10)"]
["Try to define a 'fact' function, which obtains an natural number 'n' and returns 'n * (n - 1) * ... * 2 * 1'."],
Content "We can compare numbers using functions that return '#t' or '#f'.\n'#t' means the true.\n'#f' means the false.\nFunctions that return '#t' or '#f' are called \"predicates\"."
["(eq? 1 1)", "(gt? 1 1)", "(lt? 1 1)", "(gte? 1 1)", "(lte? 1 1)"]
[],
Content "With the 'while' function, we can extract all head elements that satisfy the predicate.\n'primes' is a infinite list that contains all prime numbers."
["(while (lt? $ 100) primes)", "(while (lt? $ 1000) primes)"]
[],
Content "With the 'filter' function, we can extract all elements that satisfy the predicate."
["(take 100 (filter even? nats))", "(take 100 (filter prime? nats))", "(take 100 (filter (lambda [$p] (eq? (modulo p 4) 1)) primes))"]
["Try to enumerate the first 100 primes that are congruent to 3 modulo 4."],
Content "We combine numbers using '[]'.\nThese things are called 'tuples'."
["[1 2]", "[1 2 3]"]
[],
Content "Note that a tuple that consists of only one element is equal with that element itself."
["[1]", "[[[1]]]"]
[],
Content "With the 'zip' function, we can combine two lists as follow."
["(take 100 (zip nats nats))", "(take 100 (zip primes primes))"]
["Try to generate the prime table that is like '{[1 2] [2 3] [3 5] [4 7] [5 11] ...}'"],
Content "Try to create a fibonacci sequence that is like '{1 1 2 3 5 8 13 21 34 55 ...}'.\n\nHint:\n Replace '???' in the following expression to a proper function.\n (define $fibs {1 1 @(map ??? (zip fibs (cdr fibs)))})"
[]
[],
Content "This is the end of this section.\nPlease play freely or proceed to the next section.\nThank you for enjoying our tutorial!"
[]
[]
],
Section "Basics of pattern-matching (10 minutes)"
[
Content "Let's try pattern-matching against a collection.\nThe 'join' pattern divides a collection into two collections.\nPlease note that the 'match-all' expression enumerates all results of pattern-matching."
["(match-all {1 2 3} (list integer) [<join $hs $ts> [hs ts]])",
"(match-all {1 2 3 4 5} (list integer) [<join $hs $ts> [hs ts]])"]
[],
Content "Try another pattern-constructor 'cons'.\nThe 'cons' pattern divides a collection into the head element and the rest collection.\n"
["(match-all {1 2 3} (list integer) [<cons $x $xs> [x xs]])",
"(match-all {1 2 3 4 5} (list integer) [<cons $x $xs> [x xs]])"]
[],
Content "'_' is a wildcard and matches with any objects."
["(match-all {1 2 3} (list integer) [<cons $x _> x])",
"(match-all {1 2 3 4 5} (list integer) [<join $hs _> hs])"]
[],
Content "We can write non-linear patterns.\nNon-linear pattern is a pattern that allows multiple occurrence of same variables in a pattern.\nPatterns that begins with ',' matches the object when it is equal with the expression after ','."
["(match-all {1 1 2 3 3 2} (list integer) [<join _ <cons $x <cons ,x _>>> x])",
"(match-all {1 1 2 3 3 2} (list integer) [<join _ <cons $x <cons ,(+ x 1) _>>> x])"]
[],
Content "We can pattern-match against infinite collections.\nWe can enumerate twin primes using pattern-matching as follow."
["(take 10 (match-all primes (list integer) [<join _ <cons $p <cons ,(+ p 2) _>>> [p (+ p 2)]]))"]
["What is the 100th twin prime?"],
Content "Try to enumerate the first 10 prime pairs whose form is (p, p+6) like '{{[5 11] [7 13] [11 17] [13 19] [17 23] ...}'."
[]
[],
Content "A pattern that has '^' ahead of which is called a not-pattern.\nA not-pattern matches when the target does not match against the pattern."
["(match-all {1 1 2 2 3 4 4 5} (list integer) [<join _ <cons $x <cons ,x _>>> x])",
"(match-all {1 1 2 2 3 4 4 5} (list integer) [<join _ <cons $x <cons ^,x _>>> x])"]
[],
Content "A pattern whose form is '(& p1 p2 ...)' is called an and-pattern.\nAn and-pattern is a pattern that matches the object, if and only if all of the patterns are matched.\nAnd-pattern is used like an as-pattern in the following sample."
["(match-all {1 2 4 5 6 8 9} (list integer) [<join _ <cons $x <cons (& ^,(+ x 1) $y) _>>> [x y]])"]
[],
Content "A pattern whose form is '(| p1 p2 ...)' is called an or-pattern.\nAn or-pattern matches with the object, if the object matches one of given patterns.\nUsing it, We can enumerate prime triplets."
["(take 10 (match-all primes (list integer) [<join _ <cons $p <cons (& $m (| ,(+ p 2) ,(+ p 4))) <cons ,(+ p 6) _>>>> [p m (+ p 6)]]))"]
["What is the 20th prime triplet?"],
Content "Try to enumerate the first 8 prime quadruplets whose form is (p, p+2, p+6, p+8) like '{{[5 7 11 13] [11 13 17 19] ...}'."
[]
[],
Content "This is the end of this section.\nPlease play freely or proceed to the next section.\nThank you for enjoying our tutorial!"
[]
[]
],
Section "Pattern-matching against various data types (10 minutes)"
[
Content "We can also pattern-match against multisets and sets.\nWe can change the way of pattern-matching by just changing a matcher."
["(match-all {1 2 3} (list integer) [<cons $x $xs> [x xs]])",
"(match-all {1 2 3} (multiset integer) [<cons $x $xs> [x xs]])",
"(match-all {1 2 3} (set integer) [<cons $x $xs> [x xs]])"]
[],
Content "Try another pattern-constructor 'join'.\nThe 'join' pattern divides a collection into two collections."
["(match-all {1 2 3 4 5} (list integer) [<join $xs $ys> [xs ys]])",
"(match-all {1 2 3 4 5} (multiset integer) [<join $xs $ys> [xs ys]])",
"(match-all {1 2 3 4 5} (set integer) [<join $xs $ys> [xs ys]])"]
[],
Content "Try non-linear pattern-matching against multiset."
["(match-all {1 1 2 3 2} (multiset integer) [<cons $x <cons ,x _>> x])",
"(match-all {1 1 2 3 2} (multiset integer) [<cons $x <cons ,(+ x 2) _>> x])",
"(match-all {1 2 1 3 2} (multiset integer) [<cons $x ^<cons ,x _>> x])"]
[],
Content "The following samples enumerate pairs and triplets of natural numbers.\nNote that Egison really enumerates all results."
["(take 10 (match-all nats (set integer) [<cons $m <cons $n _>> [m n]]))",
"(take 10 (match-all nats (set integer) [<cons $l <cons $m <cons $n _>>> [l m n]]))"]
[],
Content "This is the end of our tutorial.\nThank you for enjoying our tutorial!\nPlease check our paper, manual and code for further reference!"
[]
[]
]
]
-- Section "Define your own functions"
-- [
-- Content "Did we think how about \"n\" combinations of the elements of the collection?\nWe already have a solution.\nWe can write a pattern that include '...' as the following demonstrations."
-- ["(match-all {1 2 3 4 5} (list integer) [(loop $i [1 3] <join _ <cons $a_i ...>> _) a])", "(match-all {1 2 3 4 5} (list integer) [(loop $i [1 4] <join _ <cons $a_i ...>> _) a])"]
-- [],
-- Content "Let's try 'if' expressions."
-- ["(if #t 1 2)", "(if #f 1 2)", "(let {[$x 10]} (if (eq? x 10) 1 2))"]
-- [],
-- Content "Using 'define' and 'if', we can write recursive functions as follow."
-- ["(define $your-take (lambda [$n $xs] (if (eq? n 0) {} {(car xs) @(your-take (- n 1) (cdr xs))})))", "(your-take 10 nats)"]
-- ["Try to write a 'your-while' function."],
-- Content "Try to write a 'your-map' function.\nWe may need 'empty?' function inside 'your-map' function."
-- ["(empty? {})", "(empty? {1 2 3})"]
-- []
-- Section "Writing scripts in Egison"
-- [
-- Content "Let's write a famous Hello world program in Egison.\nTry the following expression.\nIt is evaluated to the 'io-function'.\nTo execute an io-function, we use 'io' primitive as follow."
-- ["(io (print \"Hello, world!\"))"]
-- [],
-- Content "We can execute multiple io-functions in sequence as follow.\nThe io-functions is executed from the head."
-- ["(io (do {[(print \"a\")] [(print \"b\")] [(print \"c\")]} []))", "(io (do {[(write-string \"Type your name: \")] [(flush)] [$name (read-line)] [(print {@\"Hello, \" @name @\"!\"})]} []))"]
-- [],
-- Content "The following is a hello world program in Egison.\nTry to create a file with the following content and save it as \"hello.egi\", and execute it in the terminal as '% egison hello.egi'\n"
-- ["(define $main (lambda [$args] (print \"Hello, world!\")))"]
-- [],
-- Content "That's all. Thank you for finishing our tutorail! Did you enjoy it?\nIf you got into Egison programming. I'd like you to try Rosseta Code.\nThere are a lot of interesting problems.\n\n http://rosettacode.org/wiki/Category:Egison"
-- []
-- []
-- ]
-- ]
|
hatappo/egison-tutorial
|
Main.hs
|
Haskell
|
mit
| 20,686
|
module ProgrammingInHaskell2.Chap05Spec (spec) where
import SpecHelper
import ProgrammingInHaskell2.Chap05
spec :: Spec
spec = do
describe "5.1" $ do
it "[x^2 | x <- [1..5]]" $ do
[x^2 | x <- [1..5]] `shouldBe` [1,4,9,16,25]
describe "5.2 Guards" $ do
it "factors" $ do
factors 15 `shouldBe` [1,3,5,15]
it "prime" $ do
prime 15 `shouldBe` False
it "primes" $ do
primes 40 `shouldBe` [2,3,5,7,11,13,17,19,23,29,31,37]
it "find" $ do
find 'b' [('a', 1),('b', 2),('c', 3),('b', 4)] `shouldBe` [2,4]
describe "5.3 The zip function" $ do
it "zip ['a', 'b', 'c'] [1,2,3,4]" $ do
zip ['a','b','c'] [1,2,3,4] `shouldBe` [('a',1),('b',2),('c',3)]
it "pairs" $ do
pairs [1,2,3,4] `shouldBe` [(1,2),(2,3),(3,4)]
it "sorted xsγ―γγͺγΉγxsγζ΄εζΈγγ©γγγε€ζγγ" $ do
sorted [1,2,3,4] `shouldBe` True
sorted [1,3,2,4] `shouldBe` False
it "positions" $ do
positions 2 [1,2,3,2] `shouldBe` [1,3]
|
akimichi/haskell-labo
|
test/ProgrammingInHaskell2/Chap05Spec.hs
|
Haskell
|
mit
| 1,031
|
{-# htermination addListToFM_C :: (b -> b -> b) -> FiniteMap (Ratio Int) b -> [((Ratio Int),b)] -> FiniteMap (Ratio Int) b #-}
import FiniteMap
|
ComputationWithBoundedResources/ara-inference
|
doc/tpdb_trs/Haskell/full_haskell/FiniteMap_addListToFM_C_7.hs
|
Haskell
|
mit
| 144
|
module NES.Emulator.Debug where
import Data.Word (Word8, Word16)
import Data.Bits ((.|.))
import Debug.Trace (trace)
import Text.Printf (printf)
import NES.CPU(Flag(..))
import NES.Instruction
import NES.MonadEmulator
import NES.Emulator
import NES.EmulatorHelpers
emulateCycles :: MonadEmulator m => Int -> m ()
emulateCycles 0 = return ()
emulateCycles n = do
pc <- loadPC
registersState <- registersSnapshot
instruction <- decodeInstruction
execute instruction
traceCurrentState pc instruction registersState
emulateCycles $ n - 1
traceCurrentState :: MonadEmulator m => Word16 -> Instruction -> String -> m ()
traceCurrentState pc instruction registers = trace (printf "%04X %-20s" pc (show instruction) ++ " " ++ registers) $ return ()
registersSnapshot :: MonadEmulator m => m String
registersSnapshot = do
a <- loadA
x <- loadX
y <- loadY
status <- getStatus
sp <- loadSP
return $ printf "A:%02X X:%02X Y:%02X P:%02X SP:%02X" a x y status sp
where
getStatus :: MonadEmulator m => m Word8
getStatus = do
cf <- getFlag CF >>= (\b -> return $ if b then 0x01 else 0)
zf <- getFlag ZF >>= (\b -> return $ if b then 0x02 else 0)
idf <- getFlag IDF >>= (\b -> return $ if b then 0x04 else 0)
dmf <- getFlag DMF >>= (\b -> return $ if b then 0x08 else 0)
ovf <- getFlag OF >>= (\b -> return $ if b then 0x40 else 0)
nf <- getFlag NF >>= (\b -> return $ if b then 0x80 else 0)
return $ 0x20 .|. cf .|. zf .|. idf .|. dmf .|. ovf .|. nf
|
ksaveljev/hNES
|
NES/Emulator/Debug.hs
|
Haskell
|
mit
| 1,560
|
{-# LANGUAGE
TypeOperators
, MultiParamTypeClasses
, FlexibleInstances
, OverlappingInstances
, FlexibleContexts #-}
module Calculus.Expr where
{-
This module generalizes expressions.
-}
import Auxiliary.List
import Auxiliary.NameSource
import Data.Functor
import Control.Monad.State
-- Coproduct type definition
data (f :+: g) a = Inl (f a) | Inr (g a)
deriving Show
-- Making it to be right-assoc in order
-- to get linear search on it later
infixr 6 :+:
-- Making it to be functor in order to fold it later
instance (Functor f, Functor g) => Functor (f :+: g) where
fmap f (Inl x) = Inl $ fmap f x
fmap f (Inr x) = Inr $ fmap f x
-- Expr is a data type, which generates a recursive
-- Expr over whatever constructors are present in its
-- functor argument f. This argument must be a coproduct
-- type made of subExprs which can be found in subtrees
-- of Expr.
newtype Expr f = In { out :: f (Expr f) }
{-
In :: f (Expr f) -> Expr f
out :: Expr f -> f (Expr f)
`In` rolls one layer of Expr
`out` unrolls one layer of Expr
These functions allows to operate on
outermost constructor
-}
-- To fold Expr
foldExpr :: Functor f => (f a -> a) -> Expr f -> a
foldExpr f = f . fmap (foldExpr f) . out
{-
Here `f` is an algebra which tells us how to fold
a Expr's layer
-}
{-
Consider types
data A a = A a a
data B b = B b
data C c = C
And suppose that we want to make a Expr of type
Expr (A :+: B :+: C)
(:+:) is right-associative, so this type may be rewritten as
Expr (A :+: (B :+: C))
Signature of this type may be considered as list,
where `A` is a head and `B :+: C` is a tail. To get
head, we can use `Inl`; to get tail - `Inr`.
Having that in mind, we can write correct expression
`A (B (C)) C` to suit this type definition:
abcExpr :: Expr (A :+: B :+: C)
abcExpr = In (Inl (A
(In (Inr (Inl (B
(In (Inr (Inr C)))))))
(In (Inr (Inr C)))))
Using (:+:) to construct Expr in modular fashion
comes with overhead on constructors. It's not
wise to turn types from signature into a signature
each time by hands. It can be automated.
-}
class (Functor sub, Functor sup) => sub :<: sup where
inj :: sub a -> sup a
prj :: sup a -> Maybe (sub a)
instance Functor f => f :<: f where
inj = id
prj = Just . id
instance (Functor f, Functor g) => f :<: (f :+: g) where
inj = Inl
prj (Inl f) = Just f
prj _ = Nothing
instance (Functor f, Functor g, Functor h, f :<: g) => f :<: (h :+: g) where
inj = Inr . inj
prj (Inr g) = prj g
prj _ = Nothing
-- rolls next layer
inject :: (g :<: f) => g (Expr f) -> Expr f
inject = In . inj
-- unrolls layer
match :: (g :<: f) => Expr f -> Maybe (g (Expr f))
match (In f) = prj f
{- Rendering -}
class Render f where
render :: (NameState ns m, Render g) => f (Expr g) -> m String
instance (Render f, Render g) => Render (f :+: g) where
render (Inl f) = render f
render (Inr f) = render f
pretty :: (NameSource ns, Render f) => ns -> Expr f -> String
pretty ns f = evalState (render $ out f) ns
prettyPair ::
(NameSource ns, Render f, Render g) => ns -> (Expr f, Expr g) -> (String, String)
prettyPair ns (a, b) =
let
(aSrc, bSrc) = splitSrc ns
aString = pretty aSrc a
bString = pretty bSrc b
in (aString, bString)
renderPair ::
(NameState ns m, Render f) => (Expr f, Expr f) -> m (String, String)
renderPair (a, b) = get >>= return . flip prettyPair (a, b)
renderBinOp ::
(NameState ns m, Render f) => String -> (Expr f, Expr f) -> m String
renderBinOp op pair =
do (a, b) <- renderPair pair
return $ between "(" ")" $ between a b $ between " " " " op
showExprs :: (NameSource ns, Render f) => ns -> [Expr f] -> String
showExprs ns = showColumn . map (pretty ns)
|
wowofbob/calculus
|
Calculus/Expr.hs
|
Haskell
|
mit
| 3,924
|
module Zwerg.Data.HP
( HP
, adjustHP
, adjustMaxHP
) where
import Zwerg.Prelude
newtype HP = MkHP (Int, Int)
deriving stock Generic
deriving anyclass Binary
validHP :: (Int,Int) -> Bool
validHP (curHP, maxHP) = curHP >= 0 && curHP <= maxHP && maxHP > 0
instance ZWrapped HP (Int, Int) where
unwrap (MkHP hp) = hp
wrap intPair = if validHP intPair then Just (MkHP intPair) else Nothing
instance ZDefault HP where
zDefault = MkHP (1,1)
{-# INLINABLE adjustHP #-}
adjustHP :: (Int -> Int) -> HP -> HP
adjustHP f (MkHP (curHP, maxHP))
| newHP < 0 = MkHP (0, maxHP)
| newHP > maxHP = MkHP (maxHP, maxHP)
| otherwise = MkHP (newHP, maxHP)
where newHP = f curHP
{-# INLINABLE adjustMaxHP #-}
adjustMaxHP :: (Int -> Int) -> HP -> HP
adjustMaxHP f (MkHP (curHP, maxHP))
| newMaxHP < 0 = MkHP (1, 1)
| curHP > newMaxHP = MkHP (newMaxHP, newMaxHP)
| otherwise = MkHP (curHP, newMaxHP)
where newMaxHP = f maxHP
|
zmeadows/zwerg
|
lib/Zwerg/Data/HP.hs
|
Haskell
|
mit
| 962
|
-- file: ch03/ListADT.hs
-- From chapter 3, http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html
data List a = Cons a (List a)
| Nil
deriving (Show)
fromList (x:xs) = Cons x (fromList xs)
fromList [] = Nil
|
Sgoettschkes/learning
|
haskell/RealWorldHaskell/ch03/ListADT.hs
|
Haskell
|
mit
| 244
|
{-# LANGUAGE PatternSynonyms, ForeignFunctionInterface, JavaScriptFFI #-}
module GHCJS.DOM.JSFFI.Generated.SVGStringList
(js_clear, clear, js_initialize, initialize, js_getItem, getItem,
js_insertItemBefore, insertItemBefore, js_replaceItem, replaceItem,
js_removeItem, removeItem, js_appendItem, appendItem,
js_getNumberOfItems, getNumberOfItems, SVGStringList,
castToSVGStringList, gTypeSVGStringList)
where
import Prelude ((.), (==), (>>=), return, IO, Int, Float, Double, Bool(..), Maybe, maybe, fromIntegral, round, fmap, Show, Read, Eq, Ord)
import Data.Typeable (Typeable)
import GHCJS.Types (JSRef(..), JSString, castRef)
import GHCJS.Foreign (jsNull)
import GHCJS.Foreign.Callback (syncCallback, asyncCallback, syncCallback1, asyncCallback1, syncCallback2, asyncCallback2, OnBlocked(..))
import GHCJS.Marshal (ToJSRef(..), FromJSRef(..))
import GHCJS.Marshal.Pure (PToJSRef(..), PFromJSRef(..))
import Control.Monad.IO.Class (MonadIO(..))
import Data.Int (Int64)
import Data.Word (Word, Word64)
import GHCJS.DOM.Types
import Control.Applicative ((<$>))
import GHCJS.DOM.EventTargetClosures (EventName, unsafeEventName)
import GHCJS.DOM.Enums
foreign import javascript unsafe "$1[\"clear\"]()" js_clear ::
JSRef SVGStringList -> IO ()
-- | <https://developer.mozilla.org/en-US/docs/Web/API/SVGStringList.clear Mozilla SVGStringList.clear documentation>
clear :: (MonadIO m) => SVGStringList -> m ()
clear self = liftIO (js_clear (unSVGStringList self))
foreign import javascript unsafe "$1[\"initialize\"]($2)"
js_initialize :: JSRef SVGStringList -> JSString -> IO JSString
-- | <https://developer.mozilla.org/en-US/docs/Web/API/SVGStringList.initialize Mozilla SVGStringList.initialize documentation>
initialize ::
(MonadIO m, ToJSString item, FromJSString result) =>
SVGStringList -> item -> m result
initialize self item
= liftIO
(fromJSString <$>
(js_initialize (unSVGStringList self) (toJSString item)))
foreign import javascript unsafe "$1[\"getItem\"]($2)" js_getItem
:: JSRef SVGStringList -> Word -> IO JSString
-- | <https://developer.mozilla.org/en-US/docs/Web/API/SVGStringList.getItem Mozilla SVGStringList.getItem documentation>
getItem ::
(MonadIO m, FromJSString result) =>
SVGStringList -> Word -> m result
getItem self index
= liftIO
(fromJSString <$> (js_getItem (unSVGStringList self) index))
foreign import javascript unsafe "$1[\"insertItemBefore\"]($2, $3)"
js_insertItemBefore ::
JSRef SVGStringList -> JSString -> Word -> IO JSString
-- | <https://developer.mozilla.org/en-US/docs/Web/API/SVGStringList.insertItemBefore Mozilla SVGStringList.insertItemBefore documentation>
insertItemBefore ::
(MonadIO m, ToJSString item, FromJSString result) =>
SVGStringList -> item -> Word -> m result
insertItemBefore self item index
= liftIO
(fromJSString <$>
(js_insertItemBefore (unSVGStringList self) (toJSString item)
index))
foreign import javascript unsafe "$1[\"replaceItem\"]($2, $3)"
js_replaceItem ::
JSRef SVGStringList -> JSString -> Word -> IO JSString
-- | <https://developer.mozilla.org/en-US/docs/Web/API/SVGStringList.replaceItem Mozilla SVGStringList.replaceItem documentation>
replaceItem ::
(MonadIO m, ToJSString item, FromJSString result) =>
SVGStringList -> item -> Word -> m result
replaceItem self item index
= liftIO
(fromJSString <$>
(js_replaceItem (unSVGStringList self) (toJSString item) index))
foreign import javascript unsafe "$1[\"removeItem\"]($2)"
js_removeItem :: JSRef SVGStringList -> Word -> IO JSString
-- | <https://developer.mozilla.org/en-US/docs/Web/API/SVGStringList.removeItem Mozilla SVGStringList.removeItem documentation>
removeItem ::
(MonadIO m, FromJSString result) =>
SVGStringList -> Word -> m result
removeItem self index
= liftIO
(fromJSString <$> (js_removeItem (unSVGStringList self) index))
foreign import javascript unsafe "$1[\"appendItem\"]($2)"
js_appendItem :: JSRef SVGStringList -> JSString -> IO JSString
-- | <https://developer.mozilla.org/en-US/docs/Web/API/SVGStringList.appendItem Mozilla SVGStringList.appendItem documentation>
appendItem ::
(MonadIO m, ToJSString item, FromJSString result) =>
SVGStringList -> item -> m result
appendItem self item
= liftIO
(fromJSString <$>
(js_appendItem (unSVGStringList self) (toJSString item)))
foreign import javascript unsafe "$1[\"numberOfItems\"]"
js_getNumberOfItems :: JSRef SVGStringList -> IO Word
-- | <https://developer.mozilla.org/en-US/docs/Web/API/SVGStringList.numberOfItems Mozilla SVGStringList.numberOfItems documentation>
getNumberOfItems :: (MonadIO m) => SVGStringList -> m Word
getNumberOfItems self
= liftIO (js_getNumberOfItems (unSVGStringList self))
|
plow-technologies/ghcjs-dom
|
src/GHCJS/DOM/JSFFI/Generated/SVGStringList.hs
|
Haskell
|
mit
| 5,018
|
{-# LANGUAGE FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, TemplateHaskell, TupleSections, TypeFamilies, TypeSynonymInstances #-}
module SpaceWeather.FeaturePack where
import Control.Lens
import Control.Monad.Trans.Either
import Control.Monad.IO.Class
import qualified Data.Aeson.TH as Aeson
import qualified Data.ByteString.Char8 as BS
import Data.Char
import Data.Function(on)
import Data.List (sortBy)
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Yaml as Yaml
import qualified Data.Vector as V
import System.IO
import qualified System.IO.Hadoop as HFS
import System.Random
import Test.QuickCheck.Arbitrary
import Text.Printf
import SpaceWeather.CmdArgs
import SpaceWeather.Feature
import SpaceWeather.Format
import SpaceWeather.TimeLine
data FeatureSchema
= FeatureSchema
{ _colT :: Int
, _colX :: Int
, _scaling :: Double
, _isLog :: Bool
} deriving (Eq, Ord, Show, Read)
makeClassy ''FeatureSchema
Aeson.deriveJSON Aeson.defaultOptions{Aeson.fieldLabelModifier = drop 1} ''FeatureSchema
defaultFeatureSchema :: FeatureSchema
defaultFeatureSchema = FeatureSchema
{ _colT = 1
, _colX = 2
, _scaling = 1
, _isLog = False
}
newtype FeaturePack
= FeaturePack [Feature]
makeWrapped ''FeaturePack
makeClassy ''FeaturePack
data FeatureSchemaPack
= FeatureSchemaPack
{ _fspSchemaDefinitions :: Map.Map String FeatureSchema
, _fspFilenamePairs :: [(String, FilePath)]
} deriving (Eq, Ord, Show, Read)
makeClassy ''FeatureSchemaPack
Aeson.deriveJSON Aeson.defaultOptions{Aeson.fieldLabelModifier = drop 4} ''FeatureSchemaPack
instance Format FeatureSchemaPack where
encode = T.pack . BS.unpack . Yaml.encode
decode = Yaml.decodeEither . BS.pack . T.unpack
loadFeatureWithSchema :: FeatureSchema -> FilePath -> IO (Either String Feature)
loadFeatureWithSchema schema0 fp = runEitherT $ loadFeatureWithSchemaT schema0 fp
loadFeatureWithSchemaT :: FeatureSchema -> FilePath -> EitherT String IO Feature
loadFeatureWithSchemaT schema0 fp = do
txt0 <- liftIO $ do
hPutStrLn stderr $ "loading: " ++ fp
HFS.readFile $ fp
gen0 <- liftIO $ newStdGen
gen1 <- liftIO $ newStdGen
let
convert :: Double -> Double
convert x = if schema0^.isLog then (if x <= 0 then 0 else log x / log 10) else x
parseLine :: (Int, T.Text) -> Either String (TimeBin, Double)
parseLine (lineNum, txt) = do
let wtxt = T.words txt
m2e :: Maybe a -> Either String a
m2e Nothing = Left $ printf "file %s line %d: parse error" fp lineNum
m2e (Just x) = Right $ x
t <- m2e $ readAt wtxt (schema0 ^. colT - 1)
a <- m2e $ readAt wtxt (schema0 ^. colX - 1)
-- if (schema0^.isLog && a <= 0)
-- then Left $ printf "file %s line %d: logscale specified but non-positive colX value: %s" fp lineNum (show a)
-- else return (t,(schema0^.scaling) * convert a)
return (t,(schema0^.scaling) * convert a)
ret :: Either String Feature
ret = fmap fluctuate $
fmap Map.fromList $ mapM parseLine $ linesWithComment txt0
fluctuate :: Feature -> Feature
fluctuate f = f & partsOf each %~ (fluctuateSpace . fluctuateTime)
fluctuateSpace :: [Double] -> [Double]
fluctuateSpace xs = zipWith (\x f -> exp f * x) xs $ randomRs (-spacialNoise, spacialNoise) gen0
fluctuateTime :: [Double] -> [Double]
fluctuateTime xs = let vxs = V.fromList xs in
map (\i -> vxs V.! i) $
zipWith (\i r -> max 0 $ floor $ i-r) [0..] $ randomRs (0,1 + temporalNoise) gen1
hoistEither ret
loadFeatureSchemaPack :: FeatureSchemaPack -> IO (Either String FeaturePack)
loadFeatureSchemaPack fsp = do
let
list0 = fsp ^. fspFilenamePairs
scMap = fsp ^. fspSchemaDefinitions
name2map :: String -> FeatureSchema
name2map fmtName = maybe defaultFeatureSchema
id (Map.lookup fmtName scMap)
list1 <- runEitherT $ mapM (uncurry loadFeatureWithSchemaT) $
map (_1 %~ name2map) list0
return $ fmap FeaturePack list1
|
nushio3/UFCORIN
|
src/SpaceWeather/FeaturePack.hs
|
Haskell
|
mit
| 4,218
|
module TestTCP (
tests4
,tests6
) where
-- local imports
import Network.Transport.Sockets.TCP
import TransportTestSuite
-- external imports
import Test.Framework
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
tests4 :: IO [Test.Framework.Test]
tests4 = do
name1 <- newTCPAddress
name2 <- newTCPAddress
name3 <- newTCPAddress
name4 <- newTCPAddress
return $ transportTestSuite
(newTCPTransport4 tcpSocketResolver4)
"tcp4"
name1
name2
name3
name4
tests6 :: IO [Test.Framework.Test]
tests6 = do
name1 <- newTCPAddress6
name2 <- newTCPAddress6
name3 <- newTCPAddress6
name4 <- newTCPAddress6
return $ transportTestSuite
(newTCPTransport6 tcpSocketResolver6)
"tcp6"
name1
name2
name3
name4
_log :: String
_log = "_test_TCP"
|
hargettp/courier
|
tests/TestTCP.hs
|
Haskell
|
mit
| 924
|
module ExLint.Types
( Block(..)
, Language(..)
, Example(..)
, CheckResult(..)
, Plugin(..)
) where
import Data.Monoid ((<>))
import Data.Text (Text)
import Text.Markdown.Block (Block(..))
data Language
= Haskell
| Unknown
deriving Show
data Example = Example
{ examplePlugin :: Plugin
, examplePreamble :: Text
, exampleExpression :: Text
, exampleResult :: Text
}
deriving Show
data CheckResult
= Match
| MisMatch Text Text Text
deriving Show
data Plugin = Plugin
{ pluginLanguage :: Language
, pluginSigil :: Text
, pluginCheck :: Example -> IO CheckResult
}
instance Show Plugin where
show (Plugin l _ _) = "ExLint.Plugin for " <> show l
|
pbrisbin/ex-lint
|
src/ExLint/Types.hs
|
Haskell
|
mit
| 740
|
{-# LANGUAGE OverloadedStrings #-}
import CheckConcreteSyntax hiding (main)
import CheckQuantityBehaviour hiding (main)
import CheckSkeletonParser hiding (main)
import CheckTranslationPhase hiding (main)
import Core.System
import Test.Hspec (Spec, hspec)
main :: IO ()
main = do
finally (hspec suite) (putStrLn ".")
suite :: Spec
suite = do
checkQuantityBehaviour
checkConcreteSyntax
checkSkeletonParser
checkTranslationPhase
|
oprdyn/technique
|
tests/TestSuite.hs
|
Haskell
|
mit
| 439
|
{-|
Module : Lambdajudge
Description : Easily host haskell based coding competitions using Lambdajudge library
Copyright : (c) Ankit Kumar, Venugopal, Safal 2015
License : MIT
Maintainer : ankitkumar.itbhu@gmail.com
Stability : experimental
Portability : POSIX
-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE FlexibleContexts #-}
module Lambdajudge
(
muevalAvlbl,
gradeSubmission,
runLJMonad
) where
import System.Environment
import System.FilePath
import System.Timeout
import System.Process
import System.Exit
import System.IO
import Data.List
import Control.Monad.State
import Control.Monad.Writer
import Control.Monad.Error
import Control.Monad.Identity
import Control.Exception
import DataStructures
import Utility
-- TODO : export functions finalize, check if mueval present, parallelize, count threads, loading, logging
-- | runs "solution" function loaded from the Submitted solution file within mueval. The expression is provided contents of test cases as input
runSubmission :: MuevalCommand -> LJMonad StatusCode
runSubmission mue = do
let options = ["-t", show $ maxRunTime mue] ++
["-e", "lines (" ++ expression mue ++ " " ++ show (testData mue) ++ ") == lines (" ++ (trim . show) (ansData mue) ++ ")"] ++
["-l", solutionFile mue]
(status,out,err) <- liftIO $ readProcessWithExitCode "mueval" options ""
tell $ show (status,out,err)
liftIO $ print (status,out,err)
modify succ
case status of
ExitSuccess -> case err of
"" -> if (read out :: Bool)
then return AC
else return WA
ExitFailure a -> if "Time limit exceeded" `isInfixOf` err
then return TLE
else error err
-- | maps evaluation of solution file to each test case of the problem. Effectively we are sequencing the runs corresponding to each test case
gradeSubmission :: Problem -> FilePath -> LJMonad [StatusCode]
gradeSubmission prob solutionFile = do
let computations = map (\i -> uncurry (MuevalCommand "solution") (testCases prob!!i) solutionFile (timeLimit prob)) [0..length (testCases prob) - 1]
mapM runSubmission computations
-- | runs submitted code on problem testcases
runLJMonad :: Num s => WriterT w (StateT s (ErrorT e m)) a -> m (Either e ((a, w), s))
runLJMonad = runErrorT . flip runStateT 0 . runWriterT
-- | Idiom to catch any type of exception thrown
catchAny :: IO a -> (SomeException -> IO a) -> IO a
catchAny = Control.Exception.catch
-- | Check if mueval is installed on this system
muevalAvlbl :: IO Bool
muevalAvlbl = do
res <- liftIO $ catchAny (readProcessWithExitCode "mueval" ["-e","1+1"] "") $ \e -> do
putStrLn "Got an exception."
return (ExitFailure 1,"","")
case res of
(ExitSuccess,out,err) -> return True
otherwise -> return False
-- | creating a sample Contest
createContest :: IO Problem
createContest = do
let dir = "test/contest1/Q1/"
t1 <- getFileContents (dir </> "input00.txt")
s1 <- getFileContents (dir </> "output00.txt")
t2 <- getFileContents (dir </> "input02.txt")
s2 <- getFileContents (dir </> "output02.txt")
ps1 <- getFileContents (dir </> "ProblemStatement")
sol1 <- getFileContents (dir </> "Solution/Solution.hs")
return $ Problem ps1 [(t1,s1),(t2,s2)] sol1 5
test = do
prob1 <- Lambdajudge.createContest
runLJMonad $ gradeSubmission prob1 "test/contest1/Q1/Solution/Solution.hs"
|
venugangireddy/Lambdajudge
|
src/Lambdajudge.hs
|
Haskell
|
mit
| 3,627
|
-----------------------------------------------------------------------------
--
-- Module : Language.PureScript.CodeGen.Common
-- Copyright : (c) Phil Freeman 2013
-- License : MIT
--
-- Maintainer : Phil Freeman <paf31@cantab.net>
-- Stability : experimental
-- Portability :
--
-- |
-- Common code generation utility functions
--
-----------------------------------------------------------------------------
module Language.PureScript.CodeGen.Common where
import Data.Char
import Data.List (intercalate)
import Language.PureScript.Names
-- |
-- Convert an Ident into a valid Javascript identifier:
--
-- * Alphanumeric characters are kept unmodified.
--
-- * Reserved javascript identifiers are prefixed with '$$'.
--
-- * Symbols are prefixed with '$' followed by a symbol name or their ordinal value.
--
identToJs :: Ident -> String
identToJs (Ident name) | nameIsJsReserved name = "$$" ++ name
identToJs (Ident name) = concatMap identCharToString name
identToJs (Op op) = concatMap identCharToString op
-- |
-- Attempts to find a human-readable name for a symbol, if none has been specified returns the
-- ordinal value.
--
identCharToString :: Char -> String
identCharToString c | isAlphaNum c = [c]
identCharToString '_' = "_"
identCharToString '.' = "$dot"
identCharToString '$' = "$dollar"
identCharToString '~' = "$tilde"
identCharToString '=' = "$eq"
identCharToString '<' = "$less"
identCharToString '>' = "$greater"
identCharToString '!' = "$bang"
identCharToString '#' = "$hash"
identCharToString '%' = "$percent"
identCharToString '^' = "$up"
identCharToString '&' = "$amp"
identCharToString '|' = "$bar"
identCharToString '*' = "$times"
identCharToString '/' = "$div"
identCharToString '+' = "$plus"
identCharToString '-' = "$minus"
identCharToString ':' = "$colon"
identCharToString '\\' = "$bslash"
identCharToString '?' = "$qmark"
identCharToString '@' = "$at"
identCharToString '\'' = "$prime"
identCharToString c = '$' : show (ord c)
-- |
-- Checks whether an identifier name is reserved in Javascript.
--
nameIsJsReserved :: String -> Bool
nameIsJsReserved name =
name `elem` [ "abstract"
, "arguments"
, "boolean"
, "break"
, "byte"
, "case"
, "catch"
, "char"
, "class"
, "const"
, "continue"
, "debugger"
, "default"
, "delete"
, "do"
, "double"
, "else"
, "enum"
, "eval"
, "export"
, "extends"
, "final"
, "finally"
, "float"
, "for"
, "function"
, "goto"
, "if"
, "implements"
, "import"
, "in"
, "instanceof"
, "int"
, "interface"
, "let"
, "long"
, "native"
, "new"
, "null"
, "package"
, "private"
, "protected"
, "public"
, "return"
, "short"
, "static"
, "super"
, "switch"
, "synchronized"
, "this"
, "throw"
, "throws"
, "transient"
, "try"
, "typeof"
, "var"
, "void"
, "volatile"
, "while"
, "with"
, "yield" ]
-- |
-- Test if a string is a valid JS identifier (may return false negatives)
--
isIdent :: String -> Bool
isIdent s@(first : rest) | not (nameIsJsReserved s) && isAlpha first && all isAlphaNum rest = True
isIdent _ = False
moduleNameToJs :: ModuleName -> String
moduleNameToJs (ModuleName pns) = intercalate "_" (runProperName `map` pns)
|
bergmark/purescript
|
src/Language/PureScript/CodeGen/Common.hs
|
Haskell
|
mit
| 3,960
|
{-# LANGUAGE OverloadedStrings, TypeSynonymInstances, FlexibleInstances #-}
module Y2017.M10.D05.Exercise where
{--
Yesterday, I said: "Go out to the database, get some data, analyze it and chart
it." Well, that's all good if you can work on the problem when you have access
to the internet at large to retrieve your data from the DaaS, but let's say,
hypothetically speaking, that you're behind a firewall, so you only have tiny
slots of time when you go out for that Pumpkin Spiced Latte with your friendies
to access those data. You can't write code, get the data, analyze the data and
then plot the results over just one 'PSL' (The TLAs are taking over smh).
So, what's the solution? Order a second PSL? But then that contributes to the
obesity pandemic, and I don't want that on my resume, thank you very much.
And then there's the credit ratings to consider.
(somebody cue the theme to the movie 'Brazil')
So, the data sets we're talking about aren't petabytes nor exobytes, so why not
just retain those data locally so the development and analyses can be done
off-line or behind the firewall?
Why not, indeed!
Today's Haskell problem: read the rows of data from the article, subject, and
article_subject tables, save them locally as JSON, do some magic hand-waving
or coffee-drinking while you analyze those data off-line, then read in the
data from the JSON store.
--}
import Data.Aeson
import Data.Aeson.Encode.Pretty
import Data.Aeson.Types
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Data.HashMap.Strict as HM
import Data.Map (Map)
import Data.Maybe
import qualified Data.Text as T
import Data.Time
import Database.PostgreSQL.Simple
-- We've got the database reads from yesterday exercise. Do that.
-- below imports available via 1HaskellADay git repository
import Data.Hierarchy
import Store.SQL.Util.Pivots
import Y2017.M10.D04.Exercise
-- Now let's save out those data to JSON
instance ToJSON Subject where
toJSON subj = undefined
instance ToJSON Pivot where
toJSON piv = undefined
instance ToJSON ArticleSummary where
toJSON artsum = undefined
-- Okay, so we've enjsonified the rows of the tables, now let's enjsonify
-- the entire table!
data Table a = Table { name :: String, rows :: [a] }
deriving (Eq, Show)
instance ToJSON a => ToJSON (Table a) where
toJSON table = undefined
-- save out the three tables as JSON to their own files
-- TIME PASSES -------------------------------------------------------------
-- Okay now do the same thing IN REVERSE! Load the tables from JSON files
instance FromJSON a => FromJSON (Table a) where
parseJSON obj = undefined
parseKV :: FromJSON a => (String, Value) -> Parser (Table a)
parseKV (name,list) = undefined
{--
Hint: parsing tables is a bit tricky because the key to the values is the
table name. See:
https://stackoverflow.com/questions/42578331/aeson-parse-json-with-unknown-key-in-haskell
for a discussion on parsing unknown keys
--}
instance FromJSON Subject where
parseJSON obj = undefined
instance FromJSON Pivot where
parseJSON obj = undefined
instance FromJSON ArticleSummary where
parseJSON obj = undefined
-- read in your tables back
-- run your analyses
-- celebrate with Pumpkin Spice Lattes!
{-- BONUS -----------------------------------------------------------------
Once you have the grouping from yesterday, you can output the result
graphically as:
1. 3D scatterplot of topics by date
2. Concentric circles: topics containing dates containing articles
3. N-dimentional graph of Topics x Dates x Articles
you choose, or create your own data visualization
--}
visualize :: Grouping -> IO ()
visualize groups = undefined
groupToHierarchy :: Grouping -> Hierarchy String
groupToHierarchy = Hier "NYT Archive" . undefined
topicArts :: (Topic, Map Day [Title]) -> Hierarchy String
topicArts row = undefined
dayArts :: (Day, [Title]) -> Hierarchy String
dayArts = undefined
|
geophf/1HaskellADay
|
exercises/HAD/Y2017/M10/D05/Exercise.hs
|
Haskell
|
mit
| 3,937
|
import FPPrac
merge [] ys = ys
merge xs [] = xs
merge (x:xs) (y:ys) | x <= y = x : merge xs (y:ys)
| otherwise = y : merge (x:xs) ys
mSort :: Ord a => [a] -> [a]
mSort [] = []
mSort [x] = [x]
mSort xs = let (as, bs) = splitAt (length xs `quot` 2) xs
in merge (mSort as) (mSort bs)
|
tcoenraad/functioneel-programmeren
|
practica/serie2/10.merge-sort.hs
|
Haskell
|
mit
| 314
|
-- Atari 2600 6507 CPU --
module Cpu where
import Data.Word
import Data.Bits
import Data.Int
import Control.Monad.State.Strict
import Control.Applicative hiding ((<|>), many, optional, empty)
import qualified Data.Vector.Unboxed as VU
data CPU = CPU {
registers :: Registers,
memory :: Memory,
cycles :: Integer,
scanLine :: Integer
}
data Registers = Registers {
regA :: Word8, -- ^ Accumulator
regX :: Word8, -- ^ Index Register X
regY :: Word8, -- ^ Index Register Y
status :: Word8, -- ^ Processor Status Register
stackPtr :: Word8, -- ^ Stack Pointer
pc :: Word16 -- ^ Program Counter
}
data StatusFlags =
Carry
| Zero
| IRQDisable
| Decimal
| Break
| Unused
| Overflow
| Negative
data Memory = Memory {
workRAM :: VU.Vector Word8, -- ^ 0x0000 - 0x07FF Work RAM
ppuCtrlRAM :: VU.Vector Word8, -- ^ 0x2000 - 0x2007 PPU Control Registers
apuRegisters :: VU.Vector Word8, -- ^ 0x4000 - 0x401F APU Registers
expansionROM :: VU.Vector Word8, -- ^ 0x4200 - 0x5FFF Cartridge Expansion ROM
staticRAM :: VU.Vector Word8, -- ^ 0x6000 - 0x7FFF SRAM
prgROM0 :: VU.Vector Word8, -- ^ 0x8000 - 0xBFFF Program ROM bank 0
prgROM1 :: VU.Vector Word8 -- ^ 0xC000 - 0xFFFF Program ROM bank 1
}
type CPUState a = State CPU a
data AddressingMode =
Immediate | ZeroPageNoReg | ZeroPage Reg | AbsoluteNoReg |
Absolute Reg | IndirectX | IndirectY
data Reg = A | X | Y | S | SP deriving (Eq)
createCPU :: Word16 -> CPU
createCPU startAddr = cpu'
where cycles' = 0
regs = Registers 0 0 0 0x24 0xFD startAddr
cpu' = CPU regs mem cycles' 241
mem = Memory
(VU.replicate 0x0800 0x0)
(VU.replicate 0x0008 0x0)
(VU.replicate 0x0020 0x0)
(VU.replicate 0x1FDF 0x0)
(VU.replicate 0x2000 0x0)
(VU.replicate 0x4000 0x0)
(VU.replicate 0x4000 0x0)
-- Load ROM into memory starting at given address
loadMemory :: [Word8] -> Word16 -> CPU -> CPU
loadMemory rom startAddr = execState loadMemory'
where loadMemory' :: CPUState ()
loadMemory' =
mapM_ (\(val, offset) -> setMem val (startAddr + offset))
(zip rom [0,1..0xFFFF - startAddr])
getReg :: Reg -> CPUState Word8
getReg A = getA
getReg X = getX
getReg Y = getY
getReg S = getS
getReg SP = getSP
setReg :: Reg -> Word8 -> CPUState()
setReg A = setA
setReg X = setX
setReg Y = setY
setReg S = setS
setReg SP = setSP
-- Check if memory access crosses page boundary e.g. 0x01FF 0x0200 are in different pages
-- if so, add 1 to current cycle count
checkPageBoundary :: Word16 -> Word16 -> CPUState ()
checkPageBoundary w1 w2 =
if (w1 .&. 0xFF00) /= (w2 .&. 0xFF00)
then modifyCycles (+1)
else
return()
-- Obtain value in memoru for given addressing mode,
-- If boundary check is set then for Absolute X and Y, and Indirect Y check
-- if page boundary crosses and increment the cycle count if it has
obtainModeVal :: AddressingMode -> Bool -> CPUState Word8
obtainModeVal mode checkBoundary = case mode of
Immediate -> getIm
ZeroPageNoReg -> getMem . fromIntegral =<< getIm
ZeroPage reg -> getMem . fromIntegral =<< (+) <$> getIm <*> getReg reg
AbsoluteNoReg -> getMem =<< getImm
Absolute reg -> do
r <- getReg reg
imm <- getImm
let sum' = imm + (fromIntegral r)
when checkBoundary $ checkPageBoundary imm sum'
getMem sum'
IndirectX -> do
im <- getIm
x <- getX
addr <- concatBytesLe <$> (getMem $ fromIntegral (im + x))
<*> (getMem $ fromIntegral (im + x + 1))
getMem addr
IndirectY -> do
im <- getIm
addr1 <- getMem $ fromIntegral im
addr2 <- getMem $ fromIntegral (im + 1)
let addr = concatBytesLe addr1 addr2
y <- fromIntegral `fmap` getY
when checkBoundary $ checkPageBoundary addr (addr + y)
getMem $ addr + y
setModeVal :: Word8 -> AddressingMode -> CPUState ()
setModeVal w8 mode = case mode of
Immediate -> setIm w8
ZeroPageNoReg -> (setMem w8) . fromIntegral =<< getIm
ZeroPage reg -> (setMem w8) . fromIntegral =<< (+) <$> getIm <*> getReg reg
AbsoluteNoReg -> setMem w8 =<< getImm
Absolute reg -> setMem w8 =<< (+) <$> getImm <*> (fromIntegral <$> (getReg reg))
IndirectX -> do
im <- getIm
x <- getX
addr <- concatBytesLe <$> (getMem $ fromIntegral (im + x))
<*> (getMem $ fromIntegral (im + x + 1))
setMem w8 addr
IndirectY -> do
im <- getIm
addr1 <- getMem $ fromIntegral im
addr2 <- getMem $ fromIntegral (im + 1)
y <- getY
setMem w8 $ (concatBytesLe addr1 addr2) + (fromIntegral y)
push :: Word8 -> CPUState () -- [SP] = val, SP = SP - 1
push w8 = do
addr <- getSP
-- SP points to mem locations 0x100 to 0x1FF
setMem w8 $ 0x100 + (fromIntegral addr)
setSP (addr - 1)
pop :: CPUState (Word8) -- SP = SP + 1, val = [SP]
pop = do
addr <- getSP
val <- getMem $ 1 + 0x100 + (fromIntegral addr)
setSP (addr + 1)
return val
-- Get/Set all Registers from CPU
getRegs :: CPUState Registers
getRegs = get >>= return . registers
setRegs :: Registers -> CPUState ()
setRegs regs = get >>= \cpu -> put $ cpu {registers = regs}
-- Get/Set Cycles of CPU
getCycles :: CPUState Integer
getCycles = get >>= return . cycles
setCycles :: Integer -> CPUState ()
setCycles c = get >>= \cpu -> put $ cpu {cycles = c}
modifyCycles :: (Integer -> Integer) -> CPUState ()
modifyCycles f = do
cycles' <- getCycles
setCycles (f cycles')
-- Get/Set current ScanLine
getScanLine :: CPUState Integer
getScanLine = get >>= return . scanLine
setScanLine :: Integer -> CPUState ()
setScanLine sl = get >>= \cpu -> put $ cpu {scanLine = sl}
modifyScanLine :: (Integer -> Integer) -> CPUState ()
modifyScanLine f = do
sl <- getScanLine
setScanLine (f sl)
-- Set specific Registers
setA :: Word8 -> CPUState ()
setA w = getRegs >>= \regs -> setRegs $ regs {regA = w}
setX :: Word8 -> CPUState ()
setX w = getRegs >>= \regs -> setRegs $ regs {regX = w}
setY :: Word8 -> CPUState ()
setY w = getRegs >>= \regs -> setRegs $ regs {regY = w}
setS :: Word8 -> CPUState ()
setS w = getRegs >>= \regs -> setRegs $ regs {status = w}
setSP :: Word8 -> CPUState()
setSP w = getRegs >>= \regs -> setRegs $ regs {stackPtr = w}
setPC :: Word16 -> CPUState ()
setPC w = getRegs >>= \regs -> setRegs $ regs {pc = w}
-- |Get Registers
getA :: CPUState Word8
getA = getRegs >>= return . regA
getX :: CPUState Word8
getX = getRegs >>= return . regX
getY :: CPUState Word8
getY = getRegs >>= return . regY
getS :: CPUState Word8
getS = getRegs >>= return . status
getSP :: CPUState Word8
getSP = getRegs >>= return . stackPtr
getPC :: CPUState Word16
getPC = getRegs >>= return . pc
-- Memory
getIm :: CPUState Word8
getIm = do
pc' <- getPC
getMem (pc' - 1)
setIm :: Word8 -> CPUState ()
setIm w8 = do
pc' <- getPC
setMem w8 (pc' - 1)
getImm :: CPUState Word16
getImm = do
pc' <- getPC
concatBytesLe <$> getMem (pc' - 2) <*> getMem (pc' - 1)
setImm :: Word16 -> CPUState ()
setImm w16 = do
pc' <- getPC
setMem (fromIntegral (w16 .&. 0x7F)) (pc' - 2)
setMem (fromIntegral (w16 `shiftR` 8)) (pc' - 1)
getAllMem :: CPUState Memory
getAllMem = get >>= return . memory
setAllMem :: Memory -> CPUState ()
setAllMem mem = get >>= \cpu -> put $ cpu {memory = mem}
-- Get/Set word from Memory
getMem :: Word16 -> CPUState Word8
getMem addr = do
(Memory wRAM pCtrlRAM aRegisters expansionRegs static prgR0 prgR1) <- getAllMem
if addr < 0x2000 then
return $ wRAM VU.! fromIntegral (addr .&. 0x7FF)
else if addr < 0x4000 then
return $ pCtrlRAM VU.! fromIntegral (addr .&. 0x7)
else if addr < 0x4020 then
return $ aRegisters VU.! fromIntegral (addr - 0x4000)
else if addr < 0x6000 then
return $ expansionRegs VU.! fromIntegral (addr - 0x4020)
else if addr < 0x8000 then
return $ static VU.! fromIntegral (addr - 0x6000)
else if addr < 0xC000 then
return $ prgR0 VU.! fromIntegral (addr - 0x8000)
else
return $ prgR1 VU.! fromIntegral (addr - 0xC000)
setMem :: Word8 -> Word16 -> CPUState ()
setMem val addr = do
mem@(Memory wRAM pCtrlRAM aRegisters expansionRegs static prgR0 prgR1) <- getAllMem
if addr < 0x2000 then
setAllMem $ mem {workRAM = setMem' wRAM (.&. 0x7FF)}
else if addr < 0x4000 then
setAllMem $ mem {ppuCtrlRAM = setMem' pCtrlRAM (.&. 0x7)}
else if addr < 0x4020 then
setAllMem $ mem {apuRegisters = setMem' aRegisters (\a -> a - 0x4000)}
else if addr < 0x6000 then
setAllMem $ mem {expansionROM = setMem' expansionRegs (\a -> a - 0x4020)}
else if addr < 0x8000 then
setAllMem $ mem {staticRAM = setMem' static (\a -> a - 0x6000)}
else if addr < 0xC000 then
setAllMem $ mem {prgROM0 = setMem' prgR0 (\a -> a - 0x8000)}
else
setAllMem $ mem {prgROM1 = setMem' prgR1 (\a -> a - 0xC000)}
where setMem' :: VU.Vector Word8 -> (Word16 -> Word16) -> VU.Vector Word8
setMem' v f = VU.update v (VU.fromList [(fromIntegral (f addr), val)])
getCarry :: CPUState Bool
getCarry = getS >>= \s -> return $ (s .&. 0x1) /= 0
getNeg :: CPUState Bool
getNeg = getS >>= \s -> return $ (s .&. 0x80) /= 0
getZero :: CPUState Bool
getZero = getS >>= \s -> return $ (s .&. 0x2) /= 0
getOverflow :: CPUState Bool
getOverflow = getS >>= \s -> return $ (s .&. 0x40) /= 0
setFlag :: Word8 -> Bool -> CPUState ()
setFlag bits state' = do
s <- getS
setS $ op s
where op = if state' then (.|. bits) else (.&. (0xFF - bits))
setCarry :: Bool -> CPUState ()
setCarry = setFlag 0x1
setNeg :: Bool -> CPUState ()
setNeg = setFlag 0x80
setZero :: Bool -> CPUState ()
setZero = setFlag 0x2
setOverflow :: Bool -> CPUState ()
setOverflow = setFlag 0x40
setIRQ :: Bool -> CPUState ()
setIRQ = setFlag 0x4
-- Flag Checking Helper Functions --
checkNegFlag :: Word8 -> CPUState ()
checkNegFlag w8 = setNeg (w8 > 127)
checkZeroFlag :: Word8 -> CPUState ()
checkZeroFlag w8 = setZero (w8 == 0)
boolToBit :: Bool -> Word8
boolToBit b = if b then 1 else 0
-- Concatonate 2 bytes into 16 byte little endian word
-- first byte given is lower half of the word, second is upper half
concatBytesLe :: Word8 -> Word8 -> Word16
concatBytesLe b1 b2 = fromIntegral b1 .|. shiftL (fromIntegral b2) 8
add3 :: Word8 -> Word8 -> Word8 -> Word8
add3 a b c = a + b + c
isNeg :: Word8 -> Bool
isNeg = (>) 127
toSigned :: Word8 -> Int8
toSigned w8 = fromIntegral $ (w8 .&. 127) - fromIntegral (w8 .&. 128)
|
RossMeikleham/NesMonad
|
src/Cpu.hs
|
Haskell
|
mit
| 10,856
|
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TupleSections #-}
-- | http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition.html
module Stratosphere.ResourceProperties.EMRInstanceGroupConfigCloudWatchAlarmDefinition where
import Stratosphere.ResourceImports
import Stratosphere.ResourceProperties.EMRInstanceGroupConfigMetricDimension
-- | Full data type definition for
-- EMRInstanceGroupConfigCloudWatchAlarmDefinition. See
-- 'emrInstanceGroupConfigCloudWatchAlarmDefinition' for a more convenient
-- constructor.
data EMRInstanceGroupConfigCloudWatchAlarmDefinition =
EMRInstanceGroupConfigCloudWatchAlarmDefinition
{ _eMRInstanceGroupConfigCloudWatchAlarmDefinitionComparisonOperator :: Val Text
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionDimensions :: Maybe [EMRInstanceGroupConfigMetricDimension]
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionEvaluationPeriods :: Maybe (Val Integer)
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionMetricName :: Val Text
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionNamespace :: Maybe (Val Text)
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionPeriod :: Val Integer
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionStatistic :: Maybe (Val Text)
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionThreshold :: Val Double
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionUnit :: Maybe (Val Text)
} deriving (Show, Eq)
instance ToJSON EMRInstanceGroupConfigCloudWatchAlarmDefinition where
toJSON EMRInstanceGroupConfigCloudWatchAlarmDefinition{..} =
object $
catMaybes
[ (Just . ("ComparisonOperator",) . toJSON) _eMRInstanceGroupConfigCloudWatchAlarmDefinitionComparisonOperator
, fmap (("Dimensions",) . toJSON) _eMRInstanceGroupConfigCloudWatchAlarmDefinitionDimensions
, fmap (("EvaluationPeriods",) . toJSON) _eMRInstanceGroupConfigCloudWatchAlarmDefinitionEvaluationPeriods
, (Just . ("MetricName",) . toJSON) _eMRInstanceGroupConfigCloudWatchAlarmDefinitionMetricName
, fmap (("Namespace",) . toJSON) _eMRInstanceGroupConfigCloudWatchAlarmDefinitionNamespace
, (Just . ("Period",) . toJSON) _eMRInstanceGroupConfigCloudWatchAlarmDefinitionPeriod
, fmap (("Statistic",) . toJSON) _eMRInstanceGroupConfigCloudWatchAlarmDefinitionStatistic
, (Just . ("Threshold",) . toJSON) _eMRInstanceGroupConfigCloudWatchAlarmDefinitionThreshold
, fmap (("Unit",) . toJSON) _eMRInstanceGroupConfigCloudWatchAlarmDefinitionUnit
]
-- | Constructor for 'EMRInstanceGroupConfigCloudWatchAlarmDefinition'
-- containing required fields as arguments.
emrInstanceGroupConfigCloudWatchAlarmDefinition
:: Val Text -- ^ 'emrigccwadComparisonOperator'
-> Val Text -- ^ 'emrigccwadMetricName'
-> Val Integer -- ^ 'emrigccwadPeriod'
-> Val Double -- ^ 'emrigccwadThreshold'
-> EMRInstanceGroupConfigCloudWatchAlarmDefinition
emrInstanceGroupConfigCloudWatchAlarmDefinition comparisonOperatorarg metricNamearg periodarg thresholdarg =
EMRInstanceGroupConfigCloudWatchAlarmDefinition
{ _eMRInstanceGroupConfigCloudWatchAlarmDefinitionComparisonOperator = comparisonOperatorarg
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionDimensions = Nothing
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionEvaluationPeriods = Nothing
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionMetricName = metricNamearg
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionNamespace = Nothing
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionPeriod = periodarg
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionStatistic = Nothing
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionThreshold = thresholdarg
, _eMRInstanceGroupConfigCloudWatchAlarmDefinitionUnit = Nothing
}
-- | http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition.html#cfn-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition-comparisonoperator
emrigccwadComparisonOperator :: Lens' EMRInstanceGroupConfigCloudWatchAlarmDefinition (Val Text)
emrigccwadComparisonOperator = lens _eMRInstanceGroupConfigCloudWatchAlarmDefinitionComparisonOperator (\s a -> s { _eMRInstanceGroupConfigCloudWatchAlarmDefinitionComparisonOperator = a })
-- | http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition.html#cfn-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition-dimensions
emrigccwadDimensions :: Lens' EMRInstanceGroupConfigCloudWatchAlarmDefinition (Maybe [EMRInstanceGroupConfigMetricDimension])
emrigccwadDimensions = lens _eMRInstanceGroupConfigCloudWatchAlarmDefinitionDimensions (\s a -> s { _eMRInstanceGroupConfigCloudWatchAlarmDefinitionDimensions = a })
-- | http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition.html#cfn-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition-evaluationperiods
emrigccwadEvaluationPeriods :: Lens' EMRInstanceGroupConfigCloudWatchAlarmDefinition (Maybe (Val Integer))
emrigccwadEvaluationPeriods = lens _eMRInstanceGroupConfigCloudWatchAlarmDefinitionEvaluationPeriods (\s a -> s { _eMRInstanceGroupConfigCloudWatchAlarmDefinitionEvaluationPeriods = a })
-- | http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition.html#cfn-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition-metricname
emrigccwadMetricName :: Lens' EMRInstanceGroupConfigCloudWatchAlarmDefinition (Val Text)
emrigccwadMetricName = lens _eMRInstanceGroupConfigCloudWatchAlarmDefinitionMetricName (\s a -> s { _eMRInstanceGroupConfigCloudWatchAlarmDefinitionMetricName = a })
-- | http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition.html#cfn-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition-namespace
emrigccwadNamespace :: Lens' EMRInstanceGroupConfigCloudWatchAlarmDefinition (Maybe (Val Text))
emrigccwadNamespace = lens _eMRInstanceGroupConfigCloudWatchAlarmDefinitionNamespace (\s a -> s { _eMRInstanceGroupConfigCloudWatchAlarmDefinitionNamespace = a })
-- | http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition.html#cfn-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition-period
emrigccwadPeriod :: Lens' EMRInstanceGroupConfigCloudWatchAlarmDefinition (Val Integer)
emrigccwadPeriod = lens _eMRInstanceGroupConfigCloudWatchAlarmDefinitionPeriod (\s a -> s { _eMRInstanceGroupConfigCloudWatchAlarmDefinitionPeriod = a })
-- | http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition.html#cfn-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition-statistic
emrigccwadStatistic :: Lens' EMRInstanceGroupConfigCloudWatchAlarmDefinition (Maybe (Val Text))
emrigccwadStatistic = lens _eMRInstanceGroupConfigCloudWatchAlarmDefinitionStatistic (\s a -> s { _eMRInstanceGroupConfigCloudWatchAlarmDefinitionStatistic = a })
-- | http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition.html#cfn-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition-threshold
emrigccwadThreshold :: Lens' EMRInstanceGroupConfigCloudWatchAlarmDefinition (Val Double)
emrigccwadThreshold = lens _eMRInstanceGroupConfigCloudWatchAlarmDefinitionThreshold (\s a -> s { _eMRInstanceGroupConfigCloudWatchAlarmDefinitionThreshold = a })
-- | http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition.html#cfn-elasticmapreduce-instancegroupconfig-cloudwatchalarmdefinition-unit
emrigccwadUnit :: Lens' EMRInstanceGroupConfigCloudWatchAlarmDefinition (Maybe (Val Text))
emrigccwadUnit = lens _eMRInstanceGroupConfigCloudWatchAlarmDefinitionUnit (\s a -> s { _eMRInstanceGroupConfigCloudWatchAlarmDefinitionUnit = a })
|
frontrowed/stratosphere
|
library-gen/Stratosphere/ResourceProperties/EMRInstanceGroupConfigCloudWatchAlarmDefinition.hs
|
Haskell
|
mit
| 8,289
|
module FVL.EF
( F.Result(..)
, Expr(..)
, showTranslation
, run
, ParseError
, parseRun
, parseFileRun
) where
import FVL.Algebra
import qualified FVL.FAST as FAST
import qualified FVL.F as F
import FVL.EFAST
import FVL.Parser
argument :: String -> [String] -> Bool
argument s [] = False
argument s (x:xs) = if x == s then True else argument s xs
recursiveAlg :: String -> Algebra FAST.Expr Bool
recursiveAlg _ (FAST.CInt n) = False
recursiveAlg _ (FAST.CBool b) = False
recursiveAlg s (FAST.CVar s') = if s' == s then True else False
recursiveAlg _ (FAST.Add x y) = x || y
recursiveAlg _ (FAST.Sub x y) = x || y
recursiveAlg _ (FAST.Mul x y) = x || y
recursiveAlg _ (FAST.Div x y) = x || y
recursiveAlg _ (FAST.And x y) = x || y
recursiveAlg _ (FAST.Or x y) = x || y
recursiveAlg _ (FAST.Not x) = x
recursiveAlg _ (FAST.Equal x y) = x || y
recursiveAlg _ (FAST.Less x y) = x || y
recursiveAlg _ FAST.Empty = False
recursiveAlg _ (FAST.Cons x y) = x || y
recursiveAlg _ (FAST.If p x y) = p || x || y
recursiveAlg s (FAST.Function s' p) = if s' == s then False else p
recursiveAlg _ (FAST.Appl f x) = f || x
recursiveAlg s (FAST.LetRec f x p e) = if f == s
then False
else if x == s then p else p || e
recursiveAlg _ (FAST.Case p x _ _ y) = p || x || y
recursive :: String -> Fix FAST.Expr -> Bool
recursive s = cata $ recursiveAlg s
createWrapper :: [String] -> Fix FAST.Expr -> Fix FAST.Expr
createWrapper [] e = e
createWrapper (x:xs) e = Fx $ FAST.Function x (createWrapper xs e)
letTransform :: String -> Fix FAST.Expr -> Fix FAST.Expr -> Fix FAST.Expr
letTransform s x y = Fx $ FAST.Appl (Fx $ FAST.Function s y) x
modTransform :: Fix FAST.Expr -> Fix FAST.Expr -> Fix FAST.Expr
modTransform x y = let y' = Fx $ FAST.Mul y (Fx $ FAST.Div x y)
in Fx $ FAST.Sub x y'
lteTransform :: Fix FAST.Expr -> Fix FAST.Expr -> Fix FAST.Expr
lteTransform x y = Fx $ FAST.Or (Fx $ FAST.Less x y) (Fx $ FAST.Equal x y)
gtTransform :: Fix FAST.Expr -> Fix FAST.Expr -> Fix FAST.Expr
gtTransform x y = Fx . FAST.Not $ lteTransform x y
gteTransform :: Fix FAST.Expr -> Fix FAST.Expr -> Fix FAST.Expr
gteTransform x y = Fx . FAST.Not . Fx $ FAST.Less x y
alg :: Algebra Expr (Fix FAST.Expr)
alg (CInt n) = Fx $ FAST.CInt n
alg (CBool b) = Fx $ FAST.CBool b
alg (CVar s) = Fx $ FAST.CVar s
alg (Add x y) = Fx $ FAST.Add x y
alg (Sub x y) = Fx $ FAST.Sub x y
alg (Mul x y) = Fx $ FAST.Mul x y
alg (Div x y) = Fx $ FAST.Div x y
alg (Mod x y) = modTransform x y
alg (And x y) = Fx $ FAST.And x y
alg (Or x y) = Fx $ FAST.Or x y
alg (Not x) = Fx $ FAST.Not x
alg (Equal x y) = Fx $ FAST.Equal x y
alg (Less x y) = Fx $ FAST.Less x y
alg (LessEq x y) = lteTransform x y
alg (Great x y) = gtTransform x y
alg (GreatEq x y) = gteTransform x y
alg Empty = Fx FAST.Empty
alg (Cons x y) = Fx $ FAST.Cons x y
alg (If p x y) = Fx $ FAST.If p x y
alg (Function s p) = Fx $ FAST.Function s p
alg (Appl f x) = Fx $ FAST.Appl f x
alg (Let f [] p e) = letTransform f p e
alg (Let f (a:as) p e) = if recursive f p
then Fx $ FAST.LetRec f a (createWrapper as p) e
else letTransform f (createWrapper (a:as) p) e
alg (Semi x y) = Fx $ FAST.Appl (Fx $ FAST.Function "_" y) x
alg (Case p x s t y) = Fx $ FAST.Case p x s t y
translate :: Fix Expr -> Fix FAST.Expr
translate = cata alg
showTranslation :: Fix Expr -> String
showTranslation = show . translate
run :: Fix Expr -> F.Result
run = F.run . translate
parseRun :: String -> Either ParseError F.Result
parseRun s = case parseString s of
Left e -> Left e
Right e -> Right $ run e
parseFileRun :: FilePath -> IO (Either ParseError F.Result)
parseFileRun p = do
r <- parseFile p
case r of
Left e -> return $ Left e
Right e -> return . Right $ run e
|
burz/Feval
|
FVL/EF.hs
|
Haskell
|
mit
| 3,733
|
module Geometry (
sphereVolume,
sphereArea,
cubeVolume,
cubeArea,
cuboidVolume,
cuboidArea
) where
sphereVolume :: Float -> Float
sphereVolume radius = (4.0 / 3.0) * pi * (radius ^ 3)
sphereArea :: Float -> Float
sphereArea radius = 4 * pi * (radius ^ 2)
cubeVolume :: Float -> Float
cubeVolume side = cuboidVolume side side side
cubeArea :: Float -> Float
cubeArea side = cuboidArea side side side
cuboidVolume :: Float -> Float -> Float -> Float
cuboidVolume a b c = rectangleArea a b * c
cuboidArea :: Float -> Float -> Float -> Float
cuboidArea a b c = rectangleArea a b * 2 + rectangleArea a c * 2 + rectangleArea c b * 2
rectangleArea :: Float -> Float -> Float
rectangleArea a b = a * b
|
afronski/playground-fp
|
books/learn-you-a-haskell-for-great-good/modules/Geometry.hs
|
Haskell
|
mit
| 718
|
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Main where
import Nanomsg
import Test.Framework.TH (defaultMainGenerator)
import Test.Framework.Providers.QuickCheck2 (testProperty)
import Test.QuickCheck
import Test.QuickCheck.Monadic
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as C
import Control.Concurrent (threadDelay)
import Control.Applicative ( (<$>) )
import Data.Maybe (catMaybes)
instance Arbitrary ByteString where
arbitrary = C.pack <$> arbitrary
-- dummy test
prop_reverse :: [Int] -> Bool
prop_reverse xs =
xs == reverse (reverse xs)
-- test Pub and Sub sockets
prop_PubSub :: Property
prop_PubSub = monadicIO $ do
(msgs :: [ByteString]) <- pick arbitrary
pre $ not (null msgs)
res <- run $ do
pub <- socket Pub
ep1 <- bind pub "inproc://pubsub"
sub1 <- socket Sub
ep2 <- connect sub1 "inproc://pubsub"
subscribe sub1 $ C.pack ""
sub2 <- socket Sub
ep3 <- connect sub2 "inproc://pubsub"
subscribe sub2 $ C.pack ""
threadDelay 1000
r <- mapM (sendMsg pub sub1 sub2) msgs
unsubscribe sub2 $ C.pack ""
unsubscribe sub1 $ C.pack ""
shutdown sub2 ep3
shutdown sub1 ep2
shutdown pub ep1
close pub
close sub1
close sub2
threadDelay 1000
return r
assert $ and res
where
sendMsg pub sub1 sub2 msg = do
send pub msg
send pub msg
a <- recv sub1
b <- recv sub1
c <- recv sub2
d <- recv sub2
return $ a == msg && b == msg && c == msg && d == msg
-- test Pair sockets
prop_Pair :: Property
prop_Pair = monadicIO $ do
(msgs :: [ByteString]) <- pick arbitrary
pre $ not (null msgs)
res <- run $ do
s1 <- socket Pair
_ <- bind s1 "inproc://pair"
s2 <- socket Pair
_ <- connect s2 "inproc://pair"
threadDelay 1000
-- Send message from s1 to s2, then back from s2 to s1, then make sure it hasn't changed
r <- mapM (\m -> send s1 m >> recv s2 >>= send s2 >> recv s1 >>= return . (== m)) msgs
close s1
close s2
threadDelay 1000
return r
assert $ and res
-- test Pipeline (Push & Pull) sockets
prop_Pipeline :: Property
prop_Pipeline = monadicIO $ do
(msgs :: [ByteString]) <- pick arbitrary
pre $ not (null msgs)
res <- run $ do
push <- socket Push
_ <- bind push "inproc://pipeline"
pull1 <- socket Pull
pull2 <- socket Pull
_ <- connect pull1 "inproc://pipeline"
_ <- connect pull2 "inproc://pipeline"
threadDelay 1000
r <- mapM (testSockets push pull1 pull2) msgs
close push
close pull1
close pull2
threadDelay 1000
return r
assert $ and res
where
testSockets push pull1 pull2 msg = do
send push msg
send push msg
send push msg
threadDelay 1000
a <- recv' pull1
b <- recv' pull1
c <- recv' pull1
d <- recv' pull2
e <- recv' pull2
f <- recv' pull2
let xs = catMaybes [a, b, c, d, e, f]
return $ all (== msg) xs && (length xs == 3)
-- test Req and Rep sockets
prop_ReqRep :: Property
prop_ReqRep = monadicIO $ do
(msgs :: [ByteString]) <- pick arbitrary
pre $ not (null msgs)
res <- run $ do
req <- socket Req
_ <- bind req "inproc://reqrep"
rep <- socket Rep
_ <- connect rep "inproc://reqrep"
threadDelay 1000
r <- mapM (\m -> send req m >> recv rep >>= send rep >> recv req >>= return . (== m)) msgs
close req
close rep
threadDelay 1000
return r
assert $ and res
-- test Bus socket
prop_Bus :: Property
prop_Bus = monadicIO $ do
(msgs :: [ByteString]) <- pick arbitrary
pre $ not (null msgs)
res <- run $ do
-- Probably not how you're supposed to connect Bus nodes..
b1 <- socket Bus
_ <- bind b1 "inproc://bus1"
b2 <- socket Bus
_ <- connect b2 "inproc://bus1"
_ <- bind b2 "inproc://bus2"
b3 <- socket Bus
_ <- connect b3 "inproc://bus2"
_ <- bind b3 "inproc://bus3"
_ <- connect b1 "inproc://bus3"
threadDelay 1000
r <- mapM (testSockets b1 b2 b3) msgs
close b1
close b2
close b3
threadDelay 1000
return r
assert $ and res
where
testSockets b1 b2 b3 msg = do
send b1 msg
a <- recv b2
b <- recv b3
send b2 msg
c <- recv b1
d <- recv b3
send b3 msg
e <- recv b1
f <- recv b2
return $ all (== msg) [a, b, c, d, e, f]
prop_TestOptions :: Property
prop_TestOptions = monadicIO $ do
res <- run $ do
req <- socket Req
_ <- bind req "tcp://*:5560"
surveyor <- socket Surveyor
_ <- bind surveyor "inproc://surveyor"
threadDelay 1000
setTcpNoDelay req 1
v1 <- tcpNoDelay req
setTcpNoDelay req 0
v2 <- tcpNoDelay req
setRequestResendInterval req 30000
v3 <- requestResendInterval req
setIpv4Only req 0
v4 <- ipv4Only req
setIpv4Only req 1
v5 <- ipv4Only req
setSndPrio req 7
v6 <- sndPrio req
setReconnectInterval req 50
v7 <- reconnectInterval req
setReconnectIntervalMax req 400
v8 <- reconnectIntervalMax req
setRcvBuf req 200000
v9 <- rcvBuf req
setSndBuf req 150000
v10 <- sndBuf req
setLinger req 500
v11 <- linger req
setSurveyorDeadline surveyor 2000
v12 <- surveyorDeadline surveyor
close req
close surveyor
threadDelay 1000
return [v1 == 1, v2 == 0, v3 == 30000, v4 == 0, v5 == 1, v6 == 7,
v7 == 50, v8 == 400, v9 == 200000, v10 == 150000, v11 == 500, v12 == 2000]
assert $ and res
main :: IO ()
main = $defaultMainGenerator
|
ivarnymoen/nanomsg-haskell
|
tests/Properties.hs
|
Haskell
|
mit
| 6,398
|
{-# LANGUAGE DeriveFunctor #-}
-- Non-monadic tree labeling:
import Control.Monad
data Tr a = Lf a | Br (Tr a) (Tr a)
deriving Show
tr1 = Br (Lf 'a') (Br (Br (Lf 'b') (Lf 'a')) (Lf 'd'))
type Lt a = (Tr (S, a))
type S = Int
label :: Tr a -> Lt a
label tr = snd (lab tr 0)
where
lab :: Tr a -> S -> (S, Lt a)
lab (Lf contents) n = ((n+1), (Lf (n, contents)))
lab (Br l r) n0 = let (n1, l') = lab l n0
(n2, r') = lab r n1
in (n2, Br l' r')
-- Monadic tree labeling:
newtype Labeled anytype = Labeled (S -> (S, anytype)) deriving Functor
instance Monad Labeled where
return contents = Labeled (\st -> (st, contents))
Labeled fst0 >>= fany1 =
Labeled $ \st0 ->
let (st1, any1) = fst0 st0
Labeled fst1 = fany1 any1
in fst1 st1
instance Applicative Labeled where
pure = return
(<*>) = ap
mlabel :: Tr anytype -> Lt anytype
mlabel tr = let Labeled mt = mkm tr
in snd (mt 0)
mkm :: Tr anytype -> Labeled (Lt anytype)
mkm (Lf x)
= updateState >>= \n -> return $ Lf (n,x)
-- Alternative: do n <- updateState
-- return $ Lf (n,x)
mkm (Br l r)
= mkm l >>= \l' ->
mkm r >>= \r' ->
return $ (Br l' r')
-- Alternative: do l' <- mkm l
-- r' <- mkm r
-- return $ (Br l' r')
updateState :: Labeled S
updateState = Labeled (\n -> ((n+1),n))
main = print $ mlabel tr1
|
egaburov/funstuff
|
Haskell/monads/zenmonad/Mumble002.hs
|
Haskell
|
apache-2.0
| 1,521
|
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Spark.Core.Internal.TypesFunctions(
isNullable,
iInnerStrictType,
columnType,
unsafeCastType,
intType,
arrayType,
compatibleTypes,
arrayType',
frameTypeFromCol,
colTypeFromFrame,
canNull,
structField,
structType,
structTypeFromFields,
structTypeTuple,
structTypeTuple',
tupleType,
structName,
iSingleField,
-- cellType,
) where
import Control.Monad.Except
import qualified Data.List.NonEmpty as N
import Control.Arrow(second)
import Data.Function(on)
import Data.List(sort, nub, sortBy)
import qualified Data.Map.Strict as M
import qualified Data.Text as T
import Data.Text(Text, intercalate)
import qualified Data.Vector as V
import Formatting
import Spark.Core.Internal.TypesStructures
import Spark.Core.StructuresInternal
import Spark.Core.Internal.RowGenericsFrom(FromSQL(..), TryS)
import Spark.Core.Internal.Utilities
import Spark.Core.Internal.TypesStructuresRepr(DataTypeRepr, DataTypeElementRepr)
import qualified Spark.Core.Internal.TypesStructuresRepr as DTR
import Spark.Core.Try
-- Performs a cast of the type.
-- This may throw an error if the required type b is not
-- compatible with the type embedded in a.
unsafeCastType :: SQLType a -> SQLType b
-- TODO add more error checking here.
unsafeCastType (SQLType dt) = SQLType dt
-- Given a sql type tag, returns the equivalent data type for a column or a blob
-- (internal)
columnType :: SQLType a -> DataType
columnType (SQLType dt) = dt
-- (internal)
isNullable :: DataType -> Bool
isNullable (StrictType _) = False
isNullable (NullableType _) = True
-- *** Creation of data types ***
-- Takes a data type (assumed to be that of a column or cell) and returns the
-- corresponding dataset type.
-- This should only be used when talking to Spark.
-- All visible operations in Karps use Cell types instead.
-- TODO should it use value or _1? Both seem to be used in Spark.
frameTypeFromCol :: DataType -> StructType
frameTypeFromCol (StrictType (Struct struct)) = struct
frameTypeFromCol dt = _structFromUnfields [("value", dt)]
-- Given the structural type for a dataframe or a dataset, returns the
-- equivalent column type.
colTypeFromFrame :: StructType -> DataType
colTypeFromFrame st @ (StructType fs) = case V.toList fs of
[StructField {
structFieldName = fname,
structFieldType = (StrictType dt)}] | fname == "value" ->
StrictType dt
_ -> StrictType (Struct st)
-- The strict int type
compatibleTypes :: DataType -> DataType -> Bool
compatibleTypes (StrictType sdt) (StrictType sdt') = _compatibleTypesStrict sdt sdt'
compatibleTypes (NullableType sdt) (NullableType sdt') = _compatibleTypesStrict sdt sdt'
compatibleTypes _ _ = False
-- ***** INSTANCES *****
-- In the case of source introspection, datatypes may be returned.
instance FromSQL DataType where
_cellToValue = _cellToValue >=> _sDataTypeFromRepr
_sDataTypeFromRepr :: DataTypeRepr -> TryS DataType
_sDataTypeFromRepr (DTR.DataTypeRepr l) = snd <$> _sToTreeRepr l
_sToTreeRepr :: [DataTypeElementRepr] -> TryS (Int, DataType)
_sToTreeRepr [] = throwError $ sformat "_sToTreeRepr: empty list"
_sToTreeRepr [dtr] | null (DTR.fieldPath dtr) =
-- We are at a leaf, decode the leaf
_decodeLeaf dtr []
_sToTreeRepr l = do
let f dtr = case DTR.fieldPath dtr of
[] -> []
(h : t) -> [(h, dtr')] where dtr' = dtr { DTR.fieldPath = t }
let hDtrt = case filter (null . DTR.fieldPath) l of
[dtr] -> pure dtr
_ ->
throwError $ sformat ("_decodeList: invalid top with "%sh) l
let withHeads = concatMap f l
let g = myGroupBy withHeads
let groupst = M.toList g <&> \(h, l') ->
_sToTreeRepr l' <&> second (StructField (FieldName h))
groups <- sequence groupst
checkedGroups <- _packWithIndex groups
hDtr <- hDtrt
_decodeLeaf hDtr checkedGroups
_packWithIndex :: (Show t) => [(Int, t)] -> TryS [t]
_packWithIndex l = _check 0 $ sortBy (compare `on` fst) l
-- Checks that all the elements are indexed in order by their value.
-- It works by running a counter along each element and seeing that it is here.
_check :: (Show t) => Int -> [(Int, t)] -> TryS [t]
_check _ [] = pure []
_check n ((n', x):t) =
if n == n'
then (x : ) <$> _check (n+1) t
else
throwError $ sformat ("_check: could not match arguments at index "%sh%" for argument "%sh) n ((n', x):t)
_decodeLeaf :: DataTypeElementRepr -> [StructField] -> TryS (Int, DataType)
_decodeLeaf dtr l = _decodeLeafStrict dtr l <&> \sdt ->
if DTR.isNullable dtr
then (DTR.fieldIndex dtr, NullableType sdt)
else (DTR.fieldIndex dtr, StrictType sdt)
_decodeLeafStrict :: DataTypeElementRepr -> [StructField] -> TryS StrictDataType
-- The array type
_decodeLeafStrict dtr [sf] | DTR.typeId dtr == 11 =
pure $ ArrayType (structFieldType sf)
-- Structure types
_decodeLeafStrict dtr l | DTR.typeId dtr == 10 =
pure . Struct . StructType . V.fromList $ l
_decodeLeafStrict dtr [] = case DTR.typeId dtr of
0 -> pure IntType
1 -> pure StringType
2 -> pure BoolType
n -> throwError $ sformat ("_decodeLeafStrict: unknown type magic id "%sh) n
_decodeLeafStrict dtr l =
throwError $ sformat ("_decodeLeafStrict: cannot interpret dtr="%sh%" and fields="%sh) dtr l
_compatibleTypesStrict :: StrictDataType -> StrictDataType -> Bool
_compatibleTypesStrict IntType IntType = True
_compatibleTypesStrict DoubleType DoubleType = True
_compatibleTypesStrict StringType StringType = True
_compatibleTypesStrict (ArrayType et) (ArrayType et') = compatibleTypes et et'
_compatibleTypesStrict (Struct (StructType v)) (Struct (StructType v')) =
(length v == length v') &&
and (V.zipWith compatibleTypes (structFieldType <$> v) (structFieldType <$> v'))
_compatibleTypesStrict _ _ = False
tupleType :: SQLType a -> SQLType b -> SQLType (a, b)
tupleType (SQLType dt1) (SQLType dt2) =
SQLType $ structType [structField "_1" dt1, structField "_2" dt2]
intType :: DataType
intType = StrictType IntType
-- a string
structField :: T.Text -> DataType -> StructField
structField txt = StructField (FieldName txt)
-- The strict structure type
structType :: [StructField] -> DataType
structType = StrictType . Struct . StructType . V.fromList
-- The strict array type
arrayType' :: DataType -> DataType
arrayType' = StrictType . ArrayType
-- Returns the equivalent data type that may be nulled.
canNull :: DataType -> DataType
canNull = NullableType . iInnerStrictType
-- Given a type, returns the corresponding array type.
-- This is preferred to using directly buildType, as it may encounter some
-- overlapping instances.
arrayType :: SQLType a -> SQLType [a]
arrayType (SQLType dt) = SQLType (arrayType' dt)
iInnerStrictType :: DataType -> StrictDataType
iInnerStrictType (StrictType st) = st
iInnerStrictType (NullableType st) = st
iSingleField :: DataType -> Maybe DataType
iSingleField (StrictType (Struct (StructType fields))) = case V.toList fields of
[StructField _ dt] -> Just dt
_ -> Nothing
iSingleField _ = Nothing
structName :: StructType -> Text
structName (StructType fields) =
"struct(" <> intercalate "," (unFieldName . structFieldName <$> V.toList fields) <> ")"
{-| Builds a type that is a tuple of all the given types.
Following the Spark and SQL convention, the indexing starts at 1.
-}
structTypeTuple :: N.NonEmpty DataType -> StructType
structTypeTuple dts =
let numFields = length dts
rawFieldNames = ("_" <> ) . show' <$> (1 N.:| [2..numFields])
fieldNames = N.toList $ unsafeFieldName <$> rawFieldNames
fieldTypes = N.toList dts
-- Unsafe call, but we know it is going to be all different fields
in forceRight $ structTypeFromFields (zip fieldNames fieldTypes)
{-| Returns a data type instead (the most common use case)
Note that unlike Spark and SQL, the indexing starts from 0.
-}
structTypeTuple' :: N.NonEmpty DataType -> DataType
structTypeTuple' = StrictType . Struct . structTypeTuple
structTypeFromFields :: [(FieldName, DataType)] -> Try StructType
structTypeFromFields [] = tryError "You cannot build an empty structure"
structTypeFromFields ((hfn, hdt):t) =
let fs = (hfn, hdt) : t
ct = StructType $ uncurry StructField <$> V.fromList fs
names = fst <$> fs
numNames = length names
numDistincts = length . nub $ names
in if numNames == numDistincts
then return ct
else tryError $ sformat ("Duplicate field names when building the struct: "%sh) (sort names)
_structFromUnfields :: [(T.Text, DataType)] -> StructType
_structFromUnfields l = StructType . V.fromList $ x where
x = [StructField (FieldName name) dt | (name, dt) <- l]
|
krapsh/kraps-haskell
|
src/Spark/Core/Internal/TypesFunctions.hs
|
Haskell
|
apache-2.0
| 8,712
|
module Step_3_2 where
import Text.Html
page = thehtml <<
[ header << (thetitle << "Output")
, body <<
[ h1 << "A to-do list:"
, thediv << toDoHtml
]
]
-- We've changed the type of toDoItems to include a Bool value to
-- indicate if they've been done.
toDoItems :: [(Bool, String)] -- a list of tuples: each a Bool and String
toDoItems =
[ (True, "Pick up avocados")
, (True, "Make snacks")
, (False, "Clean house")
, (False, "Have party")
]
-- Haskellers often format lists with long items in this funny way!
renderToDo :: [String] -> [Html]
renderToDo ts = map (li <<) ts
toDoHtml :: Html
toDoHtml = ulist << renderToDo toDoItems
-- Try running this file. You'll find that the it doesn't compile, and gives
-- an error on the line above. Just read the first two lines of the error and
-- see if you can see what the compiler is trying to tell you about the problem
-- with the types.
-- Fix the renderToDo function to fix the problem.
-- NEXT
-- What did you end up doing with the Bool component of the tuple? Did you
-- ignore it? Did you use it in the computation somehow? Did you notice that
-- by having a clear type, you were forced to think about it?
-- Change the renderToDo function to render the items that aren't done bold.
-- NEXT
-- For a challenge, change the renderToDo function to render only the first
-- not done item bold.
hint1 = map pred "Zpv!qspcbcmz!ibwf!up!hp!cbdl!up!opu!vtjoh!nbq/"
hint2 = map pred "Zpv!qspcbcmz!xjmm!offe!bopuifs!ifmqfs!gvodujpo/"
|
mzero/barley
|
seed/Chapter3/Step_3_2.hs
|
Haskell
|
apache-2.0
| 1,554
|
-- http://www.reddit.com/r/dailyprogrammer/comments/2z68di/20150316_challenge_206_easy_recurrence_relations/
module RecurrenceRelations where
type Operator = Integer -> Integer
type Expression = String
createExpression :: Integer -> [Operator] -> Integer
createExpression seed = foldl (\_ x -> x seed) seed
parse :: Expression -> [Operator]
parse = map toOperator . words
toOperator :: String -> Operator
toOperator ('*':xs) = (*) (read xs :: Integer)
toOperator ('-':xs) = (-) (read xs :: Integer)
toOperator ('/':xs) = div (read xs :: Integer)
toOperator ('+':xs) = (+) (read xs :: Integer)
toOperator _ = error "Malformed expression"
recurrence :: Expression -> Integer -> [Integer]
recurrence expr = iterate (flip createExpression $ parse expr)
|
fffej/haskellprojects
|
daily-programmer/17-03-2015/RecurrenceRelations.hs
|
Haskell
|
bsd-2-clause
| 766
|
module Rules.Cabal (cabalRules) where
import Base
import Data.Version
import Distribution.Package as DP
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse
import Distribution.Verbosity
import Expression
import GHC
import Rules.Actions
import Settings
cabalRules :: Rules ()
cabalRules = do
-- Cache boot package constraints (to be used in cabalArgs)
bootPackageConstraints %> \out -> do
bootPkgs <- interpretWithStage Stage0 getPackages
let pkgs = filter (\p -> p /= compiler && isLibrary p) bootPkgs
constraints <- forM (sort pkgs) $ \pkg -> do
need [pkgCabalFile pkg]
pd <- liftIO . readPackageDescription silent $ pkgCabalFile pkg
let identifier = package . packageDescription $ pd
version = showVersion . pkgVersion $ identifier
DP.PackageName name = DP.pkgName identifier
return $ name ++ " == " ++ version
writeFileChanged out . unlines $ constraints
-- Cache package dependencies
packageDependencies %> \out -> do
pkgs <- interpretWithStage Stage1 getPackages
pkgDeps <- forM (sort pkgs) $ \pkg ->
if pkg == rts
then return $ pkgNameString pkg
else do
need [pkgCabalFile pkg]
pd <- liftIO . readPackageDescription silent $ pkgCabalFile pkg
let depsLib = collectDeps $ condLibrary pd
depsExes = map (collectDeps . Just . snd) $ condExecutables pd
deps = concat $ depsLib : depsExes
depNames = [ name | Dependency (DP.PackageName name) _ <- deps ]
return . unwords $ pkgNameString pkg : sort depNames
writeFileChanged out . unlines $ pkgDeps
-- When the file exists, the packageConfiguration has been initialised
-- TODO: get rid of an extra file?
forM_ [Stage0, Stage1] $ \stage ->
packageConfigurationInitialised stage %> \out -> do
let target = PartialTarget stage cabal
pkgConf = packageConfiguration stage
removeDirectoryIfExists pkgConf
-- TODO: can we get rid of this fake target?
build $ fullTarget target (GhcPkg stage) [] [pkgConf]
let message = "Successfully initialised " ++ pkgConf
writeFileChanged out message
putSuccess message
collectDeps :: Maybe (CondTree v [Dependency] a) -> [Dependency]
collectDeps Nothing = []
collectDeps (Just (CondNode _ deps ifs)) = deps ++ concatMap f ifs
where
f (_, t, mt) = collectDeps (Just t) ++ collectDeps mt
|
quchen/shaking-up-ghc
|
src/Rules/Cabal.hs
|
Haskell
|
bsd-3-clause
| 2,669
|
module Prompt
(
promptData
) where
import System.IO
type ParseFuncType a = (String -> Maybe a)
promptData :: String -> ParseFuncType a -> String -> IO a
promptData sprompt parseFunc failstr = do
putStr sprompt
hFlush stdout
input <- getLine
let maybea = parseFunc input
case maybea of
Just something -> return something
Nothing -> do
putStrLn failstr
promptData sprompt parseFunc failstr
|
monkeybits/rpgame
|
src/Prompt.hs
|
Haskell
|
bsd-3-clause
| 429
|
module Main where
import System.Exit
main = do
putStrLn "This test always passes!"
exitSuccess
|
vollmerm/shallow-fission
|
tests/Test.hs
|
Haskell
|
bsd-3-clause
| 115
|
{-# LANGUAGE DeriveDataTypeable, PatternGuards #-}
-----------------------------------------------------------------------------
-- |
-- Module : Data.Pass.L.Estimator
-- Copyright : (C) 2012-2013 Edward Kmett
-- License : BSD-style (see the file LICENSE)
--
-- Maintainer : Edward Kmett <ekmett@gmail.com>
-- Stability : experimental
-- Portability : non-portable (GADTs, Rank2Types)
--
----------------------------------------------------------------------------
module Data.Pass.L.Estimator
( Estimator(..)
, Estimate(..)
, estimateBy
) where
import Data.Ratio
import Data.Binary
import Data.Data
import qualified Data.IntMap as IM
import Data.IntMap (IntMap)
import Data.Pass.Util (clamp)
import Data.Hashable
import Data.Pass.Util.Beta (Beta(Beta))
import qualified Data.Pass.Util.Beta as Beta
-- | Techniques used to smooth the nearest values when calculating quantile functions. R2 is used by default, and the numbering convention follows the
-- use in the R programming language, as far as it goes.
data Estimator
= R1 -- ^ Inverse of the empirical distribution function
| R2 -- ^ .. with averaging at discontinuities (default)
| R3 -- ^ The observation numbered closest to Np. NB: does not yield a proper median
| R4 -- ^ Linear interpolation of the empirical distribution function. NB: does not yield a proper median.
| R5 -- ^ .. with knots midway through the steps as used in hydrology. This is the simplest continuous estimator that yields a correct median
| R6 -- ^ Linear interpolation of the expectations of the order statistics for the uniform distribution on [0,1]
| R7 -- ^ Linear interpolation of the modes for the order statistics for the uniform distribution on [0,1]
| R8 -- ^ Linear interpolation of the approximate medans for order statistics.
| R9 -- ^ The resulting quantile estimates are approximately unbiased for the expected order statistics if x is normally distributed.
| R10 -- ^ When rounding h, this yields the order statistic with the least expected square deviation relative to p.
| HD -- ^ The Harrell-Davis quantile estimator based on bootstrapped order statistics
deriving (Eq,Ord,Enum,Bounded,Data,Typeable,Show,Read)
instance Binary Estimator where
put e = put (fromIntegral (fromEnum e) :: Word8)
get = do
i <- get :: Get Word8
return $ toEnum (fromIntegral i)
instance Hashable Estimator where
hashWithSalt n e = n `hashWithSalt` fromEnum e
data Estimate r = Estimate {-# UNPACK #-} !Rational (IntMap r)
deriving Show
continuousEstimator ::
Fractional r =>
(Rational -> (Rational, Rational)) ->
(Rational -> Rational -> Rational) ->
Rational -> Int -> Estimate r
continuousEstimator bds f p n = Estimate h $
if p < lo then IM.singleton 0 1
else if p >= hi then IM.singleton (n - 1) 1
else case properFraction h of
(w,frac) | frac' <- fromRational frac -> IM.fromList [(w - 1, frac'), (w, 1 - frac')]
where
r = fromIntegral n
h = f p r
(lo, hi) = bds r
estimateBy :: Fractional r => Estimator -> Rational -> Int -> Estimate r
estimateBy HD = \q n -> Estimate (1%2) $ let
n' = fromIntegral n
np1 = n' + 1
q' = fromRational q
d = Beta (q'*np1) (np1*(1-q'))
in if q == 0 then IM.singleton 0 1
else if q == 1 then IM.singleton (n - 1) 1
else IM.fromListWith (+)
[ (i, realToFrac $ Beta.cumulative d ((fromIntegral i + 1) / n') - Beta.cumulative d (fromIntegral i / n'))
| i <- [0 .. n-1]
]
estimateBy R1 = \p n -> let np = fromIntegral n * p in Estimate (np + 1%2) $ IM.singleton (clamp n (ceiling np - 1)) 1
estimateBy R2 = \p n -> let np = fromIntegral n * p in Estimate (np + 1%2) $
if p == 0 then IM.singleton 0 1
else if p == 1 then IM.singleton (n - 1) 1
else IM.fromListWith (+) [(clamp n (ceiling np - 1), 0.5), (clamp n (floor np), 0.5)]
estimateBy R3 = \p n -> let np = fromIntegral n * p in Estimate np $ IM.singleton (clamp n (round np - 1)) 1
estimateBy R4 = continuousEstimator (\n -> (recip n, 1)) (*)
estimateBy R5 = continuousEstimator (\n -> let tn = 2 * n in (recip tn, (tn - 1) / tn)) $ \p n -> p*n + 0.5
estimateBy R6 = continuousEstimator (\n -> (recip (n + 1), n / (n + 1))) $ \p n -> p*(n+1)
estimateBy R7 = continuousEstimator (\_ -> (0, 1)) $ \p n -> p*(n-1) + 1
estimateBy R8 = continuousEstimator (\n -> (2/3 / (n + 1/3), (n - 1/3)/(n + 1/3))) $ \p n -> p*(n + 1/3) + 1/3
estimateBy R9 = continuousEstimator (\n -> (0.625 / (n + 0.25), (n - 0.375)/(n + 0.25))) $ \p n -> p*(n + 0.25) + 0.375
estimateBy R10 = continuousEstimator (\n -> (1.5 / (n + 2), (n + 0.5)/(n + 2))) $ \p n -> p*(n + 2) - 0.5
|
ekmett/multipass
|
Data/Pass/L/Estimator.hs
|
Haskell
|
bsd-3-clause
| 4,644
|
-- {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
module C where
import Control.Monad.State.Strict
import Data.Data
import Data.Either
import Data.Typeable
import Data.String
import Data.ByteString.Char8 (ByteString,pack,unpack)
import qualified Data.ByteString.Char8 as ByteString
import Network.Mime (defaultMimeLookup)
import System.Directory
import System.FilePath
import System.IO
import Text.Printf
import Types
import JS
-- toFile :: MonadIO m => FilePath -> Writer ByteString a -> m ()
-- toFile f wr = liftIO $ do
-- createDirectoryIfMissing True (takeDirectory f)
-- ByteString.writeFile f $ execWriter $ wr
toFile :: MonadIO m => FilePath -> StateT Handle IO () -> m ()
toFile f m = liftIO $ do
createDirectoryIfMissing True (takeDirectory f)
withFile f WriteMode (evalStateT m)
class LineLike x where
line :: x -> StateT Handle IO ()
line_ putter s = get >>= \h -> liftIO $ putter h s
instance LineLike ByteString where
line = line_ ByteString.hPutStrLn
instance LineLike [Char] where
line = line . pack
instance LineLike [[Char]] where
line s = forM_ s (\s -> line_ ByteString.hPutStr (pack s)) >> line ""
mk_c :: Args -> ByteString -> IO ()
mk_c a d = toFile (out_c a) $ do
line $ "/* http://stupefydeveloper.blogspot.ru/2008/08/cc-embed-binary-data-into-elf.html */"
line $ "#include <urweb.h>"
line $ "#include <stdio.h>"
line $ "#include \"" ++ (takeFileName (out_h a)) ++ "\""
line $ ""
line $ "#define BLOBSZ " ++ (printf "%d" (ByteString.length d))
line $ "static char blob[BLOBSZ];"
line $ ""
line $ [ "uw_Basis_blob ", cblobfun a , " (uw_context ctx, uw_unit unit)" ]
line $ "{"
line $ " uw_Basis_blob uwblob;"
line $ " uwblob.data = &blob[0];"
line $ " uwblob.size = BLOBSZ;"
line $ " return uwblob;"
line $ "}"
line $ ""
line $ [ "uw_Basis_string " , ctextfun a , " (uw_context ctx, uw_unit unit) {" ]
line $ " char* data = &blob[0];"
line $ " size_t size = sizeof(blob);"
line $ " char * c = uw_malloc(ctx, size+1);"
line $ " char * write = c;"
line $ " int i;"
line $ " for (i = 0; i < size; i++) {"
line $ " *write = data[i];"
line $ " if (*write == '\\0')"
line $ " *write = '\\n';"
line $ " *write++;"
line $ " }"
line $ " *write=0;"
line $ " return c;"
line $ " }"
line $ ""
line $ "static char blob[BLOBSZ] = {"
h <- get
liftIO $ do
forM_ (unpack d) $ \c -> do
hPutStr h (printf "0x%02X ," c)
-- liftIO $ ByteString.foldl' (\act c -> act >> hPutStr h (printf "0x%02X ," c)) (return ()) d
line $ "};"
line $ ""
mk_h :: Args -> IO ()
mk_h a = toFile (out_h a) $ do
line $ "#pragma once"
line $ "#include <urweb.h>"
line $ [ "uw_Basis_blob ", cblobfun a , " (uw_context ctx, uw_unit unit);" ]
line $ [ "uw_Basis_string ", ctextfun a, " (uw_context ctx, uw_unit unit);" ]
mk_urs :: Args -> IO ()
mk_urs a = toFile (out_urs a) $ do
line $ [ "val ", urblobfun, " : unit -> transaction blob" ]
line $ [ "val ", urtextfun, " : unit -> transaction string" ]
guessMime inf = fixup $ unpack (defaultMimeLookup (fromString inf)) where
fixup "application/javascript" = "text/javascript"
fixup m = m
mk_wrap :: Args -> [Url] -> Bool -> IO ()
mk_wrap a us open_js_ffi = do
toFile (out_wrapper a) $ do
let mm = guessMime (inp a)
line $ "open " ++ (uwModName (out_urs a))
line $ "fun content {} = b <- "++ urblobfun ++ " () ; returnBlob b (blessMime \"" ++ mm ++ "\")"
line $ "val propagated_urls : list url = "
forM_ us $ \u -> do
line $ " " ++ u ++ ".url ::"
line $ " []"
when (open_js_ffi) $ do
line $ "open " ++ (uwModName (out_ffi_js a))
line $ "val url = url(content {})"
line $ "val geturl = url"
liftIO $ printf "safeGet %s/content\n" (uwModName (out_wrapper a))
liftIO $ printf "allow mime %s\n" mm
mk_js_wrap :: Args -> ([JSType],[JSFunc]) -> IO ()
mk_js_wrap a (jt,jf) = do
let m = uwModName (out_ffi_js a)
toFile (out_ffi_js a) $ do
forM_ jt $ \decl -> line (urtdecl decl)
forM_ jf $ \decl -> do
line (urdecl decl)
liftIO $ printf "jsFunc %s.%s = %s\n" m (urname decl) (jsname decl)
liftIO $ printf "clientOnly %s.%s\n" m (urname decl)
-- mk_js_lib :: Args -> ([JSType],[JSFunc]) -> IO ()
-- mk_js_lib a (jt,jf) = do
-- toFile (out_ffi_js_lib a) $ do
-- let m = uwModName (out_ffi_js a)
-- line $ "ffi " ++ m
-- forM_ jf $ \decl -> do
|
grwlf/cake3
|
app/UrEmbed/C.hs
|
Haskell
|
bsd-3-clause
| 4,494
|
module Validate.US where
import Data.Char
regionStrict :: String -> Bool
regionStrict reg = reg `elem` ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"]
-- Strict parameter
region :: String -> Bool
region reg = region $ map toUpper reg
-- Not implemented
ssn :: String -> Bool
ssn code = False
|
jespino/haskell-validate
|
Validate/US.hs
|
Haskell
|
bsd-3-clause
| 555
|
module SortCheck.Forall (
MetaCtx(..),
checkForallVars
) where
import Control.Monad.Trans.State.Lazy
import Control.Monad.Except (throwError)
import Control.Monad.Trans.Class (lift)
import Control.Lens
import Data.Maybe (isJust)
import Control.Monad (when, unless)
import qualified Data.Set as Set
import AST
import SortCheck.SymbolTable as SymbolTable
type MetaCtx = [(MetaVar, Sort)]
--------------------------------------------------------------------------------
-- ForallVars
-- This function looks up a sortName in state
-- ContextDepth is needed for forming the sort (not lookup)
checkSortByName :: ContextDepth -> SortName -> SortCheckM Sort
checkSortByName depth name = do
st <- get
if Set.member name (st^.simpleSorts)
then
if depth == 0
then return (SimpleSort name)
else throwError $ "Independent sort " ++ name ++ " can't have non-empty context"
else
if Set.member name (st^.depSorts)
then return (DepSort name depth)
else
throwError $ "Sort " ++ name ++ " is not defined"
-- checks and modifies one vars and checks for dups
checkForallVars :: MetaCtx -> SortCheckM MetaCtx
checkForallVars forall = do
-- changes the sort to appropriate depth (if it's dependent at all)
forall' <- mapM (\ (a , b) -> do
b' <- checkSortByName (length $ mContext a) (getSortName b)
return (a , b') ) forall
-- check for dups in captures and x.x situations
mapM_ (\ (a , _) -> unless (allUnique (mName a : mContext a)) $
throwError "Duplicates in captures") forall'
unless (allUnique $ map (mName . fst) forall') $
throwError "Duplicates in metas"
return forall'
---
|
esengie/fpl-exploration-tool
|
src/specLang/SortCheck/Forall.hs
|
Haskell
|
bsd-3-clause
| 1,684
|
module Zero.KeyFetchToken.Relations
(
defineKeyFetchTokenRelation
, keyFetchStatesFromRelation
, keyFetchTokensFromRelation
, keyFetchTokenAttributes
) where
import qualified Data.Vector as V
import Zero.Persistence
import Zero.KeyFetchToken.Internal
import Zero.Token.Internal
------------------------------------------------------------------------------
-- Relational Attributes
------------------------------------------------------------------------------
keyFetchTokenAttributes = V.fromList
[ Attribute "token_id#" TextAtomType
, Attribute "key" TextAtomType
, Attribute "user" TextAtomType
, Attribute "token" TextAtomType
]
keyFetchTokenMatrix = []
relvarMap :: [(RelVarName, Attributes)]
relvarMap =
[ ( "keyFetchToken", keyFetchTokenAttributes ) ]
relvarDefinitions =
map (\(name, attrs) -> Define name (map NakedAttributeExpr (V.toList attrs))) relvarMap
uniqueKeys = map (uncurry databaseContextExprForUniqueKey)
[ ( "keyFetchToken", ["token_id#"] ) ]
keyFetchTokenRelations =
relvarDefinitions ++ uniqueKeys
keyFetchTokenRelation :: Relation
keyFetchTokenRelation =
case mkRelationFromList keyFetchTokenAttributes keyFetchTokenMatrix of
Left err_ ->
undefined
Right rl ->
rl
defineKeyFetchTokenRelation :: SessionId -> Connection -> IO (Either RelationalError ())
defineKeyFetchTokenRelation sid conn = do
eRes <- mapM (executeDatabaseContextExpr sid conn) keyFetchTokenRelations
handleCommit sid conn eRes
keyFetchTokenFromTuple (RelationTuple _ atoms) =
let TextAtom tokenId = atoms ! 0
TextAtom hmacKey = atoms ! 1
TextAtom user = atoms ! 2
TextAtom token = atoms ! 3
in Token tokenId hmacKey user token
keyFetchStateFromTuple (RelationTuple _ atoms) =
let TextAtom kA = atoms ! 4
TextAtom wrap_kB = atoms ! 5
in KeyFetchState kA wrap_kB
keyFetchStateFromRelation :: Relation -> Maybe KeyFetchState
keyFetchStateFromRelation r@(Relation attrNameSet tupleSet) =
let tuples = asList tupleSet in
if not (null tuples) then
Just (keyFetchStateFromTuple (head tuples))
else
Nothing
keyFetchStatesFromRelation :: Relation -> [KeyFetchState]
keyFetchStatesFromRelation r@(Relation attrNameSet tupleSet) =
let tuples = asList tupleSet in
if not (null tuples) then
map keyFetchStateFromTuple tuples
else
[]
keyFetchTokenFromRelation :: Relation -> Maybe Token
keyFetchTokenFromRelation r@(Relation attrNameSet tupleSet) =
let tuples = asList tupleSet in
if not (null tuples) then
Just $ keyFetchTokenFromTuple (head tuples)
else
Nothing
keyFetchTokensFromRelation :: Relation -> [Token]
keyFetchTokensFromRelation (Relation attrNameSet tupleSet) =
let tuples = asList tupleSet in
if not (null tuples) then
map keyFetchTokenFromTuple tuples
else
[]
|
et4te/zero
|
server/src/Zero/KeyFetchToken/Relations.hs
|
Haskell
|
bsd-3-clause
| 2,849
|
-- Samples of graphs useful for testing
-- Many examples come from organic chemistry. These were generated
-- from SMILES strings using a smiles parser which generates
-- LGraphs
module Data.Graph.Graphs (
methane, ethane, propane, butane, alkane
, isobutane, isopentane, neopentane
, cycloalkane, cyclopropane, cyclobutane, cyclopentane, cyclohexane
, ocimene, limonene, pinene
, viagra, xanax, phentermine, valium, ambien, nexium, vioxx
, paxil, lipitor, cialis, strychnine, cocaine, quinine, lsd
, morphine, heroine, nicotine, vitamine_a, caffeine
, module Data.Graph.Simple
) where
import Data.Graph.Simple
-- * Alkanes
methane, ethane, propane, butane β· Graph
methane = alkane 1
ethane = alkane 2
propane = alkane 3
butane = alkane 4
alkane β· Int β Graph
alkane n = chain n
-- * Cycles
cyclopropane, cyclobutane, cyclopentane, cyclohexane β· Graph
cyclopropane = cycloalkane 3
cyclobutane = cycloalkane 4
cyclopentane = cycloalkane 5
cyclohexane = cycloalkane 6
cycloalkane β· Int β Graph
cycloalkane n = fromList n $ (0 <-> (n-1)) : [x <-> (x+1) | x β [0..n-2]]
-- * Branched alkanes
isobutane, isopentane, neopentane β· Graph
isobutane = fromList 4 [0 <-> 1,1 <-> 2,1 <-> 3]
isopentane = fromList 5 [0 <-> 1,1 <-> 2,2 <-> 3,2 <-> 4]
neopentane = fromList 5 [0 <-> 1,1 <-> 2,1 <-> 3,1 <-> 4]
-- * Monoterpenes
ocimene,limonene,pinene β· Graph
ocimene = fromList 10 [0 <-> 1,1 <-> 2,1 <-> 3,3 <-> 4,4 <-> 5,5 <-> 6,6 <-> 7,6 <-> 8,8 <-> 9]
limonene = fromList 10 [0 <-> 1,1 <-> 2,1 <-> 3,3 <-> 4,3 <-> 9,4 <-> 5,5 <-> 6,6 <-> 7,6 <-> 8,8 <-> 9]
pinene = fromList 10 [0 <-> 1,0 <-> 3,0 <-> 6,1 <-> 2,1 <-> 8,1 <-> 9,2 <-> 3,2 <-> 4,4 <-> 5,5 <-> 6,6 <-> 7]
-- * Alkaloids
viagra,xanax,phentermine,valium,ambien,nexium,vioxx,paxil,lipitor,cialis,strychnine,cocaine,quinine,lsd,morphine,heroine,nicotine,vitamine_a,caffeine β· Graph
viagra = fromList 32 [0 <-> 1,1 <-> 2,2 <-> 3,2 <-> 12,3 <-> 4,4 <-> 5,4 <-> 6,6 <-> 7,6 <-> 12,7 <-> 8,7 <-> 9,9 <-> 10,10 <-> 11,10 <-> 13,11 <-> 12,13 <-> 14,13 <-> 18,14 <-> 15,15 <-> 16,15 <-> 22,16 <-> 17,17 <-> 18,18 <-> 19,19 <-> 20,20 <-> 21,22 <-> 23,22 <-> 24,22 <-> 25,25 <-> 26,25 <-> 31,26 <-> 27,27 <-> 28,28 <-> 29,28 <-> 30,30 <-> 31]
xanax = fromList 22 [0 <-> 1,1 <-> 2,1 <-> 21,2 <-> 3,3 <-> 4,4 <-> 5,4 <-> 21,5 <-> 6,6 <-> 7,7 <-> 8,7 <-> 14,8 <-> 9,8 <-> 13,9 <-> 10,10 <-> 11,11 <-> 12,12 <-> 13,14 <-> 15,14 <-> 20,15 <-> 16,16 <-> 17,16 <-> 18,18 <-> 19,19 <-> 20,20 <-> 21]
phentermine = fromList 11 [0 <-> 1,1 <-> 2,1 <-> 3,1 <-> 4,4 <-> 5,5 <-> 6,5 <-> 10,6 <-> 7,7 <-> 8,8 <-> 9,9 <-> 10]
valium = fromList 20 [0 <-> 1,1 <-> 2,1 <-> 19,2 <-> 3,2 <-> 4,4 <-> 5,5 <-> 6,6 <-> 7,6 <-> 13,7 <-> 8,7 <-> 12,8 <-> 9,9 <-> 10,10 <-> 11,11 <-> 12,13 <-> 14,13 <-> 19,14 <-> 15,15 <-> 16,15 <-> 17,17 <-> 18,18 <-> 19]
ambien = fromList 23 [0 <-> 1,1 <-> 2,1 <-> 3,3 <-> 4,3 <-> 5,5 <-> 6,6 <-> 7,6 <-> 15,7 <-> 8,7 <-> 16,8 <-> 9,9 <-> 10,9 <-> 15,10 <-> 11,11 <-> 12,12 <-> 13,12 <-> 14,14 <-> 15,16 <-> 17,16 <-> 22,17 <-> 18,18 <-> 19,19 <-> 20,19 <-> 21,21 <-> 22]
nexium = fromList 24 [0 <-> 1,1 <-> 2,2 <-> 3,2 <-> 10,3 <-> 4,4 <-> 5,5 <-> 6,5 <-> 9,6 <-> 7,7 <-> 8,7 <-> 11,8 <-> 9,9 <-> 10,11 <-> 12,11 <-> 13,13 <-> 14,14 <-> 15,14 <-> 22,15 <-> 16,16 <-> 17,17 <-> 18,17 <-> 19,19 <-> 20,19 <-> 22,20 <-> 21,22 <-> 23]
vioxx = fromList 22 [0 <-> 1,1 <-> 2,1 <-> 3,1 <-> 4,4 <-> 5,4 <-> 9,5 <-> 6,6 <-> 7,7 <-> 8,7 <-> 10,8 <-> 9,10 <-> 11,10 <-> 15,11 <-> 12,11 <-> 16,12 <-> 13,12 <-> 14,14 <-> 15,16 <-> 17,16 <-> 21,17 <-> 18,18 <-> 19,19 <-> 20,20 <-> 21]
paxil = fromList 24 [0 <-> 1,1 <-> 2,1 <-> 6,2 <-> 3,3 <-> 4,4 <-> 5,4 <-> 7,5 <-> 6,7 <-> 8,7 <-> 12,8 <-> 9,9 <-> 10,10 <-> 11,11 <-> 12,12 <-> 13,13 <-> 14,14 <-> 15,15 <-> 16,15 <-> 23,16 <-> 17,17 <-> 18,18 <-> 19,18 <-> 22,19 <-> 20,20 <-> 21,21 <-> 22,22 <-> 23]
lipitor = fromList 40 [0 <-> 1,1 <-> 2,1 <-> 3,3 <-> 4,3 <-> 23,4 <-> 5,4 <-> 14,5 <-> 6,5 <-> 7,7 <-> 8,8 <-> 9,8 <-> 13,9 <-> 10,10 <-> 11,11 <-> 12,12 <-> 13,14 <-> 15,14 <-> 34,15 <-> 16,15 <-> 23,16 <-> 17,16 <-> 22,17 <-> 18,18 <-> 19,19 <-> 20,19 <-> 21,21 <-> 22,23 <-> 24,24 <-> 25,25 <-> 26,26 <-> 27,26 <-> 33,27 <-> 28,28 <-> 29,28 <-> 30,30 <-> 31,31 <-> 32,31 <-> 33,34 <-> 35,34 <-> 39,35 <-> 36,36 <-> 37,37 <-> 38,38 <-> 39]
cialis = fromList 29 [0 <-> 1,1 <-> 2,1 <-> 18,2 <-> 3,3 <-> 4,3 <-> 5,5 <-> 6,5 <-> 17,6 <-> 7,6 <-> 20,7 <-> 8,7 <-> 15,8 <-> 9,9 <-> 10,9 <-> 14,10 <-> 11,11 <-> 12,12 <-> 13,13 <-> 14,14 <-> 15,15 <-> 16,16 <-> 17,17 <-> 18,18 <-> 19,20 <-> 21,20 <-> 28,21 <-> 22,22 <-> 23,23 <-> 24,23 <-> 27,24 <-> 25,25 <-> 26,26 <-> 27,27 <-> 28]
strychnine = fromList 25 [0 <-> 1,1 <-> 2,1 <-> 18,2 <-> 3,3 <-> 4,3 <-> 16,4 <-> 5,5 <-> 6,6 <-> 7,7 <-> 8,7 <-> 15,8 <-> 9,9 <-> 10,9 <-> 13,10 <-> 11,11 <-> 12,12 <-> 13,12 <-> 17,12 <-> 24,13 <-> 14,14 <-> 15,15 <-> 16,16 <-> 17,17 <-> 18,18 <-> 19,19 <-> 20,19 <-> 24,20 <-> 21,21 <-> 22,22 <-> 23,23 <-> 24]
cocaine = fromList 22 [0 <-> 1,1 <-> 2,2 <-> 3,2 <-> 4,4 <-> 5,4 <-> 10,5 <-> 6,5 <-> 20,6 <-> 7,7 <-> 8,8 <-> 9,8 <-> 20,9 <-> 10,10 <-> 11,11 <-> 12,12 <-> 13,12 <-> 14,14 <-> 15,14 <-> 19,15 <-> 16,16 <-> 17,17 <-> 18,18 <-> 19,20 <-> 21]
quinine = fromList 24 [0 <-> 1,1 <-> 2,2 <-> 3,2 <-> 23,3 <-> 4,4 <-> 5,5 <-> 6,5 <-> 22,6 <-> 7,7 <-> 8,8 <-> 9,9 <-> 10,9 <-> 22,10 <-> 11,10 <-> 12,12 <-> 13,12 <-> 17,13 <-> 14,14 <-> 15,14 <-> 19,15 <-> 16,16 <-> 17,17 <-> 18,18 <-> 19,19 <-> 20,20 <-> 21,22 <-> 23]
lsd = fromList 24 [0 <-> 1,1 <-> 2,2 <-> 3,2 <-> 5,3 <-> 4,5 <-> 6,5 <-> 7,7 <-> 8,7 <-> 22,8 <-> 9,9 <-> 10,9 <-> 11,11 <-> 12,11 <-> 21,12 <-> 13,13 <-> 14,13 <-> 23,14 <-> 15,15 <-> 16,16 <-> 17,16 <-> 23,17 <-> 18,18 <-> 19,19 <-> 20,20 <-> 21,20 <-> 23,21 <-> 22]
morphine = fromList 21 [0 <-> 1,1 <-> 2,1 <-> 11,2 <-> 3,3 <-> 4,4 <-> 5,4 <-> 8,4 <-> 12,5 <-> 6,5 <-> 15,6 <-> 7,7 <-> 8,7 <-> 19,8 <-> 9,9 <-> 10,9 <-> 17,10 <-> 11,11 <-> 12,12 <-> 13,13 <-> 14,14 <-> 15,15 <-> 16,17 <-> 18,18 <-> 19,19 <-> 20]
heroine = fromList 27 [0 <-> 1,1 <-> 2,1 <-> 11,2 <-> 3,3 <-> 4,4 <-> 5,4 <-> 8,4 <-> 12,5 <-> 6,5 <-> 15,6 <-> 7,7 <-> 8,7 <-> 22,8 <-> 9,9 <-> 10,9 <-> 20,10 <-> 11,11 <-> 12,12 <-> 13,13 <-> 14,14 <-> 15,15 <-> 16,16 <-> 17,17 <-> 18,17 <-> 19,20 <-> 21,21 <-> 22,22 <-> 23,23 <-> 24,24 <-> 25,24 <-> 26]
nicotine = fromList 12 [0 <-> 1,1 <-> 2,1 <-> 5,2 <-> 3,3 <-> 4,4 <-> 5,5 <-> 6,6 <-> 7,6 <-> 11,7 <-> 8,8 <-> 9,9 <-> 10,10 <-> 11]
caffeine = fromList 14 [0 <-> 1,1 <-> 2,1 <-> 13,2 <-> 3,3 <-> 4,4 <-> 5,4 <-> 13,5 <-> 6,5 <-> 7,7 <-> 8,7 <-> 9,9 <-> 10,9 <-> 11,11 <-> 12,11 <-> 13]
vitamine_a = fromList 21 [0 <-> 1,1 <-> 2,1 <-> 5,2 <-> 3,3 <-> 4,5 <-> 6,6 <-> 7,7 <-> 8,8 <-> 9,8 <-> 10,10 <-> 11,11 <-> 12,12 <-> 13,12 <-> 18,13 <-> 14,13 <-> 15,15 <-> 16,16 <-> 17,17 <-> 18,18 <-> 19,18 <-> 20]
|
stefan-hoeck/labeled-graph
|
Data/Graph/Graphs.hs
|
Haskell
|
bsd-3-clause
| 6,901
|
module Type (
typeOf
) where
import Context
import Base
typeOf :: Term -> TermType
typeOf t = go makeEmptyContext t
go :: Context -> Term -> TermType
{- T-True -}
go _ TermTrue = TypeBool
{- T-False -}
go _ TermFalse = TypeBool
{- T-If -}
go ctx (TermIfThenElse t t1 t2) = case go ctx t of
TypeBool -> if ty1 == ty2
then ty1
else error "type error: arms of conditional have different types"
_ -> error "type error: guard of conditional not a boolean"
where
ty1 = go ctx t1
ty2 = go ctx t2
{- T-Var -}
go ctx (TermVar var) = snd (indexToName ctx var)
{- T-Abs -}
go ctx (TermAbs name vty t) = TypeArrow vty tty
where
ctx' = addName ctx name vty
tty = go ctx' t
{- T-App -}
go ctx (TermApp t1 t2) = case ty1 of
TypeArrow ty11 ty12 -> if ty2 == ty11 then ty12 else error "type error: parameter type mismatch"
_ -> error "type error: arrow type expected"
where
ty1 = go ctx t1
ty2 = go ctx t2
|
foreverbell/unlimited-plt-toys
|
tapl/simplebool/Type.hs
|
Haskell
|
bsd-3-clause
| 970
|
{-# LANGUAGE OverloadedStrings, CPP #-}
module RoundTripSpec where
import Control.Monad (replicateM)
import Data.IP (IP (..), IPv4, IPv6, toIPv4, toIPv6)
import qualified Data.ByteString.Char8 as BS
import Network.DNS.Internal
import Network.DNS.Decode
import Network.DNS.Encode
import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck (Gen, arbitrary, choose, elements, forAll, frequency, listOf, oneof)
import Data.Word (Word8, Word16, Word32)
import Data.Monoid ((<>))
#if __GLASGOW_HASKELL__ < 709
import Control.Applicative
#endif
spec :: Spec
spec = do
prop "IPv4" . forAll genIPv4 $ \ ip4 -> do
let str = show ip4
read str `shouldBe` ip4
show (read str :: IPv4) `shouldBe` str
prop "IPv6" . forAll genIPv6 $ \ ip6 -> do
let str = show ip6
read str `shouldBe` ip6
show (read str :: IPv6) `shouldBe` str
prop "TYPE" . forAll genTYPE $ \ t ->
intToType (typeToInt t) `shouldBe` t
prop "Domain" . forAll genDomain $ \ dom -> do
let bs = encodeDomain dom
decodeDomain bs `shouldBe` Right dom
fmap encodeDomain (decodeDomain bs) `shouldBe` Right bs
prop "DNSFlags" . forAll genDNSFlags $ \ flgs -> do
let bs = encodeDNSFlags flgs
decodeDNSFlags bs `shouldBe` Right flgs
fmap encodeDNSFlags (decodeDNSFlags bs) `shouldBe` Right bs
prop "ResourceRecord" . forAll genResourceRecord $ \ rr -> do
let bs = encodeResourceRecord rr
decodeResourceRecord bs `shouldBe` Right rr
fmap encodeResourceRecord (decodeResourceRecord bs) `shouldBe` Right bs
prop "DNSHeader" . forAll genDNSHeader $ \ hdr ->
decodeDNSHeader (encodeDNSHeader hdr) `shouldBe` Right hdr
prop "DNSMessage" . forAll genDNSMessage $ \ msg ->
decode (encode msg) `shouldBe` Right msg
----------------------------------------------------------------
genDNSMessage :: Gen DNSMessage
genDNSMessage =
DNSMessage <$> genDNSHeader <*> listOf genQuestion <*> listOf genResourceRecord
<*> listOf genResourceRecord <*> listOf genResourceRecord
genQuestion :: Gen Question
genQuestion = do
typ <- genTYPE
dom <- genDomain
pure $ Question dom typ
genTYPE :: Gen TYPE
genTYPE = frequency
[ (20, elements
[ A, AAAA, NS, TXT, MX, CNAME, SOA, PTR, SRV, DNAME, OPT, DS, RRSIG
, NSEC, DNSKEY, NSEC3, NSEC3PARAM, TLSA, CDS, CDNSKEY, CSYNC
])
, (1, intToType <$> genWord16)
]
genResourceRecord :: Gen ResourceRecord
genResourceRecord = frequency
[ (8, genRR)
-- fixme: Add this back in when it works.
, (0, genOptRecord)
]
where
genRR = do
dom <- genDomain
t <- elements [A , AAAA, NS, TXT, MX, CNAME, SOA, PTR, SRV, DNAME, DS]
ResourceRecord dom t <$> genWord32 <*> mkRData dom t
genRDataOpt = do
odata <- listOf genOData
pure $ ResourceRecord "" OPT (fromIntegral $ length odata) (RD_OPT odata)
genOptRecord = do
dom <- genDomain
t <- genTYPE
OptRecord <$> genWord16 <*> genBool <*> genWord8 <*> mkRData dom t
mkRData :: Domain -> TYPE -> Gen RData
mkRData dom typ =
case typ of
A -> RD_A <$> genIPv4
AAAA -> RD_AAAA <$> genIPv6
NS -> pure $ RD_NS dom
TXT -> RD_TXT <$> genByteString
MX -> RD_MX <$> genWord16 <*> genDomain
CNAME -> pure $ RD_CNAME dom
SOA -> RD_SOA dom <$> genDomain <*> genWord32 <*> genWord32 <*> genWord32 <*> genWord32 <*> genWord32
PTR -> RD_PTR <$> genDomain
SRV -> RD_SRV <$> genWord16 <*> genWord16 <*> genWord16 <*> genDomain
DNAME -> RD_DNAME <$> genDomain
DS -> RD_DS <$> genWord16 <*> genWord8 <*> genWord8 <*> genByteString
TLSA -> RD_TLSA <$> genWord8 <*> genWord8 <*> genWord8 <*> genByteString
_ -> pure . RD_TXT $ "Unhandled type " <> BS.pack (show typ)
genIPv4 :: Gen IPv4
genIPv4 = toIPv4 <$> replicateM 4 (fromIntegral <$> genWord8)
genIPv6 :: Gen IPv6
genIPv6 = toIPv6 <$> replicateM 8 (fromIntegral <$> genWord16)
genOData :: Gen OData
genOData = oneof
[ genOD_Unknown
, OD_ClientSubnet <$> genWord8 <*> genWord8 <*> oneof [ IPv4 <$> genIPv4, IPv6 <$> genIPv6 ]
]
where
genOD_Unknown = do
bs <- genByteString
pure $ OD_Unknown (fromIntegral $ BS.length bs) bs
genByteString :: Gen BS.ByteString
genByteString = elements
[ "", "a", "a.b", "abc", "a.b.c" ]
genDomain :: Gen Domain
genDomain = do
bs <- genByteString
pure $ bs <> "."
genDNSHeader :: Gen DNSHeader
genDNSHeader = DNSHeader <$> genWord16 <*> genDNSFlags
genDNSFlags :: Gen DNSFlags
genDNSFlags =
DNSFlags <$> genQorR <*> genOPCODE <*> genBool <*> genBool
<*> genBool <*> genBool <*> genRCODE <*> genBool
genWord16 :: Gen Word16
genWord16 = arbitrary
genWord32 :: Gen Word32
genWord32 = arbitrary
genWord8 :: Gen Word8
genWord8 = arbitrary
genBool :: Gen Bool
genBool = elements [True, False]
genQorR :: Gen QorR
genQorR = elements [minBound .. maxBound]
genOPCODE :: Gen OPCODE
genOPCODE = elements [minBound .. maxBound]
genRCODE :: Gen RCODE
genRCODE = elements [minBound .. maxBound]
|
greydot/dns
|
test/RoundTripSpec.hs
|
Haskell
|
bsd-3-clause
| 5,178
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.