以下代码:
template <class T1>
struct A1
{
template <int INDEX>
struct A2 { /* ... */ };
template <>
struct A2<-1> { /* ... */ };
};
int main()
{
A1<int>::A2<-1> x;
}
Run Code Online (Sandbox Code Playgroud)
给出了这个错误:
prog.cpp:7:13:错误:非命名空间范围的显式特化
'struct A1<T1>'prog.cpp:8:10:错误:部分特化中未使用的模板参数:
prog.cpp:8:10:错误:'T1'
如何最好地解决此错误?我试过这个:
template <class T1>
struct A1
{
template <int INDEX, class DUMMY = void>
struct A2 { /* ... */ };
template <class DUMMY>
struct A2<-1, DUMMY> { /* ... */ };
};
int main()
{
A1<int>::A2<-1> x;
}
Run Code Online (Sandbox Code Playgroud)
这似乎工作,但似乎有点软糖. …
考虑以下签名 foldMap
foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m
Run Code Online (Sandbox Code Playgroud)
这与"绑定"非常相似,只是交换了参数:
(>>=) :: Monad m => m a -> (a -> m b) -> m b
Run Code Online (Sandbox Code Playgroud)
在我看来,有因此必须有某种关系的Foldable,Monoid和Monad,但在超我找不到它.据推测,我可以将其中的一个或两个转换为另一个,但我不确定如何.
这种关系可以详细说明吗?
我注意到溪流看起来很像列表,除了不断添加时间.当然,添加常量时间附加到列表并不太复杂,DList就是这么做的.
让我们假设在剩下的讨论中,任何一个列表都有不变的时间附加,或者我们根本就不感兴趣.
我的想法是Haskell列表应该简单地实现为流.对于这种情况并非如此,我认为以下内容需要保留:
我的问题是:上述两种情况的例子是什么?
注意:出于这个问题的目的,请忽略我讨论过的特定实现中容易修复的遗漏.我在这里寻找核心结构差异.
附加信息:
我想我在这里得到的部分是说如果我们写[1..1000000],Haskell编译器(比如说GHC)会这样做:
如果是这种情况(1),为什么这样做,因为创建中间列表似乎是不必要的性能损失?
或者如果是这种情况(2),那么为什么我们需要流?
可以说我有两张桌子,tab_a而且tab_b.
我创建了如下视图:
create view join_tabs as
(
select col_x as col_z from tab_a
union
select col_y as col_z from tab_b
);
Run Code Online (Sandbox Code Playgroud)
如果我做以下事情:
select * from join_tabs where col_z = 'BLAH';
Run Code Online (Sandbox Code Playgroud)
如果tab_a索引col_x和tab_b索引col_y,我们应该能够通过两个索引搜索来执行此操作.
但是,如果我可以在一个索引中对两个表创建索引,甚至索引视图,那么如果源表(tab_a或tab_b)发生更改则会立即自动更新,这样会很好.
有没有办法在Oracle中执行此操作?
以下代码:
data HelloWorld = HelloWorld;
instance Show HelloWorld where show _ = "hello world";
hello_world = "hello world"
main = putStr $ show $ (HelloWorld, hello_world)
Run Code Online (Sandbox Code Playgroud)
打印:
(hello world,"hello world")
Run Code Online (Sandbox Code Playgroud)
我想要它打印:
(hello world,hello world)
Run Code Online (Sandbox Code Playgroud)
即我想要的行为如下:
f "hello world" = "hello world"
f HelloWorld = "hello world"
Run Code Online (Sandbox Code Playgroud)
不幸的是,show不满足于此,因为:
show "hello world" = "\"hello world\""
Run Code Online (Sandbox Code Playgroud)
有没有像f我上面描述的那样工作的功能?
我可以,如果是,我该如何编写函数的类型签名:
g f x y = (f x, f y)
Run Code Online (Sandbox Code Playgroud)
这样给出:
f1 :: a -> [a]
f1 x = [x]
x1 :: Int
x1 = 42
c1 :: Char
c1 = 'c'
f2 :: Int -> Int
f2 x = 3 * x
x2 :: Int
x2 = 5
Run Code Online (Sandbox Code Playgroud)
这样:
g f1 x1 c1 == ([42], ['c']) :: ([Int], [Char])
g f2 x1 x2 == (126, 15) :: (Int, Int)
Run Code Online (Sandbox Code Playgroud) 鉴于以下定义:
import Control.Monad.ST
import Data.STRef
fourty_two = do
x <- newSTRef (42::Int)
readSTRef x
Run Code Online (Sandbox Code Playgroud)
以下在GHC下编译:
main = (print . runST) fourty_two -- (1)
Run Code Online (Sandbox Code Playgroud)
但这不是:
main = (print . runST) $ fourty_two -- (2)
Run Code Online (Sandbox Code Playgroud)
但随后bdonlan在评论中指出,这确实编译:
main = ((print . runST) $) fourty_two -- (3)
Run Code Online (Sandbox Code Playgroud)
但是,这不编译
main = (($) (print . runST)) fourty_two -- (4)
Run Code Online (Sandbox Code Playgroud)
这似乎表明(3)只是由于中缀的特殊处理而编译$,但是,它仍然没有解释为什么(1)编译.
问题:
1)我已经阅读了以下两个问题(第一,第二),并且我被引导相信$只能用单态类型实例化.但我同样假设.只能用单态类型进行实例化,结果同样会失败.为什么第一个代码成功但第二个代码没有?(例如,GHC对第一种情况有特殊规则,它不能在第二种情况下适用吗?)
2)是否有当前的GHC扩展编译第二个代码?(也许ImpredicativePolymorphism在某些时候这样做了,但它似乎已被弃用,有什么东西取而代之吗?)
3)有没有办法定义说`my_dollar`使用GHC扩展做什么$,但也能够处理多态类型,所以(print . runST) `my_dollar` …
这是一个代码,它将两个三联的未装箱的单词添加到一个新的三个未装箱的单词中,并且还返回任何溢出:
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
import GHC.Prim(plusWord2#, Word#, or#)
longAdd ::
(# Word#, Word#, Word# #) ->
(# Word#, Word#, Word# #) ->
(# Word#, (# Word#, Word#, Word# #) #)
longAdd (# xl, xm, xh #) (# yl, ym, yh #) =
let
plusWord3 x y c =
let
(# c1, r1 #) = plusWord2# x y
(# c2, r2 #) = plusWord2# r1 c
in
(# plusWord# c1 c2, r2 #)
(# cl, rl …Run Code Online (Sandbox Code Playgroud) 我希望能够在没有完整函数调用的开销的情况下从Haskell调用LLVM代码.例如:
-- Main.hs --
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE GHCForeignImportPrim #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE UnliftedFFITypes #-}
{-# LANGUAGE BangPatterns #-}
import GHC.Exts(Word(W#))
import GHC.Prim(Word#)
foreign import ccall llvminc :: Word# -> Word#
main = do
line1 <- getLine
let !(W# x1) = read line1
let !r1 = llvminc x1
print (W# r1)
-- funcs.ll --
define fastcc i64 @llvminc(i64 inreg %x) {
%r = add i64 %x, 1
ret i64 %r
} …Run Code Online (Sandbox Code Playgroud) 我注意到:
chmod -R a+x 为所有文件添加执行权限,而不仅仅是当前可执行文件.
有没有办法只将执行权限添加到那些已经拥有用户权限执行集的文件中?