4
我正在建模關係數據庫的類型信息。我想構建如下圖:在haskell中構建圖結構
頂點是表格。
的邊緣從表A中存在表B對A的每個列是一個外鍵B.
這是用於構建圖中的初始數據我有。
newtype TableName = TableName T.Text
newtype ColName = ColName T.Text
newtype ColType = ColType T.Text
type DBInfo = H.HashMap TableName TableInfo
type TableInfo = H.HashMap ColName ColInfo
data ColInfo = CISimple ColType
-- Forms the basis for building the graph
| CIFKey ColType TableName -- col type, referenced table
getDBInfo :: IO (DBInfo)
這些是我期待的圖形結構的類型。
data SafeColInfo = SCISimple ColType
-- This captures an edge between two tables
| SCIFKey ColType SafeTableInfo
type SafeTableInfo = H.HashMap TableName SafeColInfo
type SafeDBInfo = ..
我還想寫這個函數:
convDBInfo :: DBInfo -> Either String SafeDBInfo
convDBInfo
應該建立上圖。有關t
在任何外鍵(CIFKey ctype t)
的信息可以通過查找t
在DBInfo
中找到。如果找不到,則輸入數據不一致並且是錯誤的。
這在命令式語言中很引用。但是,我想不出在Haskell中解決這個問題的方法。據我瞭解,這看起來很適合'綁結'的範式,但我似乎無法把頭圍住它。我如何編寫這個函數?
對不起,convDBInfo'應該做些什麼? – Ryan
對不起,我不清楚。編輯添加信息。 – svr
如果你知道不會有錯誤,爲什麼它需要返回'Either'? – Ryan