考虑以下(简化)电路模型 MySQL的:
create table circuit (
id int not null auto_increment primary key,
name varchar(30) not null
);
create table device (
id int not null auto_increment primary key,
circuit_id int not null references circuit (id),
chip varchar(30) not null,
primary key (id)
);
create table pin (
id int not null auto_increment primary key,
device_id int not null references device (id),
name varchar(10) not null unique,
unique (device_id, name)
);
create table wire (
circuit_id int not null references circuit (id),
pin1_id int not null unique references pin (id),
pin2_id int not null unique references pin (id),
primary key (pin1_id, pin2_id)
);
作为一个例子,考虑两个(有点无意义)的电路 电阻连接在一个环路中:
RES
+---/\/\/--+
| A B |
| |
| RES |
----/\/\/--+
B A
使用我前面描述的数据库结构,这可能是 表示为:
insert into circuit (name) values ('example');
set @c = last_insert_id();
insert into device (circuit_id, chip) values (@c, 'RES');
set @r1 = last_insert_id();
insert into pin (device_id, name) values (@r1, 'A');
set @a1 = last_insert_id();
insert into pin (device_id, name) values (@r1, 'B');
set @b1 = last_insert_id();
insert into device (circuit_id, chip) values (@c, 'RES');
set @r2 = last_insert_id();
insert into pin (device_id, name) values (@r2, 'A');
set @a2 = last_insert_id();
insert into pin (device_id, name) values (@r2, 'B');
set @b2 = last_insert_id();
insert into wire (circuit_id, pin1_id, pin2_id) values
(@c, @b1, @a2), (@c, @b2, @a1);
我如何在Haskell中表示这种结构?它将不绑定到数据库 - SQL就是这样我可以精确定义我想要表示的结构。
答案 0 :(得分:4)
您的pin
表类似于图表的邻接列表,因此我使用图表库 - Data.Graph
或Data.Graph.Inductive
。