import std/io import std/list import std/ref import std/string // Multi-shot resumptions (ADR 0017): a `multi ctl` clause may call // `resume` several times; each call runs a copy of the rest of the body. effect amb { multi ctl choose :: -> Bool final ctl fail :: -> Never } // Every result of `action`: `choose` tries both answers, `fail` abandons // the current branch. fn all :: action: fn() -> a / -> List / { handle { [action ()] } with { choose -> list.append (resume True) (resume False), fail -> [] } } // The same, with `resume` kept in a list: no call is its last use, so // every call runs a copy, and the continuation itself is released when // the handler finishes. fn all_kept :: action: fn() -> a / -> List / { handle { [action ()] } with { choose -> { let kept = [resume] let results = list.append (first_of kept True) (first_of kept False) if list.length kept == 1 { results } else { [] } } , fail -> [] } } fn first_of :: ks: List List / > b: Bool -> List / { match ks { [k, ..] -> k b, [] -> [] } } fn digit :: -> Int / { if choose () { 0 } else { 1 } } // A column between `lo` and `hi`. fn pick :: lo: Int hi: Int -> Int / { if lo == hi { lo } else if choose () { lo } else { pick (lo + 1) hi } } // Whether a queen in column `col` is attacked by `queens` (the rows // above, nearest first). fn safe :: col: Int queens: List distance: Int -> Bool { match queens { [] -> True , [q, ..rest] -> q != col && q - col != distance && col - q != distance && safe col rest (distance + 1) } } fn place :: n: Int rows: Int queens: List -> List / { if rows == 0 { queens } else { let col = pick 1 n if safe col queens 1 { place n (rows - 1) [col, ..queens] } else { fail () } } } fn show_all :: xs: List> -> String { string.join (list.map xs fn x { string.join (list.map x show) "" }) " " } pub fn main :: -> () / { let pairs = all fn { (digit (), digit ()) } io.println ("pairs " <> show (list.length pairs)) let sums = all fn { let a = digit () let b = digit () if a + b == 1 { a * 10 + b } else { fail () } } io.println ("sums " <> show sums) let four = all fn { place 4 4 [] } io.println ("4 queens: " <> show_all four) let six = all fn { place 6 6 [] } match six { [first, ..] -> io.println ( "6 queens: " <> show (list.length six) <> " solutions, first " <> show_all [first] ) , [] -> io.println "6 queens: none" } // A cleanup runs once per branch that leaves its scope. let cleaned = all fn { defer io.println "branch done" digit () } io.println ("cleaned " <> show cleaned) let kept = all_kept fn { let held = list.range 1 3 defer io.println "kept branch done" digit () + list.length held } io.println ("kept " <> show kept) // References are shared by the branches; the region ends after the last. let shared = all fn { region { let r = ref.new 0 if choose () { ref.set r 1 } else { () } ref.get r } } io.println ("shared " <> show shared) // A branch that is never resumed is cancelled when the handler finishes. let none = handle { defer io.println "never resumed" [digit ()] } with { choose -> [], fail -> [] } io.println ("none " <> show (list.length none)) } //> pairs 4 //> sums [1, 10] //> 4 queens: 3142 2413 //> 6 queens: 4 solutions, first 531642 //> branch done //> branch done //> cleaned [0, 1] //> kept branch done //> kept branch done //> kept [3, 4] //> shared [1, 1] //> never resumed //> none 0