sql - 用 Haskell 表示电路

标签 sql haskell

考虑以下(简化)电路模型 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 中表示这种结构?它不会与数据库绑定(bind)——SQL只是为了让我可以精确地定义我试图表示的结构。

最佳答案

您的 pin 表类似于图形的邻接列表,因此我会使用图形库 - Data.GraphData.Graph.Inducing.

关于sql - 用 Haskell 表示电路,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12735103/

相关文章:

haskell - 警告 : newtype `CInt' is used in an FFI declaration,

haskell - 使用反射和 DataKinds 进行类型推断

facebook - 如何使用 yesod-auth-fb 从 Facebook 登录获取用户的电子邮件地址?

sql - 获取用户名大小写不同的用户总数

sql - 如何计算查询时间的累计和?

java - sql语法错误 MySQLSyntaxErrorException

mysql - 更新对 mysql 表的数组响应

mysql - 使用哪种数据类型来存储分数?

haskell - 我如何从 haskell 中的函数返回多个值?

list - 连接 (++) 列表而不重叠