Scala syntax/grammar 类型系统的例子?

Scala syntax/grammar of type system example?

我正在学习 scala。

在 scala 语法中:https://www.scala-lang.org/files/archive/spec/2.13/13-syntax-summary.html,我读到了 type system

  Type              ::=  FunctionArgTypes ‘=>’ Type
                      |  InfixType [ExistentialClause]
  FunctionArgTypes  ::= InfixType
                      | ‘(’ [ ParamType {‘,’ ParamType } ] ‘)’
  ExistentialClause ::=  ‘forSome’ ‘{’ ExistentialDcl {semi ExistentialDcl} ‘}’
  ExistentialDcl    ::=  ‘type’ TypeDcl
                      |  ‘val’ ValDcl
  InfixType         ::=  CompoundType {id [nl] CompoundType}
  CompoundType      ::=  AnnotType {‘with’ AnnotType} [Refinement]
                      |  Refinement
  AnnotType         ::=  SimpleType {Annotation}
  SimpleType        ::=  SimpleType TypeArgs
                      |  SimpleType ‘#’ id
                      |  StableId
                      |  Path ‘.’ ‘type’
                      |  ‘(’ Types ‘)’
  TypeArgs          ::=  ‘[’ Types ‘]’
  Types             ::=  Type {‘,’ Type}
  Refinement        ::=  [nl] ‘{’ RefineStat {semi RefineStat} ‘}’
  RefineStat        ::=  Dcl
                      |  ‘type’ TypeDef
                      |
  TypePat           ::=  Type

  Ascription        ::=  ‘:’ InfixType
                      |  ‘:’ Annotation {Annotation}
                      |  ‘:’ ‘_’ ‘*’

我可以举一些例子:

  1. StableIdIntStableId => SimpleType => AnnotType => CompoundType => InfixType => Type 时,我们将得到一个名为 IntType
  2. FunctionArgTypes()TypeInt时,我们将得到一个名为()=>IntType
  3. TypeArrayTypeArgs[Int],我们会得到SimpleType = Array[Int],那么TypeArray[Int].

我不明白某些类型系统:

  1. InfixType 是什么?
  2. CompoundType 是什么?
  3. Refinement 是什么?
  4. Ascription 是什么?
  5. 什么时候SimpleType会导出一个SimpleType '#' id,什么时候会导出'(' Types ')'

有没有 scala 类型系统的例子?

  1. 例如Int Op String中的Op是中缀类型

    trait Op[A, B]
    
    type T = Int Op String
    
  2. 例如Int with String with Boolean是复合类型

  3. 例如{ type X }

    中的类型细化
    trait MyTrait
    
    type T = MyTrait { type X }
    
  4. 例如1 : Int中的: Int是类型归属。

  5. 例如MyTrait#T是类型投影

    trait MyTrait {
      type T
    }
    

    类型

    type T = (Int, String, Boolean)
    

    是元组类型。