Skip to content

Instantly share code, notes, and snippets.

@nmattia
Created March 4, 2017 15:35
Show Gist options
  • Save nmattia/72e68c399a6e0dca64349f620375665d to your computer and use it in GitHub Desktop.
Save nmattia/72e68c399a6e0dca64349f620375665d to your computer and use it in GitHub Desktop.
Simple neo4j type system + inference

CREATE (:Value {value_ref: 42})

CREATE (:Value {value_ref: 1789})

CREATE (:Function)

MATCH (f:Function) WHERE id(f) = 33 SET f.function_name = "inc"

MATCH (f:Function {function_name: "inc"}), (v:Value {value_ref: 1789}) CREATE (v)-[:FEEDS]->(app:Application)<-[:APPLIES]-(f) CREATE (app)-[:PRODUCES]->(res:Value {value_ref: 1790})

CREATE (:Value {value_ref: "foo"})

CREATE (f:Function {function_name: "reverse"})

MATCH (f:Function {function_name: "reverse"}), (v:Value {value_ref: "foo"}) CREATE (v)-[:FEEDS]->(app:Application)<-[:APPLIES]-(f) CREATE (app)-[:PRODUCES]->(res:Value {value_ref: "oof"})

MATCH (v:Value)-[:FEEDS]->() RETURN v

CREATE (f:Function {function_name: "uppercase"})

MATCH (f:Function {function_name: "uppercase"}), (v:Value {value_ref: "foo"}) CREATE (v)-[:FEEDS]->(app:Application)<-[:APPLIES]-(f) CREATE (app)-[:PRODUCES]->(res:Value {value_ref: "FOO"})

MATCH (f:Function {function_name: "inc"}), (v:Value {value_ref: 42}) CREATE (v)-[:FEEDS]->(app:Application)<-[:APPLIES]-(f) CREATE (app)-[:PRODUCES]->(res:Value {value_ref: 43})

CREATE (t:Type {type_name: "Int"}) CREATE (t:Type {type_name: "Text"})

MATCH (v:Value {value_ref: 42}), (t:Type {type_name:"Int"}) CREATE (v)-[:HAS_TYPE]->(t)

MATCH (v:Value {value_ref: 43}), (t:Type {type_name:"Int"}) CREATE (v)-[:HAS_TYPE]->(t)

MATCH (v:Value {value_ref: 1789}), (t:Type {type_name:"Int"}) CREATE (v)-[:HAS_TYPE]->(t)

MATCH (v:Value {value_ref: 1790}), (t:Type {type_name:"Int"}) CREATE (v)-[:HAS_TYPE]->(t)

MATCH (v:Value {value_ref: "foo"}), (t:Type {type_name:"Text"}) CREATE (v)-[:HAS_TYPE]->(t)

MATCH (v:Value {value_ref: "oof"}), (t:Type {type_name:"Text"}) CREATE (v)-[:HAS_TYPE]->(t)

MATCH (v:Value {value_ref: "FOO"}), (t:Type {type_name:"Text"}) CREATE (v)-[:HAS_TYPE]->(t)

MATCH rels=()-[:HAS_TYPE]->(:Type {type_name: "Int"}) RETURN rels

MATCH rels=()-[:HAS_TYPE]->(:Type {type_name: "Text"}) RETURN rels

MATCH rels=()-[:HAS_TYPE]->(type:Type) WHERE type.type_name = "Int" RETURN rels

MATCH (int:Type {type_name: "Int"}) MATCH (text:Type {type_name: "Text"}) MATCH (f:Function) WITH (CASE f.function_name WHEN "reverse" THEN text WHEN "inc" THEN int WHEN "uppercase" THEN text END) AS typ, f AS f CREATE (f)-[:INPUT_TYPE]->(typ)

MATCH (t:Type {type_name: "Text"}) CREATE (v:Value {value_ref: "bar"})-[:HAS_TYPE]->(t)

MATCH (bar)-[:HAS_TYPE]->(typ) WHERE id(bar)=57 MATCH (f:Function)-[:INPUT_TYPE]->(typ) RETURN bar, f, typ

MERGE (str:Type {type_name:"String"}) CREATE (baz:Value {value_ref: "baz"})-[:HAS_TYPE]->(str)

MATCH (v:Value)-[:HAS_TYPE]->(t:Type) RETURN v,t

CREATE (strish:Type {type_name: "Stringish"}) WITH strish AS strish MATCH (t:Type) WHERE t.type_name = "String" OR t.type_name = "Text" CREATE (t)-[:SUBTYPE_OF]->(strish)

MATCH (f:Function) RETURN f

MATCH (uppercase:Function)-[typing:INPUT_TYPE]->(t) WHERE id(uppercase) = 47 DELETE typing WITH uppercase AS uppercase MATCH (strish:Type) WHERE id(strish) = 60 CREATE (uppercase)-[:INPUT_TYPE]->(strish)

MATCH (baz)-[:HAS_TYPE]->(t) WHERE id(baz)=59 MATCH (f:Function)-[:INPUT_TYPE]->(t) RETURN baz, f, t

MATCH (baz)-[:HAS_TYPE]->()-[:SUBTYPE_OF*0..]->(t) WHERE id(baz)=59 MATCH (f:Function)-[:INPUT_TYPE]->(t) RETURN baz, f, t

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment