【问题标题】:How are declarations in a SML program separated?SML 程序中的声明是如何分隔的?
【发布时间】:2020-08-17 00:51:07
【问题描述】:

SML's grammar

程序

prog ::=   dec core declaration
           functor fctbind functor declaration
           signature sigbind signature declaration
           empty
           prog1 ⟨;⟩ prog2 sequence

fctbind ::= id1 ( id2 : sig ) ⟨:⟨>⟩ sig⟩ = str ⟨and fctbind⟩    plain
id ( spec ) ⟨:⟨>⟩ sig⟩ = str ⟨and fctbind⟩ opened
sigbind    ::= id = sig ⟨and sigbind⟩ signature

为什么

val a = 1
val b = 2;

a
b

ab 之间有错误,但两个 val 声明之间没有?

$sml < main.sml
Standard ML of New Jersey v110.78 [built: Thu Aug 31 03:45:42 2017]
- val a = 1 : int
val b = 2 : int
= stdIn:4.1-5.2 Error: operator is not a function [tycon mismatch]
  operator: int
  in expression:
    a b

谢谢。

【问题讨论】:

    标签: sml ml


    【解决方案1】:

    val a = 1
    val b = 2
    

    你所做的只是绑定两个变量,对应规则 dec ::= val ⟨var⟩(,) valbinddec ::= dec1 ⟨;⟩ dec2,在顶层, prog ::= dec.

    然后在

    a
    b
    

    您实际上是在尝试应用 ab (exp ::= exp1 exp2 (application))。将其视为等效编写可能更简单

    a b
    

    但是,a 没有函数类型,因此出现错误 operator is not a function。目前尚不清楚您实际尝试使用 ab 做什么。

    【讨论】:

      猜你喜欢
      • 2020-08-16
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-21
      • 2020-12-12
      • 1970-01-01
      相关资源
      最近更新 更多