Wednesday, May 03, 2006

Links - Programming Language for Web application

เมื่อวันก่อนอ่านเจอ paper Links
คำอธิบายย่อๆ ก็คือ
Link คือ Programming Language ที่ออกแบบมาสำหรับการเขียน Web application
โดยเขามองว่า เดิมที่เราเขียน web application นั้น
เราต้องใช้อย่างน้อย 3 ภาษา ก็คือ sql สำหรับ access database
, javascript + HTML สำหรับส่วน UI
สุดท้ายก็ programming language สำหรับส่วน
business logic ซึ่งอาจจะเป็น c, java, python ฯลฯ
ซึ่งคนที่จะเขียน Web Application ต้องใช้เวลาในการเรียนรู้ทั้ง 3 ส่วน
และยังเกิดปัญหาที่เรียกว่า impedance mismatch
(ข้อมูลจาก sql แปลงให้อยู่ในรูป object เพื่อให้เหมาะกับ java
จากนั้นเมื่อจะ render ก็แปลงกลับมาอยู่ในรูปตาราง html )

Links ก็เลยเกิดมา เพื่อเป็น unify language สำหรับ
การเขียน Web application

ลองดูตัวอย่าง program เขาดู จะได้เห็นภาพ
การทำงานของโปรแกรมนี้ ก็คือ
เริ่มต้นมันจะแสดง form ให้เราใส่ตัวเลขที่ต้องการ
เมื่อสั่ง submit ก็จะแสดงผลเป็นตาราง factorial ตามจำนวนที่เราใส่

fun request(s) client {
dom!Document(
<html>
<body>
<h1>Please type a number</h1>
<form l:onsubmit="{response(t)}" l:onkeyup="{request(t)}">
<input type="text" value="{s}" l:name="t"/>
{
if (is_integer(s))
<input type="submit"/>
else
<input type="submit" disabled="disabled"/>
}
</form>
</body>
</html>
)
}

fun response(s) client {
var n = int_of_string(s);
dom!Document(
<html>
<body>
<h1>Factorials up to {enxml(string_of_int(n))}</h1>
<table>{
for ((i,f) <- factorialsUpto(n))
<tr>
<td>{enxml(string_of_int(i))}</td>
<td>{enxml(string_of_int(f))}</td>
</tr>
}</table>
</body>
</html>
)
}

fun factorialsUpto(n) server {
for (row <- Table "factorials"
with { i : Int, f : Int }
order [i : asc]
from database "postgresql:factorials::5432:www-data:")
where (row.i <= n)
[(row.i,row.f)]
}

request("")

จะเห็นว่า ตัวภาษามีลักษณะเป็น functional language
และ function แต่ละ function จะต้องกำหนดว่า
run อยู่บน client หรือ run บน server
ประเด็นที่น่าสนใจ ก็คือ
  • จะเห็นว่า function สามารถ call ข้าม space กันได้ (client -> server)
    โดย technique ที่เขาใช้ implement สำหรับการ call แบบนี้ก็คือ
    XMLHttpRequest (Ajax)
  • html integrate เข้าเป็นเนื้อเดียวกับ language
  • Style โปรแกรมมีลักษณะเป็น Continuations
    (ผมกำลังหัดเขียน Web Application ด้วย PLT Scheme อยู่
    style การเขียนเหมือนกันมากเลย)



ทดลอง run demo ของ Links ได้ที่นี่ Links demos

Related link from Roti

No comments: