Skip to content

Commit

Permalink
Compile Prim nodes to Hugr DFG (#34)
Browse files Browse the repository at this point in the history
* Driveby cleanups of some misnamed `examples/`
* Add monomorphic test so it compiles+validaties rather than having any type erasure issues (#18)
  • Loading branch information
acl-cqc authored Oct 21, 2024
1 parent 7e922da commit 462a480
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
13 changes: 11 additions & 2 deletions brat/Brat/Compile/Hugr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,17 @@ compileWithInputs parent name = gets compiled <&> M.lookup name >>= \case
-- This should only have one outgoing wire which leads to an `Id` node for
-- the brat representation of the function, and that wire should have a
-- function type
Prim _ -> error $ "TODO: Compiling a Prim node being used as a thunk, not directly Evaled."
++ " Should construct trivial 4-node DFG+I/O+CustomOp Hugr."
Prim (ext,op) -> do
let n = ext ++ ('_':op)
let [] = ins
let [(_, VFun Braty cty)] = outs
box_sig@(FunctionType inputTys outputTys) <- body <$> compileSig Braty cty
((Port loadConst _, _ty), ()) <- compileConstDfg parent n box_sig $ \dfg_id -> do
ins <- addNodeWithInputs ("Inputs" ++ n) (OpIn (InputNode dfg_id inputTys)) [] inputTys
outs <- addNodeWithInputs n (OpCustom (CustomOp dfg_id ext op box_sig [])) ins outputTys
addNodeWithInputs ("Outputs" ++ n) (OpOut (OutputNode dfg_id outputTys)) outs []
pure ()
pure $ default_edges loadConst

-- Check if the node has prefix "globals", hence should be a direct call
Eval (Ex outNode outPort) -> do
Expand Down
3 changes: 3 additions & 0 deletions brat/examples/holes.brat
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
ha :: Nat
ha = ?idx

-- Holes can go in kernels
f :: { Qubit -o Qubit, Bool }
f = { q => q, ?b }
Expand Down
15 changes: 15 additions & 0 deletions brat/examples/mono_list_map.brat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ext "to_float" to_float(i :: Int) -> Float

map1({ Int -> Float }, List(Int)) -> List(Float)
map1(_, []) = []
map1(f, x ,- xs) = f(x) ,- map1(f, xs)

test1 :: List(Float)
test1 = map1(to_float, [5])

map2({ {Int -> Float} -> Float}, List({Int->Float})) -> List(Float)
map2(_, []) = []
map2(f, x ,- xs) = f(x) ,- map2(f, xs)

test2 :: List(Float)
test2 = map2({f => f(5)}, [to_float])
File renamed without changes.
9 changes: 0 additions & 9 deletions brat/examples/vector.brat
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
swap(Nat, Nat) -> Nat, Nat
swap = a, b => b, a

nat :: Nat
nat = 0

test :: Nat
test = 1

ha :: Nat
ha = ?idk

type A = Nat

isNil :: { Vec(A, 0) -> Bool }
Expand Down
2 changes: 1 addition & 1 deletion brat/test/Test/Compile/Hugr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ nonCompilingExamples = (expectedCheckingFails ++ expectedParsingFails ++
,"kernel-syntax"
,"kinds"
,"let"
,"list"
,"listpair"
,"one"
,"patterns"
,"qft"
,"test"
,"type_alias"
,"vector"
,"fanout" -- Contains Selectors
-- Conjecture: These examples don't compile because number patterns in type
Expand Down

0 comments on commit 462a480

Please sign in to comment.