GHCのCore言語
GHCのcompile workflowでHaskellコードがまず変換されるIR (中間表現)
構文はとてもsimpleで、10個ほどの値コンストラクタを持ったAST
この観点でHaskellの言語機能をかなり制限したものと捉えることができる
Haskell自体の拡張は継続的にされているが、Core部分は30年弱の歴史でほぼ変わっていないらしい
一方で、型システムにはSystem FCを採用しており、元のHaskellより型機能は豊かになっている
全てに対して型が明示的に指定されているため、推論の必要がなく型検査が高速
hackage
GHCの中間言語Coreへの脱糖を覗き見る - Hash λ Bye
概要が掴める
2017年の記事なので細かいところは古くなっていたりする
大枠を掴むには問題ないと思うmrsekut.icon
記事中で触れているCoreSyn(CoreのSyntax)というmoduleは9.2.1ではないっぽいmrsekut.icon
同様のものがGHC.Coreに入ってるっぽい
#WIP
小さな構文
code:hs
data Expr b
= Var Id
| Lit Literal
| App (Expr b) (Arg b)
| Lam b (Expr b)
| Let (Bind b) (Expr b)
| Case (Expr b) b Type Alt b
| Cast (Expr b) CoercionR
| Tick CoreTickish (Expr b)
| Type Type
| Coercion Coercion
deriving Data
type Arg b = Expr b
data Alt b
= Alt AltCon b (Expr b)
deriving (Data)
data AltCon
= DataAlt DataCon
| LitAlt Literal
| DEFAULT
deriving (Eq, Data)
data Bind b
= NonRec b (Expr b)
| Rec (b, (Expr b))
deriving Data
https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/compiler/core-syn-type
GHC.Coreで定義されている
https://downloads.haskell.org/~ghc/9.2.1/docs/html/libraries/ghc-9.2.1/GHC-Core.html
全て型がついているため高速に型検査ができる
既に推論済みなので検査が高速
どのように変換されるか
https://gyazo.com/91cd39b08f88160241c6f39290f368c4 https://youtu.be/haZl-q6mfyk?t=10808
https://ilyaletre.hatenablog.com/entry/2017/12/10/195016
いくつかの変換例が紹介されている
(+)とか(.)のような演算子も全部前置される
https://www.scs.stanford.edu/11au-cs240h/notes/ghc.html
脱糖
変換過程を自分で見る
{-# OPTIONS -ddump-splices -ddump-to-file -dsuppress-all #-}を追加してbuildする
--ddump-prep
-ddump-simpl
中間core?
ref
https://ilyaletre.hatenablog.com/entry/2017/12/10/195016
/haskell-shoen/Core言語
https://takenobu-hs.github.io/downloads/haskell_ghc_illustrated.pdf
https://www.youtube.com/watch?v=haZl-q6mfyk&t=10380s
STGについての発表だが、序盤でCoreについても触れている
https://www.youtube.com/watch?v=uR_VzYxvbxg
http://www.erlang-factory.com/static/upload/media/1488806820775921euc2016intothecoresimonpeytonjones.pdf
https://xtech.nikkei.com/it/atcl/news/16/092002726/
https://medium.com/@maoe/haskell-day-2016-a52cd41a06c0
https://stackoverflow.com/questions/6121146/reading-ghc-core
https://myuon.github.io/posts/giving-up-core-parser/
http://www.kotha.net/hperf/ghc.html
https://www.stephendiehl.com/posts/ghc_03.html