我们可以证明 Haskell 中涉及本机类型的同构吗?
import Prelude
newtype Leibniz a b = Leibniz {unLeibniz :: forall f. f a -> f b}
data One = One
-- `u` and `v` are inverse
u :: Bool -> Either One One
u b = if b then Right One else Left One
v :: Either One One -> Bool
v = \case
(Left _) -> False
(Right _) -> True
--- Can we prove proof that ?
p :: Leibniz Bool (Either One One) …Run Code Online (Sandbox Code Playgroud) 是否可以构造一个种类级别的身份,将一种类型映射a到 haskell 中的同类a?
以下是一些非答案:
type One :: * -> *
newtype One a = One {unOne :: a}
-- >>> :kind! 'One
-- 'One :: a -> One a -- not the identity
-- = 'One
Run Code Online (Sandbox Code Playgroud)
type family Id a where -- *pointwise* identity, not a function (can't not apply it)
Id a = a
Run Code Online (Sandbox Code Playgroud) 我使用Unquote并没有看到任何近似的内容.所以我决定写一个.
let inline (=~=) x y = abs x-y < 1.E-10
Run Code Online (Sandbox Code Playgroud)
然而,运营商没有映射到Lists上
let test = [1;2] =~= [1;2] //---> error
Run Code Online (Sandbox Code Playgroud)
是否可以声明此运算符流动如何(=)?
或者它需要定义像'StructuralEquality-ishness'这样的新特征?
使用http://code.google.com/p/fsharp-typeclasses/来定义新的运算符是否更好?
我似乎无法找到为什么这不起作用的参考:
- (2000,1)<(2000,1);
stdIn:18.1-18.18 Error: operator and operand don't agree [overload]
operator domain: 'Z * 'Z
operand: (int * int) * (int * int)
in expression:
(2000,1) < (2000,1)
Run Code Online (Sandbox Code Playgroud)
标准ML是否支持结构比较?
我有一系列项目,我想从中抽样.
我的印象是,Set会有一个好的结构来取样,在折叠中我会回复原始或修改后的元素,如果我想要替换不是,则将检索到的元素丢失.但是,似乎没有方法直接从Set检索元素.
有什么我想念的吗?或者我应该使用一组索引,以及一个随机开始的代理函数,position < Set.count直到它找到一个成员为止?
也就是说,沿着这条线
module Seq =
let modulo (n:int) start =
let rec next i = seq { yield (i + 1)%n ; yield! next (i+1)}
next start
module Array =
let Sample (withReplacement:bool) seed (entries:'T array) =
let prng, indexes = new Random(seed), Set(Seq.init (entries |> Array.length) id)
Seq.unfold (fun set -> let N = set |> Set.count
let next = Seq.modulo N (prng.Next(N)) |> Seq.truncate N |> Seq.tryFind(fun i -> set |> Set.exists …Run Code Online (Sandbox Code Playgroud) 当我们运行一个STM命中的表达式时retry,线程被阻塞,如果修改了条目,则再次运行该事务.
但我想知道:
如果我们读取一个STM变量,该变量在导致重试的特定分支中实际上没有使用,那么更新它会尝试再次执行该事务吗?
线程被阻止时,它是否真的被阻止了?或者它是否在线程池中回收以供其他可能等待的操作使用?
使用以下代码时,我收到下面的错误消息,以便递归使用 toInt
module Intro0 where
data Zero
data Succ n
class Nat n where
toInt :: n -> Int
instance Nat Zero where
toInt _ = 0
instance (Nat n) => Nat (Succ n) where
toInt _ = (1::Int) + toInt (undefined :: n)
Run Code Online (Sandbox Code Playgroud)
我会想象发现toInt是微不足道的,因为它是在递归实例的上下文中指定的,但是我得到了一个统一问题
Could not deduce (Nat n0) arising from a use of ‘toInt’
from the context (Nat n)
bound by the instance declaration
at EPhantomTypes.hs:10:10-32
The type variable ‘n0’ is ambiguous
Note: there are several potential instances: …Run Code Online (Sandbox Code Playgroud) 我们都知道并喜欢/讨厌从右到左的作文:
(.) :: (b -> c) -> (a -> b) -> a -> c
Run Code Online (Sandbox Code Playgroud)
什么是自然/从左到右组成的"最标准"运算符(如在某种公共库中):
(???) :: (a -> b) -> (b -> c) -> a -> c
Run Code Online (Sandbox Code Playgroud)
它概括了常识运算符的值,从()以下函数看作:
(&) :: a -> (a -> b) -> b
Run Code Online (Sandbox Code Playgroud)
一些图书馆提供
PS:如果没有,那么这个运营商名称的任何一方?在数学中,自然成分经常被写成";" 但是第二个最好的名字是什么?(&.)也许 ?
试图抽象一些我只需要Set我编写的操作的算法
use std::collections::HashSet;
trait Set<T> {
fn insert(self: &mut Self, item: T);
fn contains(self: &Self, item: T) -> bool;
}
impl<T> Set<T> for HashSet<T> {
fn insert(&mut self, item: T) {
self.insert(item);
}
fn contains(&self, item: T) -> bool {
self.contains(item)
}
}
impl<T> Set<T> for Vec<T> {
fn insert(&mut self, item: T) {
self.push(item);
}
fn contains(&self, item: T) -> bool {
self.contains(item)
}
}
Run Code Online (Sandbox Code Playgroud)
我在编译时收到有关递归的警告,而我“显然”不想递归,而是在实现者上使用底层实现。
function cannot return without recursing
cannot return without recursing
note: `#[warn(unconditional_recursion)]` …Run Code Online (Sandbox Code Playgroud) tokio有一个合并数据结构,允许“合并”两个同构流并忘记出处。
impl<T, U> Stream for Merge<T, U> where
T: Stream,
U: Stream<Item = T::Item>, { ...
Run Code Online (Sandbox Code Playgroud)
是否存在流的代数逐点标记联合,它从 的流a和 的流b生成 的流Either a b?
PS:我想答案是否定的,因为 Rust 显然没有标准的总和类型。