cse341_pl_partA_note_01

language:SML 

 

Three things we should focus:

  Syntax: how you write

  Semantics:

    - Type-checking ( before program run)

    - Evaluation (as program run)

 

ML:

    - 认为程序是由一系列bindings组成,binding是抽象和物理的结合。

    - 按顺序地在静态环境中check由之前产生的bindings的type,类型在静态环境中

    - 按顺序地在动态环境中evaluate由之前产生的bindings,value 在动态环境中

 

(* type-check 感觉和C差不多,但是语法上有差异,必须是binding,即一个name/variables bind a value, nothong else, 没有C中或者其他中存在声明和初始化,至于在内存中如何进行的, 和C内存声明和调用什么的,老师没讲,资料也少,留坑先 *)

 

Expressions:

  •   Vriables:
    • Syntax:  val name = e;      // 其中name和其他语言一样命名方式,但是 必须bind a value ,这里就是the reuslt of e, 而且,没有赋值,就是没有mutable or change a value, 类比Python,也是bind,但是Python中有可mutable data structure ,对于a=a+1, ML 中使用shadown 上一个动态环境,新建一个环境,假设之前a=1,那么ML对于a=a+1的处理就是 type-check a and 1都是int ,所以=左边的type 也是int ,对于evaluation , 就是先对“+”左边的subexpression evaluate ->1,右边->1,所以“=”左边的value 就是2,那么就把"="左边的variable name bind the vaule,里面最关键的地方就是这个动态产生的value是在current enviroment,而之前的是在之前的动态环境中,如果名字一样就shadown 了上面的了,至于具体内部如何实现这个shadown 我也不清楚。
    • Type-check: 在current static environment 中找,没有就报错
    • Evaluation: 在current dynamic enviroment中找,没有就报错
  •      Addtion:
    • Syntax: e1 + e2
    • Type-check: e1 and e2  must have the same type , which is the type of the value
    • Evaluate: evaluate e1 -> v1, evaluate e2 -> v2, the value is the sum of v1 and v2
  •      Values:
    • all values are expression,
    • a value evaluates to itself

 

 

神奇的语言:

    type, expression, binding -> write anything

    synatx, type-rule, evaluation-rule-> guide us the whole way

    没有其他(暂时我知道的)

    -mutation   just use new bindings instead 这个到和Python对一个unmutable data 比如int a=1 ,a=2 因为1不能变,所以a指向2,1还是存在的只是我们不指向它,但是对Python中的list是可变的,这里ML的元组什么的还没讲但是老师说没有mutation存在

    -statements   everything is an expression 其实就是说什么都是value,什么都可以用表达式表达,最后不久evaluate出一个value了嘛,Python C Java 都有statement吧

    -Loops  just  recursion

 

    

 

posted @ 2017-02-22 19:16  lixinnjupt  阅读(144)  评论(0编辑  收藏  举报