#Lambda 一個弱型別的靜態語言

1 messages · Page 1 of 1 (latest)

idle seal
#

Homo你是不是住在線上

#

我一發你就按表情

sturdy swift
#

不過為什麼要弱型別w

open crag
elder sparrow
#

看了下只有grammar definition?

fervent vapor
idle seal
idle seal
elder sparrow
#

靜待其變,畢竟靜態語言要求明確的型別(或是資料大小)

idle seal
#

雀食

open crag
idle seal
#

靜態容易做嗎?

elder sparrow
#

我會說做出來產生的執行檔會非常肥大

idle seal
#

maybe

elder sparrow
#

不然你可以告訴我你預計產生出甚麼樣可以執行的形式

#

native實現python會超級肥,llvm的話是register-based也不太適合

#

大概只有bytecode才不會肥大

idle seal
#

有呼叫才編譯

#

簡單說是這樣

lilac drum
#
Cond
    ::= Value
    ::= Value CmpOp
    ::= CmpOp Value
    ::= Value CmpOp "$" CmpOp Value
    ::= Cond CondConcat Cond
    ::= "{" CondSet "|" Cond "}"
    ::= "{" CondSet ":" Cond "}"
    ::= Range
    ::= "?"
    ::= "_"

CondSet
    ::= Identifier
    ::= Identifier "N"
    ::= Identifier "Z"
    ::= Identifier "Q"
    ::= Identifier "R"
    ::= Identifier "C"
    ::= Cond

這段定義感覺會毀滅

lilac drum
#

我猜你的 N Z Q R C 是想代表數學的那些數字集合
但這跟你下面定的 Value 顯然不搭(只有 NumberFloat
而且我不認為有辦法區別 QR

idle seal
#

好像是耶,好吧

idle seal
#

反正那邊還沒做

sturdy swift
idle seal
#

py的優點不是開發簡單嗎happythonk

open crag
#

py的優點是他吃很多記憶體

idle seal
#

笑死

#

看來我的語言既靜態也動態

sturdy swift
#

#

弱型別真的會害死人,看看JS

#

而且你要怎麼隱式轉換happythonk

idle seal
#

他只是不合理的都轉成一個系統的預設值

#

叫meaningless

idle seal
#

有動態的編譯語言嗎happythonk

open crag
lilac drum
#

這個語言實際寫程式會長什麼樣子
有範例嗎
為什麼要取名叫做 Lambda

idle seal
#

因為靈感從微積分來的

open crag
#

我還以為是```py
func = lambda x: x + 1

idle seal
#

bruh

#
formula "./test.ld"
formula "./test2.ld"

main of () = 
    boolTest();
    symbolTest()
end

boolTest of () =
    print 1 > 2;
    print 1 < 2;
    print 1 == 2;
    print 1 >= 2;
    print 1 <= 2;
    print 1 != 2
end

symbolTest of () =
    print 1 + 2;
    print 1 - 2;
    print 1 * 2;
    print 1 / 2;
    print 1 % 2;
    print 1 ** 2
end

{|$x|}
(||) of x =
    >= 0 => x;
    < 0  => -x
end

{$x~$y}
(~) of x y = \
    x..y

type point = (int int)
type pointS = (x: int y: int)
type pointC: pointS =
    @pub _ of x y =
        _x = x;
        _y = y
    end

    @pub @property x of () = _x
    @pub @property y of () = _y
end
lilac drum
open crag
#

你要說的484線性代數

idle seal
#

因為微積分我就想到數學,因為數學我就想到Lambda

lilac drum
open crag
#

這什麼神奇的邏輯

idle seal
#

窩不知道

#

我爸覺得我是用右腦思考的

open crag
#

這又跟右腦什麼關係

idle seal
#

沒邏輯

lilac drum
#

你取名叫做 Lambda
別人就會覺得這是什麼 functional 之類的

idle seal
#

確實

idle seal
#

我是想做成FP的沒錯

lilac drum
#

然而你的 lambda 寫得很麻煩

idle seal
#

上面還沒有

open crag
#

BTW你的README壞了

lilac drum
#

Lambda ::= "(" Params ")" "->" Expressions "end"

idle seal
#

喔對啊

#

因為想不到

idle seal
#

我去看看

lilac drum
idle seal
#

笑死

#

結果我第一個想到的是

#

宣告函式要用of

#

f of x

lilac drum
#

為什麼要那麼麻煩

open crag
#

口語化是吧

idle seal
open crag
#

原來這是數學語言

idle seal
lilac drum
#

open crag
#

好的 封鎖清單多一個人了

lilac drum
#

type pointS = (x: int y: int) 這邊 xy 的用意是什麼

idle seal
#

可以用名稱來取屬性

#

怎麼這樣就被封鎖了

lilac drum
#

🤔 所以這個 x y 要怎麼使用

sturdy swift
sturdy swift
lilac drum
#

所以要怎麼用#

#

像是 Haskell 有所謂的 Record Syntax

data Person = Person { name :: String, age :: Int }

這邊 name 的型別就會是 Person -> String
如果 p = Person "Alice" 20,那麼 name p 就會回傳 "Alice"

sturdy swift
lilac drum
elder sparrow
idle seal
#

就直接回傳一個tuple

sturdy swift
sturdy swift
#

因為是靜態定型應該也就不需要名稱

idle seal
#

嗯嗯

open crag
#

pointS: tuple[int, int] = x, y類似這樣?

#

ㄅ欠我只會py所以用py的方式表達

idle seal
#
type Vec<T> = (length: int buf: []T)
Vec<T> of () = (0 []T)
lilac drum
#

所以說到底怎麼使用
type pointS = (x: int y: int)
這邊的 x 在哪裡可以出現

lilac drum
idle seal
#

constructor的概念?

lilac drum
#

從 functional 的角度上來看好像不是那麼好用

open crag
#

為啥感覺好像```py
class poinsS:
def init(self, x: int, y: int):
self.x = x
self.y = y

open crag
#

😂 結果是OO嗎

lilac drum
#

你應該可以把 type point = (int int) 看成

class point:
  def __init__(self, _1: int, _2: int):
    self._1 = _1
    self._2 = _2
idle seal
lilac drum
#

lilac drum
#

不好用的意思是說
如果我想要一個輸入 p : pointS 而回傳 p_x 的函式
我還要額外定義

idle seal
#

如果沒加pub就要定義

#

但如果是tuple或struct就不用

#

等等,讓我理解遺下

lilac drum
#
xOfPointS of (p : pointS) = p_x
idle seal
#

因為haskell是用atom?

lilac drum
#

我看不懂你在說什麼

idle seal
#

我不知道為什麼要額外定義

sturdy swift
#
type Point = {x: Int, y: Int}
def makePoint: Int -> Int -> Point = (x) -> (y) -> Point{x, y}
```感覺醬比較好
lilac drum
#

所以當我寫 type pointS = (x: int y: int) 之後
(p : pointS) => p_x 的函式從哪裡來?

sturdy swift
#

我覺得你其實是在搞IP,但是想結合一點所謂的FP特性,像Optional之類的

lilac drum
idle seal
#

p_x就是

#

_是函式

sturdy swift
#

皮皮應該很想傳拍額頭.gif

idle seal
sturdy swift
#

命令式

idle seal
#

喔喔

lilac drum
#

…打個比方好了
我有一個列表 ps : [pointS],我想要拿到另一個列表 xs : [int],使得我有 xs[i] == ps[i]_x
要得到 xs,functional 語言裡很自然地想法就是用 map 函式:map f ps
這邊的 f 就要是 (p : pointS) => p_x

#

在不額外定義的情況下,我要如何得到那個 f

idle seal
#

f = _x

sturdy swift
#

按他的說法_是一個運算子/函式

idle seal
#

嗯嗯

sturdy swift
#

但要像這樣用,_要是「static」

idle seal
#

x 的 y 屬性就是 x_y

lilac drum
#

umm 好
所以是 map (_x) ps

sturdy swift
#

好怪

lilac drum
#

如果把 _ 當作運算子的話這樣還能接受
就像是 map (+1) [0..10] 一樣

open crag
#

_是function還是operator?

sturdy swift
lilac drum
#

(operator 就是一個二元的 function

open crag
#

😂 好像也是

sturdy swift
#

運算子就是比較可讀的函式

idle seal
lilac drum
#

你先別嗯

idle seal
#

lilac drum
#

所以 xmap (_x) ps 裡面是什麼

idle seal
#

一個atom

#

我應該沒說錯?

#

atom是這樣用的吧?

sturdy swift
#

對欸,x不是一個在上下文(?)有定義的變數之類的

open crag
#

類似xs = map(.x, ps)?

lilac drum
#

我不知道 Atom 是啥,聽不懂

sturdy swift
idle seal
lilac drum
open crag
#

我猜是built-in var

sturdy swift
#

我覺得把_改成'x' of p會比較好

sturdy swift
#

而且醬的話屬性/欄位名是第一運算元(?

lilac drum
#

先別急著說可以

idle seal
#

lilac drum
#

所以 x y 是啥

idle seal
#

編譯器會做推論的動作,所以他會是token

lilac drum
#

不是

open crag
#

[_.x, _.y] for _ in p?

idle seal
#

sturdy swift
#

token是編譯器切分的「單詞」

idle seal
#

嗯嗯

#

資訊量衝擊,我的腦袋好小

lilac drum
idle seal
#

讓我想一下

open crag
#

而且如果p還不是只有pointS呢

idle seal
#

可以用就有值

#

不能就是meaningless

lilac drum
#

我覺得你在使用沒有共識的術語

idle seal
#

Option::None

lilac drum
#

你說可以寫 map (p_) [x y]
那這邊的 [x y] 該怎麼理解

idle seal
#

two atom token

sturdy swift
#

編譯器要怎麼理解

idle seal
#

看_出現的地方,以及最多會影響到哪個步驟,然後去推斷

lilac drum
#

我懷疑你其實沒有那個推斷能力

idle seal
#

這只是個目標

lilac drum
#

天呀,你要做的是弱型別語言欸

idle seal
#

我還可以學習

#

沒錯

#

我好緊張

lilac drum
#

在我的經驗裡
你如果想要推斷出什麼
首先你的型別要有足夠豐富的表達能力
而且應該不能太弱

idle seal
#

嗯嗯

elder sparrow
#

先說好,我認為編譯器的核心理論是context-based,雖然grammar大部分語言都是採context-free,但對於推論這點context-based所要基於的點會非常多,如果你沒辦法把一個東西明確地給出他的定義,不用說機器,連人類要寫都會很頭痛

idle seal
#

我有定義,但我不太會用言語表達

#

換句話說就是我語文爛

elder sparrow
#

TLDR要先有definition才能有usage

而這邊剛剛的map (p_) [x y]我只有看到function map的definition和local variable p_的definition,xy我無法得知他到底有沒有definition

open crag
#

x y同時可能是key of obj也可能是variable

sturdy swift
#

||```
type Point{x: Int, y: Int}

def makePoint: Int -> Int -> Point = x -> y -> Point{x, y}

def xOf: Point -> Int = p -> p.x // Notice this is "static"(?

idle seal
#

有_就是key

#

我總覺得我講錯話了

sturdy swift
idle seal
#

嗯...我...看不懂

sturdy swift
#

我也

open crag
#

this?

sturdy swift
open crag
#

如果我沒理解錯的話

#

draw.io真的好好用

lilac drum
#

我不覺得這是可行的實作

sturdy swift
#

我其實混亂到看不懂了,函數揉製是FP必須的嗎?

open crag
#

py裡是可以做到 但會用到magic method

#

但只能[key] 不能[*keys]

#

但我看不出為啥會需要這樣

#

分這兩個的用意在哪

idle seal
#

好吧,我不理解

#

那張圖

lilac drum
#

沒關係

open crag
#

這種討論484語音會比較好溝通(前提是願意講話的情況下

lilac drum
#

你想要靠程式推斷出 [x y] 並不是做不到

sturdy swift
lilac drum
#

型別表達足夠豐富的語言還是有辦法推斷 [x y] 是什麼:

map : (A -> B) -> [A] -> [B]
(p_) : <key of pointS> -> <value of pointS>
map (p_) : [<key of pointS>] -> [<value of pointS>]

所以 [x y] 必須是 [<key of pointS>]

#

或許有別的方法也說不定

open crag
sturdy swift
open crag
#

手打輔助咩

idle seal
#

那我要從頭開始講ㄇ?

#

(打字)

#

不是語音啦,是打字

#

先洗澡

lilac drum
#

oh

#

其實應該有更重要的問題

#

你怎麼知道這邊的 x 不是變數

open crag
lilac drum
#

或者說,按照你的定義,(p_) (f x) 應該是可以寫出來的

open crag
#

沒記錯他是說s前面有沒有_

lilac drum
# open crag

這不是理解它的好方式
他說了這個語言的目標是有 functional
而且 _ 是一個 operator

open crag
#

就...很不直覺

#

反正照他的說法,是這樣寫的```
map (x, p)
map (p
) [x y]

elder sparrow
open crag
#

我好像是看懂了

#

_就相當於py的.

#

做為一個連接作用

lilac drum
lilac drum
#

為什麼能知道是 p_x

idle seal
#

因為最後是x傳進去

lilac drum
#

我換個寫法ㄛ

idle seal
#

lilac drum
#

((y) -> p_y) x

#

這樣ㄋ

idle seal
#

p_y(x)

#

wait wait

#

喔喔是p_x啦

open crag
#

p x ::= p_x?

lilac drum
idle seal
#

嗯...我覺得我應該要換掉使用方法?

open crag
#

x(y) |> z之類的

lilac drum
#

我看不懂你說的#

sturdy swift
#

||```
def map<T, U> :: (T -> U) -> [T] -> [U] =
f -> ts -> {
if null ts
then []
else f (head ts) : map f (tail ts)
}

#

沒 來亂的

lilac drum
#

f (head ts) : map f (tail ts)

#

空格的優先度應該是最高ㄉ

sturdy swift
open crag
#
def map ({_prekeys}, iter{_}) {surfkeys}
  if prekeys is null
    return map (_surfkeys iter)
  ...
```||(來亂的+1||
lilac drum
#

假設 p : pointS = (0, 0)
如果我寫 x = 3; p_x
這會得到什麼結果?

idle seal
#

0

sturdy swift
open crag
#

lambda不用return

#
def map(f):
    return lambda ts: [] if ts == [] else map(f)(ts[1:]).insert(0, f(ts[0]))
sturdy swift
#

py的lambda有夠反人類

open crag
#
map = lambda f: lambda ts: [] if ts == [] else map(f)(ts[1:]).insert(0, f(ts[0]))
```(X
open crag
#

然後我發現一個點

lilac drum
#

假設 p : pointS = (0, 1)
如果我寫 x = 3; map ((f) -> f x) [(y) -> p_y, (z) -> z]
這會得到什麼結果?

open crag
#

變數名不能用底線內

#

還是說有特殊方式可以繞過?

sturdy swift
idle seal
#

嗯...那我用另一個方法好了

lilac drum
#

什麼另一個方法

sturdy swift
#

就算動態也不存在p_3,這不是巨集,因此y不會被x取代掉,大概

idle seal
#

就 everything is function

sturdy swift
idle seal
#

確實

open crag
#

member尋找順序是變數→key?

idle seal
#

所以我想改移下

sturdy swift
#

我覺得動態定型會比較好

lilac drum
#

這聽起來跟發大財一樣沒有內容

sturdy swift
#

然後_是接受一個字串,參考Lua的table

idle seal
#

我在吃飯,等一下講

foggy panther
#

Code:(~~) x f = f x infix 5 ~~ data Point = Point {x :: Int, y :: Int} p = Point 1 3 q = Point 2 6 main = do print $ p ~~ x print $ map (p ~~) [x, y] print $ map (~~ x) [p, q] print $ (\y -> p ~~ y) x(~~) x f = f x infix 5 ~~ data Point = Point {x :: Int, y :: Int} p = Point 1 3 q = Point 2 6 main = do print $ p ~~ x print $ map (p ~~) [x, y] print $ map (~~ x) [p, q] print $ (\y -> p ~~ y) x
Output:```p
[1 of 1] Compiling Main ( .code.tio.hs, .code.tio.o )

.code.tio.hs:1:17: error: parse error on input ‘infix’
|
1 | () x f = f x infix 5 ~~ data Point = Point {x :: Int, y :: Int} p = Point 1 3 q = Point 2 6 main = do print $ p ~~ x print $ map (p ) [x, y] print $ map ( x) [p, q] print $ (\y -> p ~~ y) x() x f = f x infix 5 ~~ data Point = Point {x :: Int, y :: Int} p = Point 1 3 q = Point 2 6 main = do print $ p ~~ x print $ map (p ) [x, y] print $ map ( x) [p, q] print $ (\y -> p ~~ y) x
| ^^^^^
/srv/wrappers/haskell: line 5: ./.bin.tio: No such file or directory

Exit code: 127```

lilac drum
#

./run

(~~) x f = f x
infix 5 ~~

data Point = Point {x :: Int, y :: Int}
p = Point 1 3
q = Point 2 6
main = do
  print $ p ~~ x
  print $ map (p ~~) [x, y]
  print $ map (~~ x) [p, q]
  print $ (\y -> p ~~ y) x
young caveBOT
#

Here is your haskell(9.0.1) output @lilac drum

1
[1,3]
[1,2]
1
idle seal
sturdy swift
lilac drum
#

你覺得會出什麼事

sturdy swift
#

二元運算子只有一個運算元…所以p~~是函式?但~~x是什麼

lilac drum
#

也是函式呀

#

二元運算的定義在 Haskell 是 A -> B -> C
所以其實是先吃一個元,回傳給你一個一元函式

sturdy swift
#

對,但這個順序可以反過來?

lilac drum
#

oh 這應該可以算是語法糖,對一個中綴的 op
(op y) 代表 \x -> x op y

molten vigil
#

haskell mentioned

molten vigil
#

我覺得很酷的一個例子:

#
applyFive = map ($ 5) [(+ 1), (2 ^)]
applyFive = map (\f -> f 5) [(+ 1), (2 ^)]
lilac drum
#

對,上面的 (~~) 其實就是 flip ($)

molten vigil
#

還真的有