haskell

How to make specialized type classes for certain types, default implementation for the rest of types

 ̄綄美尐妖づ 提交于 2020-01-13 17:56:55
问题 I would like to have a type class of types that can possibly casted to other types when possible. class Castable a b where cast :: a -> Maybe b cast _ = Nothing -- default implementation Now the class would be implemented for some types and for all the others I would like to have default implementation. How one can do that? 回答1: It's not necessarily a safe or Haskell-y thing to do, but it is certainly possible, using OverlappingInstances First, enable them: {-# LANGUAGE MultiParamTypeClasses

How to make specialized type classes for certain types, default implementation for the rest of types

杀马特。学长 韩版系。学妹 提交于 2020-01-13 17:56:09
问题 I would like to have a type class of types that can possibly casted to other types when possible. class Castable a b where cast :: a -> Maybe b cast _ = Nothing -- default implementation Now the class would be implemented for some types and for all the others I would like to have default implementation. How one can do that? 回答1: It's not necessarily a safe or Haskell-y thing to do, but it is certainly possible, using OverlappingInstances First, enable them: {-# LANGUAGE MultiParamTypeClasses

how to instance Eq without deriving

浪尽此生 提交于 2020-01-13 13:09:25
问题 Sorry about my poor English. The title may not explain what I mean. In Data.Tree, Tree is defined as following: -- | Multi-way trees, also known as /rose trees/. data Tree a = Node { rootLabel :: a, -- ^ label value subForest :: Forest a -- ^ zero or more child trees } #ifdef __GLASGOW_HASKELL__ deriving (Eq, Read, Show, Data) #else deriving (Eq, Read, Show) #endif It uses deriving to instance == and /= for Tree (date). Could I do the same thing without deriving? I try things like this: data

how to instance Eq without deriving

妖精的绣舞 提交于 2020-01-13 13:09:11
问题 Sorry about my poor English. The title may not explain what I mean. In Data.Tree, Tree is defined as following: -- | Multi-way trees, also known as /rose trees/. data Tree a = Node { rootLabel :: a, -- ^ label value subForest :: Forest a -- ^ zero or more child trees } #ifdef __GLASGOW_HASKELL__ deriving (Eq, Read, Show, Data) #else deriving (Eq, Read, Show) #endif It uses deriving to instance == and /= for Tree (date). Could I do the same thing without deriving? I try things like this: data

Auto convert type in haskell

陌路散爱 提交于 2020-01-13 11:30:08
问题 I've written some helpful functions to do logical operation. It looks like (a and b or c) `belongs` x . Thanks to Num , IsList and OverloadedLists , I can have it for integers and lists. λ> (1 && 2 || 3) `belongs` [2] False λ> (1 && 2 || 3) `belongs` [1, 2] True λ> (1 && 2 || 3) `belongs` [3] True λ> :set -XOverloadedLists λ> ([1, 2] && [2, 3] || [3, 4]) `contains` 1 False λ> ([1, 2] && [2, 3] || [3, 4]) `contains` 2 True I do it this way: newtype BoolLike a = BoolLike ((a -> Bool) -> Bool) (

Haskell Netwire: wires of wires

∥☆過路亽.° 提交于 2020-01-13 11:23:36
问题 I'm playing around with the netwire package trying to get a feel for FRP, and I have a quick question. Starting with the following simple wires, I'm able to emit an event every 5 seconds (approx) myWire :: (Monad m, HasTime t s) => Wire s () m a Float myWire = timeF myWire' :: (Monad m, HasTime t s) => Wire s () m a Int myWire' = fmap round myWire myEvent :: (Monad m, HasTime t s) => Wire s () m a (Event Int) myEvent = periodic 5 . myWire' This is pretty nice and straight forward, but what I

connecting http-conduit to xml-conduit

蓝咒 提交于 2020-01-13 11:02:41
问题 I'm struggling converting a Response from http-conduit to an XML document via xml-conduit. The doPost function takes an XML Document and posts it to the server. The server responds with an XML Document. doPost queryDoc = do runResourceT $ do manager <- liftIO $ newManager def req <- liftIO $ parseUrl hostname let req2 = req { method = H.methodPost , requestHeaders = [(CI.mk $ fromString "Content-Type", fromString "text/xml" :: Ascii) :: Header] , redirectCount = 0 , checkStatus = \_ _ ->

connecting http-conduit to xml-conduit

一世执手 提交于 2020-01-13 11:02:10
问题 I'm struggling converting a Response from http-conduit to an XML document via xml-conduit. The doPost function takes an XML Document and posts it to the server. The server responds with an XML Document. doPost queryDoc = do runResourceT $ do manager <- liftIO $ newManager def req <- liftIO $ parseUrl hostname let req2 = req { method = H.methodPost , requestHeaders = [(CI.mk $ fromString "Content-Type", fromString "text/xml" :: Ascii) :: Header] , redirectCount = 0 , checkStatus = \_ _ ->

How can I write this pattern synonym without ambiguous type errors?

南笙酒味 提交于 2020-01-13 10:25:17
问题 Using ViewPatterns and Data.Typeable , I’ve managed to write a function that allows me to write something resembling case analysis on types. Observe: {-# LANGUAGE GADTs, PatternSynonyms, RankNTypes, ScopedTypeVariables , TypeApplications, TypeOperators, ViewPatterns #-} import Data.Typeable viewEqT :: forall b a. (Typeable a, Typeable b) => a -> Maybe ((a :~: b), b) viewEqT x = case eqT @a @b of Just Refl -> Just (Refl, x) Nothing -> Nothing evilId :: Typeable a => a -> a evilId (viewEqT @Int

How to install FTGL library on windows?

那年仲夏 提交于 2020-01-13 08:53:08
问题 I want to use the haskell FTGL binding, which need the FTGL library. There seems no binary for windows and I can't find any useful info on how to compile it. What's the easiest way to make it work? 回答1: FTGL's source download comes with visual studio solution files (.sln), which can be built with visual studio to product the c/C++ .lib files. They may be an older version of visual studio, but you can upgrade them which ever version you have. 来源: https://stackoverflow.com/questions/6264681/how