Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-threaded type inference #41

Draft
wants to merge 82 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
04c52ac
Add "hoping" variables
croyzor Aug 19, 2024
3a1d6fe
Expand Free monad with Define and Yield
croyzor Aug 19, 2024
bf6cfcf
Add Define/Yield cases to handler functions
croyzor Aug 20, 2024
a6f00dd
Handle Yield in toplevel handler
croyzor Aug 20, 2024
4204c50
Add bang parsing
mark-koch Aug 20, 2024
3b8fdc1
Turn on ApplicativeDo
croyzor Aug 20, 2024
8f904bc
Add some dummy code for typechecking !
croyzor Aug 20, 2024
8b852a2
Revert "Add "hoping" variables"
croyzor Aug 20, 2024
77b92fe
Some parallelisation
croyzor Aug 20, 2024
3a1288d
Add hope set to monad
croyzor Aug 20, 2024
b979c70
Move occur check logic to Eval
croyzor Aug 20, 2024
a197fb2
misc changes
croyzor Aug 20, 2024
b115f84
[broken] update eqworker
croyzor Aug 20, 2024
ce1d1cf
Revert "[broken] update eqworker"
croyzor Aug 20, 2024
c064466
Add !,! test
croyzor Aug 20, 2024
b33f100
Rewrite typeEq
mark-koch Aug 20, 2024
c093ced
feat: Solve numbers that are just variables
croyzor Aug 21, 2024
7986539
typeEqRow returns sub-problems
acl-cqc Aug 21, 2024
7afa2c4
sequence_ -> traverse_ id
acl-cqc Aug 21, 2024
265a5b6
[broken] Build things to wire into solved hopes
croyzor Aug 21, 2024
2a360b6
Don't hide that error
croyzor Aug 21, 2024
6be252b
Fix typechecking for infer example
croyzor Aug 22, 2024
646f6f9
feat: Complain when we have remaining Nat-kinded holes
croyzor Aug 22, 2024
c2c8bbe
Incude file context in hopeSet
croyzor Aug 22, 2024
3765a8a
Allow comparing VInx
croyzor Aug 22, 2024
a22531b
Rewrite dynamic num refinements to work on srcs and tgts
croyzor Aug 22, 2024
af75834
Fix demandEven logic
croyzor Aug 22, 2024
283ccae
Flip that hypo node back around
croyzor Aug 22, 2024
0da9554
Rewrite computeMeta to do definition as well
croyzor Aug 23, 2024
2139140
SolvePatterns build fixes; unified.brat+infer.brat fail with non-clos…
acl-cqc Sep 17, 2024
78cc87c
Step0. Yield in getThunks
acl-cqc Aug 21, 2024
fb99b1a
Drop already-commented-out Helpers.hs export list
acl-cqc Sep 20, 2024
07c17ed
WIP two-stage check
acl-cqc Aug 21, 2024
23853e5
Add infer_thunks(,2).brat example; ...2 works but map body not checked
acl-cqc Aug 21, 2024
75e9017
checkClause returns subproblem (oops) - BOTH infer_thunks(,2) failing…
acl-cqc Sep 20, 2024
0f21ce7
Revert "checkClause returns subproblem (oops) - BOTH infer_thunks(,2)…
acl-cqc Sep 20, 2024
3bebd4c
WIP debug (shows we stop at [to_float] and never check to_float)
acl-cqc Aug 21, 2024
dce7f0f
Try just using *> rather than returning subproblem (tests build, 59 f…
acl-cqc Sep 17, 2024
5764083
WIP add request to Fork, but TODO captureOuterLocals (tests build but…
acl-cqc Sep 17, 2024
a2ebfcf
Add captureSets, AddCapture
acl-cqc Sep 17, 2024
2e4b14e
Plumb captureSet into CompilationState (never read), various Load.hs …
acl-cqc Sep 17, 2024
85ff9c7
captureOuterLocals routes captures via AddCapture, so Box VEnv always…
acl-cqc Aug 23, 2024
c90845c
And remove VEnv from Box
acl-cqc Aug 23, 2024
115c0fa
wip debug
acl-cqc Aug 23, 2024
c24ca0d
new: Add `Lluf` operation to the BRAT hugr extension
croyzor Sep 25, 2024
672392c
checkpoint b4 I break eerything
croyzor Sep 25, 2024
9f3faa8
A couple of unit tests with Fork/Define/Yield (add assertCheckingFail)
acl-cqc Aug 23, 2024
e694a8d
Reduce change, revert checkInputs/Outputs back to Checking, also chec…
acl-cqc Sep 17, 2024
2b73543
Add string description to Fork
acl-cqc Oct 1, 2024
251455b
TEMP remove failure tests
acl-cqc Sep 13, 2024
47f4c31
Fork outside Req, with both child computations (44 fails)
acl-cqc Oct 1, 2024
9dc4b0e
WIP remove debug
acl-cqc Oct 1, 2024
554bae2
Fix compilation crashing on empty captureSet
acl-cqc Sep 18, 2024
0bfcc27
Add isSkolem func, use in typeEqEta - TODO for now True for any outport
acl-cqc Sep 18, 2024
a4115fb
simpleCheck is Checking, allow to define hopes (except nat/int?!) and…
acl-cqc Sep 18, 2024
3b76be2
typeMap stores skolem-ness (always False ATM); TypeOf returns (EndTyp…
acl-cqc Sep 17, 2024
2ebb941
Inline only use of declareSrc
acl-cqc Sep 17, 2024
e0c7997
Declare takes Bool param (still always False)
acl-cqc Oct 1, 2024
45751ac
anext' allows setting Sources to be skolem
acl-cqc Sep 17, 2024
87270bf
implement isSkolem using TypeOf
acl-cqc Sep 18, 2024
2fcdd48
Common up Checking wrappers via 'wrapper' and 'wrapper2'
acl-cqc Sep 25, 2024
75a5ac0
print hopeset when blocked
acl-cqc Oct 1, 2024
9f1c6ff
Use defineEnd more
acl-cqc Oct 1, 2024
0808dab
Add mkYield, use it
acl-cqc Oct 1, 2024
a83de68
Remove concurrency-blocking -!s in makeBox (27 -> 24 fails)
acl-cqc Oct 1, 2024
98a5671
Remove howStuck, just use Unstuck (30 -> 27 fails)
acl-cqc Oct 1, 2024
db83ba9
typeEqEta shouldWait for ends inside nums as well, assert -> unless
acl-cqc Oct 1, 2024
710c931
Redo invertNatVal
croyzor Oct 3, 2024
3b36023
Refactor typeEqEta
acl-cqc Oct 4, 2024
3e15acc
typeEqEta: fast-path without blocking for both ends the same
acl-cqc Oct 4, 2024
a900ac4
typeEqEta shouldWait for ends inside nums as well, assert -> unless
acl-cqc Oct 1, 2024
3bf4f0b
Refactor typeEqEta
acl-cqc Oct 4, 2024
2c8f26b
typeEqEta: fast-path without blocking for both ends the same
acl-cqc Oct 4, 2024
42ef6d3
Call eval after resumption in simpleCheck
acl-cqc Oct 4, 2024
5b13e9d
Merge remote-tracking branch 'origin/main' into inference-wip/fork
acl-cqc Oct 4, 2024
43bcaf8
Merge remote-tracking branch 'origin/inference-wip/fork' into new/inf…
croyzor Oct 7, 2024
62332cf
Merge remote-tracking branch 'origin/craig/inference-wip/fork-and-nat…
acl-cqc Oct 7, 2024
5517854
SolvePatterns build fixes
acl-cqc Oct 7, 2024
48cd1e2
Merge remote-tracking branch 'origin/main' into inference-wip/fork (c…
acl-cqc Oct 21, 2024
4f8099b
Comment out mapVec
acl-cqc Oct 21, 2024
9203cb6
Fix checking of infer.brat w/note, add more. Compilation fails on Pow ?!
acl-cqc Oct 21, 2024
95dad0f
Revert "TEMP remove failure tests"
acl-cqc Oct 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 122 additions & 81 deletions brat/Brat/Checker.hs

Large diffs are not rendered by default.

129 changes: 75 additions & 54 deletions brat/Brat/Checker/Helpers.hs
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
{-# LANGUAGE AllowAmbiguousTypes #-}

module Brat.Checker.Helpers {-(pullPortsRow, pullPortsSig
,simpleCheck
,combineDisjointEnvs
,ensureEmpty, noUnders
,rowToSig
,showMode, getVec
,mkThunkTy
,wire
,next, knext, anext
,kindType, getThunks
,binderToValue, valueToBinder
,kConFields
,defineSrc, defineTgt
,declareSrc, declareTgt
,makeBox
,uncons
,evalBinder
,evalSrcRow, evalTgtRow
)-} where

import Brat.Checker.Monad (Checking, CheckingSig(..), captureOuterLocals, err, typeErr, kindArgRows)
module Brat.Checker.Helpers where

import Brat.Checker.Monad (Checking, CheckingSig(..), captureOuterLocals, err, typeErr, kindArgRows, defineEnd, throwLeft, isSkolem, mkYield)
import Brat.Checker.Types
import Brat.Error (ErrorMsg(..))
import Brat.Eval (eval, EvMode(..), kindType)
import Brat.FC (FC)
import Brat.Graph (Node(..), NodeType(..))
import Brat.Naming (Name, FreshMonad(..))
import Brat.Naming (Name)
import Brat.Syntax.Common
import Brat.Syntax.Core (Term(..))
import Brat.Syntax.Simple
Expand All @@ -37,20 +19,39 @@
import Hasochism
import Util (log2)

import Control.Monad.Freer (req, Free(Ret))
import Control.Monad.Freer
import Control.Arrow ((***))
import Data.Functor ((<&>))
import Data.List (intercalate)
import Data.Type.Equality (TestEquality(..), (:~:)(..))
import qualified Data.Map as M
import qualified Data.Set as S
import Prelude hiding (last)

simpleCheck :: Modey m -> Val Z -> SimpleTerm -> Either ErrorMsg ()
simpleCheck Braty TNat (Num n) | n >= 0 = pure ()
simpleCheck Braty TInt (Num _) = pure ()
simpleCheck Braty TFloat (Float _) = pure ()
simpleCheck Braty TText (Text _) = pure ()
simpleCheck _ ty tm = Left $ TypeErr $ unwords
simpleCheck :: Modey m -> Val Z -> SimpleTerm -> Checking ()
simpleCheck my ty tm = case (my, ty) of
(Braty, VApp (VPar e) _) -> do
isHope <- req AskHopeSet <&> M.member e
if isHope then
case tm of

Check warning on line 36 in brat/Brat/Checker/Helpers.hs

View workflow job for this annotation

GitHub Actions / build

Pattern match(es) are non-exhaustive

Check warning on line 36 in brat/Brat/Checker/Helpers.hs

View workflow job for this annotation

GitHub Actions / build

Pattern match(es) are non-exhaustive
Float _ -> defineEnd e TFloat
Text _ -> defineEnd e TText
Num n | n < 0 -> defineEnd e TInt
Num _ -> typeErr $ "Can't determine whether Int or Nat: " ++ show tm
else isSkolem e >>= \case
True -> throwLeft $ helper Braty ty tm
False -> do
mkYield "simpleCheck" (S.singleton e)
ty <- eval S0 ty
simpleCheck Braty ty tm
_ -> throwLeft $ helper my ty tm
where
helper :: Modey m -> Val Z -> SimpleTerm -> Either ErrorMsg ()
helper Braty TNat (Num n) | n >= 0 = pure ()
helper Braty TInt (Num _) = pure ()
helper Braty TFloat (Float _) = pure ()
helper Braty TText (Text _) = pure ()
helper _ ty tm = Left $ TypeErr $ unwords
["Expected something of type"
,"`" ++ show ty ++ "`"
,"but got"
Expand Down Expand Up @@ -143,11 +144,6 @@
ensureEmpty _ [] = pure ()
ensureEmpty str xs = err $ InternalError $ "Expected empty " ++ str ++ ", got:\n " ++ showSig (rowToSig xs)

noUnders m = do
((outs, ()), (overs, unders)) <- m
ensureEmpty "unders" unders
pure (outs, overs)

rowToSig :: Traversable t => t (NamedPort e, ty) -> t (PortName, ty)
rowToSig = fmap $ \(p,ty) -> (portName p, ty)

Expand Down Expand Up @@ -180,26 +176,40 @@
-> Ro m i j -- Inputs and Outputs use de Bruijn indices
-> Ro m j k
-> Checking (Name, Unders m Chk, Overs m UVerb, (Semz k, Some Endz))
anext str th vals0 ins outs = do
anext str th vals0 ins outs = anext' str th vals0 ins outs $ case th of
Source -> True
_ -> False

anext' :: forall m i j k
. EvMode m
=> String
-> NodeType m
-> (Semz i, Some Endz)
-> Ro m i j -- Inputs and Outputs use de Bruijn indices
-> Ro m j k
-> Bool -- whether outports are skolem consts (will never be defined), inports never are
-> Checking (Name, Unders m Chk, Overs m UVerb, (Semz k, Some Endz))
anext' str th vals0 ins outs skol = do
node <- req (Fresh str) -- Pick a name for the thunk
-- Use the new name to generate Ends with which to instantiate types
(unders, vals1) <- endPorts node InEnd In 0 vals0 ins
(overs, vals2) <- endPorts node ExEnd Ex 0 vals1 outs
() <- sequence_ $
[ declareTgt tgt (modey @m) ty | (tgt, ty) <- unders ] ++
[ declareSrc src (modey @m) ty | (src, ty) <- overs ]
[ req (Declare (ExEnd (end src)) (modey @m) ty skol) | (src, ty) <- overs ]

let inputs = [ (portName p, biType @m ty) | (p, ty) <- unders ]
let outputs = [ (portName p, biType @m ty) | (p, ty) <- overs ]

() <- req (AddNode node (mkNode (modey @m) th inputs outputs))
pure (node, unders, overs, vals2)
where
mkNode :: forall m. Modey m -> NodeType m
-> [(PortName, Val Z)]
-> [(PortName, Val Z)]
-> Node
mkNode Braty = BratNode
mkNode Kerny = KernelNode

mkNode :: forall m. Modey m -> NodeType m
-> [(PortName, Val Z)]
-> [(PortName, Val Z)]
-> Node
mkNode Braty = BratNode
mkNode Kerny = KernelNode

type Endz = Ny :* Stack Z End

Expand Down Expand Up @@ -265,7 +275,11 @@
(nodes, unders', overs') <- getThunks Braty rest
pure (node:nodes, unders <> unders', overs <> overs')
(VFun _ _) -> err $ ExpectedThunk (showMode Braty) (showRow row)
v -> typeErr $ "Force called on non-thunk: " ++ show v
v -> do
h <- req AskHopeSet
case v of
VApp (VPar e) _ | M.member e h -> mkYield "getThunks" (S.singleton e) >> getThunks Braty row
_ -> typeErr $ "Force called on non-thunk: " ++ show v
getThunks Kerny row@((src, Right ty):rest) = eval S0 ty >>= \case
(VFun Kerny (ss :->> ts)) -> do
(node, unders, overs, _) <- let ?my = Kerny in anext "" (Splice (end src)) (S0, Some (Zy :* S0)) ss ts
Expand Down Expand Up @@ -293,16 +307,13 @@
valueToBinder Kerny = id

defineSrc :: Src -> Val Z -> Checking ()
defineSrc src v = req (Define (ExEnd (end src)) v)
defineSrc src v = defineEnd (ExEnd (end src)) v

defineTgt :: Tgt -> Val Z -> Checking ()
defineTgt tgt v = req (Define (InEnd (end tgt)) v)

declareSrc :: Src -> Modey m -> BinderType m -> Checking ()
declareSrc src my ty = req (Declare (ExEnd (end src)) my ty)
defineTgt tgt v = defineEnd (InEnd (end tgt)) v

declareTgt :: Tgt -> Modey m -> BinderType m -> Checking ()
declareTgt tgt my ty = req (Declare (InEnd (end tgt)) my ty)
declareTgt tgt my ty = req (Declare (InEnd (end tgt)) my ty False)

-- listToRow :: [(PortName, BinderType m)] -> Ro m Z i
-- listToRow [] = R0
Expand All @@ -319,14 +330,14 @@
(tgt, unders, _, _) <- anext (name ++ "/out") Target ctx ts R0
case (?my, body) of
(Kerny, _) -> do
(_,_,[thunk],_) <- next (name ++ "_thunk") (Box M.empty src tgt) (S0, Some (Zy :* S0))
(_,_,[thunk],_) <- next (name ++ "_thunk") (Box src tgt) (S0, Some (Zy :* S0))
R0 (RPr ("thunk", VFun Kerny cty) R0)
bres <- name -! body (overs, unders)
bres <- body (overs, unders)
pure (thunk, bres)
(Braty, body) -> do
(bres, captures) <- name -! (captureOuterLocals $ body (overs, unders))
(_, [], [thunk], _) <- next (name ++ "_thunk") (Box captures src tgt) (S0, Some (Zy :* S0))
(node, [], [thunk], _) <- next (name ++ "_thunk") (Box src tgt) (S0, Some (Zy :* S0))
R0 (RPr ("thunk", VFun ?my cty) R0)
bres <- (captureOuterLocals node $ body (overs, unders))
pure (thunk, bres)

-- Evaluate either mode's BinderType
Expand Down Expand Up @@ -410,3 +421,13 @@
-- 2^(2^k * upr) + 2^(2^k * upr) * (full(2^(k + k') * mono))
= pure $ NumValue (upl ^ upr) (StrictMonoFun (StrictMono (l * upr) (Full (StrictMono (k + k') mono))))
runArith _ _ _ = Nothing

buildArithOp :: ArithOp -> Checking ((Tgt, Tgt), Src)
buildArithOp op = do
(_, [(lhs,_), (rhs,_)], [(out,_)], _) <- next "" (ArithNode op) (S0, Some (Zy :* S0)) (RPr ("lhs", TNat) (RPr ("rhs", TNat) R0)) (RPr ("value", TNat) R0)
pure ((lhs, rhs), out)

buildConst :: SimpleTerm -> Val Z -> Checking Src
buildConst tm ty = do
(_, _, [(out,_)], _) <- next "" (Const tm) (S0, Some (Zy :* S0)) R0 (RPr ("value", ty) R0)
pure out
Loading
Loading