CSE271

CSE271: Principles of Programming Languages

Syntactic Sugar

Jooyong Yi (UNIST)

2024 Fall
CSE271

Multiple Arguments

let f = proc (x,y) -(x,y)
in (f (4, 3))
2024 Fall
CSE271

Extending the Language

  • Extend the concrete syntax
  • Extend the abstract syntax
  • Extend the parser
  • Extend the evaluator (value-of function)
2024 Fall
CSE271

Extending the Evaluator

(value-of (proc-exp (args 𝑣𝑎𝑟₁ 𝑣𝑎𝑟₂ ... 𝑣𝑎𝑟ₙ) 𝑏𝑜𝑑𝑦) ρ)  = (proc-val (procedure (𝑣𝑎𝑟₁ 𝑣𝑎𝑟₂ ... 𝑣𝑎𝑟ₙ) 𝑏𝑜𝑑𝑦 ρ))
  (value-of 𝑟𝑎𝑡𝑜𝑟 ρ₁) = (proc-val (procedure 𝑣𝑎𝑟 𝑏𝑜𝑑𝑦 ρ₂))
  (value-of 𝑟𝑎𝑛𝑑₁ ρ₁) = 𝑣𝑎𝑙₁
  (value-of 𝑟𝑎𝑛𝑑₂ ρ₁) = 𝑣𝑎𝑙₂
  ...
  (value-of 𝑟𝑎𝑛𝑑ₙ ρ₁) = 𝑣𝑎𝑙ₙ
----------------------------------------------------------------------------------------------
  (value-of (call-exp 𝑟𝑎𝑡𝑜𝑟 𝑟𝑎𝑛𝑑₁ 𝑟𝑎𝑛𝑑₂ ... 𝑟𝑎𝑛𝑑ₙ) ρ₁) = (value-of 𝑏𝑜𝑑𝑦 [𝑣𝑎𝑟₁=𝑣𝑎𝑙₁,...,𝑣𝑎𝑟ₙ=𝑣𝑎𝑙ₙ]ρ₂)
2024 Fall
CSE271

Extending the Language with Syntactic Sugar

  • Extend the concrete syntax
  • Extend the abstract syntax
  • Extend the parser
  • Extend the evaluator (value-of function)
2024 Fall
CSE271

Transformation

let f = proc (x,y) -(x,y)
in (f (4, 3))

let f = proc (x) proc (y) -(x,y)
in ((f 4) 3)
2024 Fall
CSE271
(value-of
 <<let f = proc (x) proc (y) -(x,y)
   in ((f 4) 3)>> ρ₁)
=
(value-of
 <<((f 4) 3)>> [f=(proc-val (procedure x <<proc (y) -(x,y)>> ρ₁))]ρ₁)   
(value-of (proc-exp 𝑣𝑎𝑟 𝑏𝑜𝑑𝑦) ρ)  = (proc-val (procedure 𝑣𝑎𝑟 𝑏𝑜𝑑𝑦 ρ))
  (value-of 𝑟𝑎𝑡𝑜𝑟 ρ₁) = (proc-val (procedure 𝑣𝑎𝑟 𝑏𝑜𝑑𝑦 ρ₂))
  (value-of 𝑟𝑎𝑛𝑑 ρ₁) = 𝑣𝑎𝑙
----------------------------------------------------------------
  (value-of (call-exp 𝑟𝑎𝑡𝑜𝑟 𝑟𝑎𝑛𝑑) ρ₁) = (value-of 𝑏𝑜𝑑𝑦 [𝑣𝑎𝑟=𝑣𝑎𝑙]ρ₂)
2024 Fall
CSE271
(value-of <<(f 4)>> [f=(proc-val (procedure x <<proc (y) -(x,y)>> ρ₁))]ρ₁)
=
(value-of <<proc (y) -(x,y)>> [x=4]ρ₁)
=
(proc-val (procedure y <<-(x,y)>> [x=4]ρ₁))
(value-of (proc-exp 𝑣𝑎𝑟 𝑏𝑜𝑑𝑦) ρ)  = (proc-val (procedure 𝑣𝑎𝑟 𝑏𝑜𝑑𝑦 ρ))
  (value-of 𝑟𝑎𝑡𝑜𝑟 ρ₁) = (proc-val (procedure 𝑣𝑎𝑟 𝑏𝑜𝑑𝑦 ρ₂))
  (value-of 𝑟𝑎𝑛𝑑 ρ₁) = 𝑣𝑎𝑙
----------------------------------------------------------------
  (value-of (call-exp 𝑟𝑎𝑡𝑜𝑟 𝑟𝑎𝑛𝑑) ρ₁) = (value-of 𝑏𝑜𝑑𝑦 [𝑣𝑎𝑟=𝑣𝑎𝑙]ρ₂)
2024 Fall
CSE271
(value-of
 <<((f 4) 3)>> [f=(proc-val (procedure x <<proc (y) -(x,y)>> ρ₁))]ρ₁)   
=
(value-of <<-(x,y)>> [y=3][x=4]ρ₁)
= 1
(value-of <<(f 4)>> [f=(proc-val (procedure x <<proc (y) -(x,y)>> ρ₁))]ρ₁)
=
(proc-val (procedure y <<-(x,y)>> [x=4]ρ₁))
  (value-of 𝑟𝑎𝑡𝑜𝑟 ρ₁) = (proc-val (procedure 𝑣𝑎𝑟 𝑏𝑜𝑑𝑦 ρ₂))
  (value-of 𝑟𝑎𝑛𝑑 ρ₁) = 𝑣𝑎𝑙
----------------------------------------------------------------
  (value-of (call-exp 𝑟𝑎𝑡𝑜𝑟 𝑟𝑎𝑛𝑑) ρ₁) = (value-of 𝑏𝑜𝑑𝑦 [𝑣𝑎𝑟=𝑣𝑎𝑙]ρ₂)
2024 Fall
CSE271

Syntactic Sugar

  • Syntactic sugar makes the language "sweeter" for humans to use.
let f = proc (x,y) -(x,y)
in (f (4, 3))

v.s.

let f = proc (x) proc (y) -(x,y)
in ((f 4) 3)
2024 Fall
CSE271

Syntactic Sugar

  • Syntactic sugar does not change the language's semantic expressiveness.
    • We did not extend the evaluator.
2024 Fall