import Tests.Parsingnamespace Determinize.Testsopen Frontend Spec.Paperdef inference : IO Unit := do let p ← IO.ofExcept (compile "uniform(0,1) + gauss(2,1)") check (p.annotated.sites == [.E, .E]) "unconstrained draws should use E" let p ← IO.ofExcept (compile "let x = uniform(0,1) in if x < 0.5 then x else 0") check (p.annotated.sites == [.G]) "branching must force G" let p ← IO.ofExcept (compile "uniform[G](1,2) * uniform[E](0,1)") check (p.annotated.sites == [.G, .E]) "general left multiplication" let p ← IO.ofExcept (compile "uniform[E](0,1) / uniform[G](1,2)") check (p.annotated.sites == [.E, .G]) "general denominator" for text in ["let x = uniform[E](0,1) in x*x", "uniform[E](0,1) < 0.5", "uniform[G](0,uniform[E](0,1))", "fun x => x x", "true + 1", "missing", "flip(uniform[E](0,1))", "discrete(1,2,3)", "discrete(0.2,0.3)", "discrete(-0.5,1.5)"] do check (compile text |> fun r ↦ !r.isOk) s!"invalid program accepted: {text}" for text in ["fun x => x", "[]", "inl 1", "(1,true)", "let x = uniform[G](0,1) in x + uniform[E](0,1)", "flip(0.5)", "bernoulli(0.5)", "discrete(0.25,0.25,0.5)", "observe(true)"] do check (compile text |>.isOk) s!"valid program rejected: {text}" for text in [ "uniform[G](0,1) :: uniform[E](0,1) :: []", "uniform[E](0,1) :: uniform[G](0,1) :: []", "if true then (uniform[G](0,1), true) else (uniform[E](0,1), false)", "if true then inl uniform[G](0,1) else inl uniform[E](0,1)", "let f = if true then (fun x => uniform[G](0,1)) else (fun x => uniform[E](0,1)) in f 0", "let f = if true then (rec f x => if x < 0 then f (x+1) else uniform[G](0,1)) else (fun x => \ uniform[E](0,1)) in f 0"] do let p ← IO.ofExcept (compile text) check (p.annotated.sites.contains .G && p.annotated.sites.contains .E) s!"structural subtyping changed requested affinities: {text}" for sample in ["uniform(0,1)", "uniform[E](0,1)"] do for calls in [s!"f x + f ({sample})", s!"f ({sample}) + f x"] do let text := s!"let use = fun f => fun x => {calls} + x*x in use (fun z => z) (uniform[G](0,1))" let p ← IO.ofExcept (compile text) check (p.ty == .float .E && p.annotated.sites == [.E, .G]) s!"subtype constraints lost independent affinities: {text}" for text in [ "fun x => let f = fun y => x :: y in f x", "fun x => let f = fun y => x :: y :: [] in f (x :: [])", "fun f => let g = fun x => f x in g f", "fun x => let f = fun y => x :: y :: [] in let a = f true in f 0"] do let input ← IO.ofExcept (elaborate (← IO.ofExcept (parse text))) check (!(infer input).isOk) s!"incompatible finite shapes accepted: {text}"end Determinize.Tests