haskell

How do I represent a tuple in dhall?

╄→尐↘猪︶ㄣ 提交于 2020-01-02 01:36:06
问题 I would like to represent IPv4 addresses in dhall, so I can manage my host configurations. By default, this is held as Text; but that's clearly unsatisfactory as it allows any old text to slip through. I would like to keep these values as a 4-tuple of 8-bit values. I don't think that Dhall can allow this natively - the nearest I can see is a record of { a : Natural, b : Natural }, etc., but that's syntactically clunky and still allows for octet values outside of 0-255. Assuming that I can't

How can I express foldr in terms of foldMap for type-aligned sequences?

空扰寡人 提交于 2020-01-02 01:18:25
问题 I'm playing around with type-aligned sequences, and in particular I'm messing around with the idea of folding them. A foldable type-aligned sequence looks something like this: class FoldableTA fm where foldMapTA :: Category h => (forall b c . a b c -> h b c) -> fm a b d -> h b d foldrTA :: (forall b c d . a c d -> h b c -> h b d) -> h p q -> fm a q r -> h p r foldlTA :: ... It's pretty easy to implement foldrTA in terms of foldMapTA by first using foldMapTA to convert the sequence to a type

How can I extract this polymorphic recursion function?

折月煮酒 提交于 2020-01-02 01:18:08
问题 I'm doing so fairly fun stuff with GHC 7.8, but have ran in to a bit of a problem. I have the following: mkResultF :: Eq k => Query kvs ('KV k v) -> k -> ResultF (Reverse kvs) (Maybe v) mkResultF Here key = ResultComp (pure . lookup key) mkResultF q@(There p) key = case mkResultF p key of ResultFId a -> pure a ResultComp c -> ResultComp $ \foo -> case c foo of ResultFId a -> pure a ResultComp c -> ResultComp $ \foo -> case c foo of ResultFId a -> pure a Clearly there is something to abstract

Is there a library or typeclass for getting the transformer version of a monad?

≯℡__Kan透↙ 提交于 2020-01-02 01:18:08
问题 In my current project I've run into the need to turn various monads into their transformer counterparts e.g. stateT :: Monad m => State s a -> StateT s m a stateT stf = StateT $ return . runState stf It's trivial to write these utility functions for the monads I need, but I was wondering if there already exists a library that contains this functionality for the standard monads and maybe a typeclass that abstracts this sort of transformation. Something like class (Monad f, MonadTrans t) =>

How can I encode and enforce legal FSM state transitions with a type system?

流过昼夜 提交于 2020-01-02 01:12:08
问题 Suppose I have a type Thing with a state property A | B | C , and legal state transitions are A->B, A->C, C->A . I could write: transitionToA :: Thing -> Maybe Thing which would return Nothing if Thing was in a state which cannot transition to A . But I'd like to define my type, and the transition functions in such a way that transitions can only be called on appropriate types. An option is to create separate types AThing BThing CThing but that doesn't seem maintainable in complex cases.

What is the mathematical significance of “all (==1) [1,1..]” not terminating?

∥☆過路亽.° 提交于 2020-01-02 01:12:08
问题 Intuitively, I would expect the "mathematical" answer to all (==1) [1,1..] to be True because all of the elements in a list that only contains 1s are equal to 1. However I understand that "computationally", the process of evaluating the infinite list in order to check that each element does in fact equal 1 will never terminate, therefore the expression instead "evaluates" to bottom or ⊥ . I find this result counter-intuitive and a little unnerving. I think the fact that the list is an

Getting all the diagonals of a matrix in Haskell

隐身守侯 提交于 2020-01-02 01:11:12
问题 The two-dimensional list looks like: 1 | 2 | 3 - - - - - 4 | 5 | 6 - - - - - 7 | 8 | 9 Or in pure haskell [ [1,2,3], [4,5,6], [7,8,9] ] The expected output for diagonals [ [1,2,3], [4,5,6], [7,8,9] ] is [ [1], [4, 2], [7, 5, 3], [8, 6], [9] ] Writing allDiagonals (to include anti-diagonals) is then trivial: allDiagonals :: [[a]] -> [[a]] allDiagonals xss = (diagonals xss) ++ (diagonals (rotate90 xss)) My research on this problem Similar question here at StackOverflow Python this question is

Illegal polymorphic or qualified type using RankNTypes and TypeFamilies

主宰稳场 提交于 2020-01-02 01:03:13
问题 I've been slowly working on porting the llvm package to use data kinds, type families and type-nats and ran into a minor issue when trying to remove the two newtypes used for classifying values ( ConstValue and Value ) by introducing a new Value type parameterized by its constness. CallArgs only accepts Value 'Variable a arguments and provides a function for casting a Value 'Const a to a Value 'Variable a . I'd like to generalize CallArgs to allow each argument to be either 'Const or

Could not find module ‘Text.Regex.Posix’

我们两清 提交于 2020-01-02 00:58:10
问题 I want to try doing regexes in GHCi. I tried loading the module :mod +Text.Regex.Posix But got this error instead <no location info>: Could not find module ‘Text.Regex.Posix’ It is not a module in the current program, or in any known package. But I should have Text installed ghc-pkg find-module Text.Regex.Posix would give me /usr/local/Cellar/ghc/7.8.4/lib/ghc-7.8.4/package.conf.d /Users/a/.ghc/x86_64-darwin-7.8.4/package.conf.d What do I do? I have no problem with this though: import Text

Manipulating the order of arguments to type constructors

半城伤御伤魂 提交于 2020-01-02 00:51:21
问题 I wrote something like this: instance Functor (Either e) where fmap _ (Left a) = Left a fmap f (Right b) = Right (f b) How do I do the same if I want fmap to change the value only if it's Left ? I mean, what syntax do I use to indicate that I use type Either _ b instead of Either a _ ? 回答1: I don't think there's a way to do that directly, unfortunately. With a function you can use flip to partially apply the second argument, but that doesn't work with type constructors like Either . The