今天我想知道有多少素数符合这个范围Word8,但这个最微不足道的任务给了我一个意想不到的...... 没有结果.
? import Data.Numbers.Primes
? import Data.Word
? takeWhile (<= (maxBound :: Word8)) primes
[2,3,5,7,11,13,17,19,23,25,29,31,35,37,41,43,47,49,53,55,59,61,71,73,
77,79,83,85,89,95,97,101,103,107,109,115,119,121,125,127,133,137,139,
145,149,151,157,161,163,167,173,175,179,185,187,191,197,199,203,205,
209,215,217,223,235^CInterrupted.
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,此评估不仅不会终止,而且所产生的数字也不是全部!
我继续围着这个案子跳舞:
? maxBound :: Word8
255
? takeWhile (<= 255) primes
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,
97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181
,191,193,197,199,211,223,227,229,233,239,241,251]
? takeWhile (<= (maxBound :: Word8)) [1..]
[1,2,3, {- ..., -} 253,254,255]
-- This of course works, so I do not present the consecutive
-- natural numbers inbetween 3 and 253.
? takeWhile (<= 255) $ take 255 primes
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,
97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181
,191,193,197,199,211,223,227,229,233,239,241,251]
? …Run Code Online (Sandbox Code Playgroud) 考虑一下最近一个问题中的一个简单函数:
myButLast :: [a] -> a
myButLast [x, y] = x
myButLast (x : xs) = myButLast xs
myButLast _ = error "List too short"
Run Code Online (Sandbox Code Playgroud)
我们可以要求GHC给我们提供的简化输出ghc -ddump-simpl。(可能有一些额外的标志一样-dsuppress-module-prefixes -dsuppress-uniques)。据我了解,这是编译的最后阶段,结果仍然有任何外表到原来的高级代码。所以这就是它的意思:
-- RHS size: {terms: 21, types: 22, coercions: 0, joins: 0/0}
myButLast :: forall a. [a] -> a
[GblId,
Arity=1,
Str=<S,1*U>,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True, Guidance=IF_ARGS [30] 100 0}]
myButLast
= \ (@ a) (ds :: [a]) ->
case ds …Run Code Online (Sandbox Code Playgroud) 固定点是一个函数的参数,它将返回不变:f x == x.一个例子是(\x -> x^2) 1 == 1- 这里固定点是1.
有吸引力的固定点是那些可以通过从某个起点迭代找到的固定点.例如,(\x -> x^2) 0.5会收敛到0,因此0是该函数的有吸引力的固定点.
幸运的是,通过从该点迭代函数,可以从合适的非固定点接近(并且在某些情况下,甚至在那么多步骤中达到)有吸引力的固定点.其他时候,迭代会发散,所以首先应该有一个证据表明固定点会吸引迭代过程.对于某些功能,证明是常识.
我已经整理了一些能够整齐地完成任务的现有技术.然后我开始将相同的想法扩展到monadic函数,但没有运气.这是我现在的代码:
module Fix where
-- | Take elements from a list until met two equal adjacent elements. Of those,
-- take only the first one, then be done with it.
--
-- This function is intended to operate on infinite lists, but it will still
-- work on finite ones.
converge :: Eq …Run Code Online (Sandbox Code Playgroud) 我有这个F-Algebra(在之前的一个问题中介绍过),我想对它进行有效的代数.通过绝望的审判,我设法将一个有效的monadic catamorphism放在一起.我想知道它是否可以推广到一个应用程序,如果不是,为什么.
这就是我定义的方式Traversable:
instance Traversable Expr where
traverse f (Branch xs) = fmap Branch $ traverse f xs
traverse f (Leaf i ) = pure $ Leaf i
Run Code Online (Sandbox Code Playgroud)
这是monadic catamorphism:
type AlgebraM a f b = a b -> f b
cataM :: (Monad f, Traversable a) => AlgebraM a f b -> Fix a -> f b
cataM f x = f =<< (traverse (cataM f) . unFix $ x)
Run Code Online (Sandbox Code Playgroud)
这就是它的工作原理:
? let printAndReturn x …Run Code Online (Sandbox Code Playgroud) 考虑这个代码:
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeFamilyDependencies #-}
module Study where
class C a where
type T a = r | r -> a
pred :: T a -> Bool
pred _ = True
Run Code Online (Sandbox Code Playgroud)
我想有一个更有意义的默认定义pred,像这样:
class C' a where
...
pred' = not . null
Run Code Online (Sandbox Code Playgroud)
(我想默认 T' a 是 = [a]。)
有办法吗?
考虑zip由Peano数字索引的通常矢量长度的这个定义:
{-# language DataKinds #-}
{-# language KindSignatures #-}
{-# language GADTs #-}
{-# language TypeOperators #-}
{-# language StandaloneDeriving #-}
{-# language FlexibleInstances #-}
{-# language FlexibleContexts #-}
module Vector
where
import Prelude hiding (zip)
data N
where
Z :: N
S :: N -> N
data Vector (n :: N) a
where
VZ :: Vector Z a
(:::) :: a -> Vector n a -> Vector (S n) a
infixr 1 :::
deriving instance Show a …Run Code Online (Sandbox Code Playgroud) 考虑:
? :type (+) @Integer
(+) @Integer :: Integer -> Integer -> Integer
? :type (\x y -> x + y) @Integer
...
...error...
...Cannot apply expression...
...
? f = (+)
? :type f @Integer
...
...error...
...Cannot apply expression...
...
? g = \x y -> x + y
? :type g @Integer
...
...error...
...Cannot apply expression...
...
? h x y = x + y
? :type h @Integer
...
...error...
...Cannot apply expression...
...
Run Code Online (Sandbox Code Playgroud)
同时: …
我试图牢固地了解异常,以便改善条件循环的实现。为此,我正在进行各种实验,扔东西,看看有什么被发现。
这让我惊讶不已:
% cat X.hs
module Main where
import Control.Exception
import Control.Applicative
main = do
throw (userError "I am an IO error.") <|> print "Odd error ignored."
Run Code Online (Sandbox Code Playgroud)
% ghc X.hs && ./X
...
X: user error (I am an IO error.)
Run Code Online (Sandbox Code Playgroud)
% cat Y.hs
module Main where
import Control.Exception
import Control.Applicative
main = do
throwIO (userError "I am an IO error.") <|> print "Odd error ignored."
Run Code Online (Sandbox Code Playgroud)
% ghc Y.hs && ./Y
...
"Odd error ignored."
Run Code Online (Sandbox Code Playgroud)
我认为替代方案应该完全忽略IO错误。(不知道我是从哪里得到这个想法的,但是我当然不能提供在替代链中将被忽略的非IO异常。)因此,我认为自己可以手工制作并传递IO错误。事实证明,是否忽略它取决于包装和内容:如果我 …
考虑以下示例:
{-# language ApplicativeDo #-}
module X where
data Tuple a b = Tuple a b deriving Show
instance Functor (Tuple a) where
fmap f (Tuple x y) = Tuple x (f y)
instance Foldable (Tuple a) where
foldr f z (Tuple _ y) = f y z
instance Traversable (Tuple a) where
traverse f (Tuple x y) = do
y' <- f y
let t' = Tuple x y'
return $ t'
Run Code Online (Sandbox Code Playgroud)
看起来不错!但不是:
[1 of 1] Compiling X ( …Run Code Online (Sandbox Code Playgroud) Curry的悖论 (以与当前编程语言相同的人命名)是一种错误逻辑的构造,它允许人们证明任何东西。
我对逻辑一无所知,但是有多难?
module Main where
import Data.Void
import Data.Function
data X = X (X -> Void)
x :: X
x = fix \(X f) -> X f
u :: Void
u = let (X f) = x in f x
main :: IO ()
main = u `seq` print "Done!"
Run Code Online (Sandbox Code Playgroud)
它肯定会循环。(GHC如何知道?!)
% ghc -XBlockArguments Z.hs && ./Z
[1 of 1] Compiling Main ( Z.hs, Z.o )
Linking Z ...
Z: <<loop>>
Run Code Online (Sandbox Code Playgroud)
fix递归的情况下做同样的事情吗?为什么?haskell ×10
types ×2
applicative ×1
catamorphism ×1
compilation ×1
curry-howard ×1
do-notation ×1
exception ×1
ghc ×1
induction ×1
logic ×1
monads ×1
paradox ×1
recursion ×1