拆箱类型和严格性之间的关系是什么? [英] What is the relationship between unboxed types and strictness?

查看:110
本文介绍了拆箱类型和严格性之间的关系是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无盒类型,如 Int#和严格函数,如 f(!x)= ... ,是不同的,但我看到概念上的相似性 - 他们在某种程度上不允许thunk / laziness。如果Haskell是像Ocaml这样严格的语言,那么每个函数都将是严格的,每种类型都会被拆箱。什么是取消装箱类型和强制执行严格性之间的关系? >

要支持使用盒装对象,将构造函数编码到指向闭包的指针中。



与严格关系

通常情况下,函数式语言编译器会以临时方式生成非装箱值。然而,在Haskell中,取消装箱值是特殊的。他们:


  1. 他们有不同的类型,;

  2. 只能用在特殊的地方;和
  3. 它们未被提升,所以不会被指示为堆值的指针。因为它们没有被提升,所以它们必然是严格的。懒惰的表示是不可能的。所以特别的unboxed类型,比如 Int# Double#,真的在机器上表示为double或int(以C表示)。



    严格性分析

    另外,GHC也会执行严格分析的常规Haskell类型。如果一个值的使用被认为是严格的 - 即它永远不会是'未定义的 - 优化器可能会取代所有使用常规类型(例如 Int )与一个unboxed ( Int#),因为它知道 Int 的使用总是严格的,因此用更高效的替换(并且总是严格)类型 Int#是安全的。



    我们当然可以拥有严格的类型,例如,元素严格的多态列表:

     数据列表a =空|缺点!a(列表a)

    在其元素中是严格的,但并不表示它们是取消装箱的值。



    这也指出了你对严格语言所犯的错误,像OCaml 。他们仍然需要支持多态,所以他们提供一个统一的表示,或者他们专门为每种类型提供数据类型和函数。 GHC默认使用统一表示法,就像OCaml一样,尽管GHC也可以专用类型和函数现在(如C ++模板)。


    Unboxed types, like Int#, and strict functions, like f (!x) = ..., are something different, but I see conceptual similarity - they disallow thunks/laziness in some way. If Haskell was a strict language like Ocaml, every function would be strict and every type unboxed. What is the relationship between unboxed types and enforcing strictness?

    解决方案

    Unboxed vs Boxed Data

    To support parametric polymorphism and laziness, by default Haskell data types are represented uniformly as a pointer to a closure on the heap, with a structure like this:

    These are "boxed" values. An unboxed object is represented by the value itself directly, without any indirection or closure. Int is boxed, but Int# is unboxed.

    Lazy values require a boxed representation. Strict values do not: they can represented either as fully evaluated closures on the heap, or as primitive unboxed structures. Note that pointer tagging is an optimization that we can use on boxed objects, to encode the constructor in the pointer to the closure.

    The Relationship to Strictness

    Normally, unboxed values are generated in an ad hoc fashion by functional language compilers. In Haskell, however, unboxed values are special. They:

    1. they have a different kind, #;
    2. can only be used in special places; and
    3. they're unlifted, so are not represented as a pointer to a heap value.

    Because they are unlifted they are necessarily strict. The representation of laziness is not possible.

    So particular unboxed types, like Int#, Double#, really are represented just as double or int on the machine (in C notation).

    Strictness Analysis

    Separately, GHC does strictness analysis of regular Haskell types. If a value's use is found to be strict – i.e. it can never be 'undefined' – the optimizer might replace all uses of the regular type (e.g. Int) with an unboxed one (Int#), since it knows that the use of Int is always strict, and thus replacement with the more efficient (and always strict) type Int# is safe.

    We can of course have strict types without unboxed types, for example, an element-strict polymorphic list:

    data List a = Empty | Cons !a (List a)
    

    is strict in its elements, but does not represent them as unboxed values.

    This also points out the mistake you made about strict languages, like OCaml. They still need to support polymorphism, so either they provide a uniform representation, or they specialize data types and functions to every type. GHC by default uses uniform representation, as does OCaml, though GHC can also specialize types and functions now (like C++ templates).

    这篇关于拆箱类型和严格性之间的关系是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆