packages feed

alms-0.4.9: examples/session-types-polygons.alms

-- Sutherland-Hodgman (1974) re-entrant polygon clipping

#load "libthread"
#load "libsessiontype"

open SessionType

let putAny 'a (x: 'a) = putStr (string_of x)

--
-- We first build a tiny 3-D geometry library
--

-- Points and planes in R^3.
type point = Point of float * float * float
type plane = Plane of float * float * float * float

-- We use the plane Plane(a, b, c, d) to represent the open half-space
-- { Point(x, y, z) | ax + by + cz + d > 0 }

let string_of_point (Point(x, y, z): point) =
    "(" ^ string_of x ^ ", " ^ string_of y ^ ", " ^ string_of z ^ ")"

let string_of_plane (Plane(a, b, c, d): plane) =
    string_of a ^ "x + " ^ string_of b ^ "y + " ^
    string_of c ^ "z + " ^ string_of d ^ " > 0"

(* Some of this should be in the library! *)
let splitWhile['a] : ('a -> bool) -> 'a list -> 'a list * 'a list
  = fun pred: ('a -> bool) ->
      let rec loop (acc: 'a list) (xs: 'a list) : 'a list * 'a list =
                match xs with
                | Nil         -> (rev acc, Nil['a])
                | Cons(x,xs') -> if pred x
                                   then loop (Cons(x,acc)) xs'
                                   else (rev acc, xs)
       in loop Nil['a]

let not (b: bool) = if b then false else true

let notp['a] (pred: 'a -> bool): 'a -> bool =
  fun a: 'a -> not (pred a)

let isSpace (c: int): bool =
  match c with
  | ' '  -> true
  | '\t' -> true
  | '\n' -> true
  | '\r' -> true
  | _    -> false

let dropSpace (cs : int list) : int list =
  let (_, result) = splitWhile isSpace cs in result

let parsePoint (s : string) : point =
  let foil (x: int list) = float_of_string (implode x) in
    let cs = explode s in
    let Cons('(', cs) = dropSpace cs in
    let (x, Cons(_,cs)) = splitWhile (notp ((==) ',')) (dropSpace cs) in
    let (y, Cons(_,cs)) = splitWhile (notp ((==) ',')) (dropSpace cs) in
    let (z, Cons(_,cs)) = splitWhile (notp ((==) ')')) (dropSpace cs) in
      Point (foil x, foil y, foil z)

let parsePlane (s: string) : plane =
  let foil (x: int list) = float_of_string (implode x) in
    let cs = explode s in
    let (a, Cons(_,cs)) = splitWhile (notp ((==) 'x')) (dropSpace cs) in
    let Cons('+',cs)    = dropSpace cs in
    let (b, Cons(_,cs)) = splitWhile (notp ((==) 'y')) (dropSpace cs) in
    let Cons('+',cs)    = dropSpace cs in
    let (c, Cons(_,cs)) = splitWhile (notp ((==) 'z')) (dropSpace cs) in
    let Cons('+',cs)    = dropSpace cs in
    let (d, Cons(_,cs)) = splitWhile (notp ((==) '>')) (dropSpace cs) in
    let Cons('0',cs)    = dropSpace cs in
      Plane (foil a, foil b, foil c, foil d)

-- Is the point above the plane?  (i.e., in the semi-space)
let isPointAbovePlane (Point(x, y, z): point)
                         (Plane(a, b, c, d): plane): bool =
  a *. x +. b *. y +. c *. z +. d >. 0.0

-- Does the line segment between the two points intersect the plane,
-- and if so, where?
let intersect (Point(x1, y1, z1) as p1 : point)
                 (Point(x2, y2, z2) as p2 : point)
                 (Plane(a, b, c, d) as plane : plane): point option =
 if isPointAbovePlane p1 plane == isPointAbovePlane p2 plane
   then None[point]
   else let t = (a *. x1 +. b *. y1 +. c *. z1 +. d) /.
                (a *. (x1 -. x2) +.
                 b *. (y1 -. y2) +.
                 c *. (z1 -. z2)) in
        let x = x1 +. (x2 -. x1) *. t in
        let y = y1 +. (y2 -. y1) *. t in
        let z = z1 +. (z2 -. z1) *. t in
          Some (Point (x, y, z))

--
-- When we implement the algorithm in A, we will treat points
-- and planes as opaque objects, so there's no need to marshal them,
-- but we do need to marshal options for the result of intersect.
-- The standard way to do this is to write an elimination function
-- in the "from" sublanguage and then call the elimination function
-- with "to" constructors in the "to" sublanguage:
--

let maybeC['a,'r] (some: 'a -> 'r) (none: 'r) (opt: 'a option): 'r =
  match opt with
  | Some a -> some a
  | None   -> none

--
-- In sublanguage A, our protocol is to send an unbounded
-- sequence of points:
--

type 'a stream = mu 'x. 1 |&| ?'a; 'x

--
-- Each transducer takes a plane to clip by, and two rendezvous objects,
-- the first on which it expects to receive points, and the second on
-- which it will send points.
--

let clipper (plane: plane)
               (ic: point stream channel)|
               (oc: point stream dual channel): unit =
       let finish (oc: point stream dual channel) =
             sel1 oc; () in
       let put (oc: point stream dual channel) (pt: point) =
             send (sel2 oc) pt in
       let putCross (oc: point stream dual channel)
                    (p1: point) (p2: point) =
             match intersect p1 p2 plane with
             | Some pt -> put oc pt
             | None    -> oc in
       let putVisible (oc: point stream dual channel)
                      (pt: point) =
             if isPointAbovePlane pt plane
               then put oc pt
               else oc in
         match follow ic with
         | Left _   -> finish oc
         | Right ic ->
             let (pt0, ic) = recv ic in
             let rec loop (ic: point stream channel)|
                          (oc: point stream dual channel)
                          (pt: point) : unit =
                       let oc = putVisible oc pt in
                         match follow ic with
                         | Left _   -> let oc = putCross oc pt pt0 in
                                         finish oc
                         | Right ic -> let (pt', ic) = recv ic in
                                       let oc = putCross oc pt pt' in
                                         loop ic oc pt'
               in loop ic oc pt0

let printer : point stream channel -> unit =
  let rec loop (ic: point stream channel): unit =
            match follow ic with
            | Left _   -> ()
            | Right ic -> let (pt, ic) = recv ic in
                            putStrLn (string_of_point pt);
                            loop ic
   in loop

-- The main protocol for the program, which lets us split our parser
-- from our main loop.
type main_prot = mu 'x. point stream |&| ?plane; 'x

let parser : main_prot dual channel -> unit =
  let rec plane_loop (oc: main_prot dual channel): unit =
            match getLine () with
            | "" -> point_loop (sel1 oc)
            | s  -> let plane = parsePlane s in
                    let oc    = send (sel2 oc) plane in
                      plane_loop oc
      and point_loop (oc: point stream dual channel): unit =
            match getLine () with
            | "" -> sel1 oc; ()
            | s  -> let point = parsePoint s in
                    let oc    = send (sel2 oc) point in
                      point_loop oc
   in plane_loop

let main : unit -> unit =
  let rec get_planes (acc: plane list) (ic: main_prot channel)
                     : plane list * point stream channel =
            match follow ic with
            | Left ic  -> (rev acc, ic)
            | Right ic -> let (plane, ic) = recv ic in
                            get_planes (Cons(plane,acc)) ic in
  let rec connect (planes: plane list)
                  (ic: point stream channel)
                  : point stream channel =
            match planes with
            | Nil              -> ic
            | Cons(plane,rest) ->
                let outrv = newRendezvous[point stream] () in
                  AThread.fork (fun () ->
                    clipper plane ic (accept outrv));
                  connect rest (request outrv) in
  fun () ->
    let rv           = newRendezvous[main_prot] () in
    let _            = AThread.fork (fun () -> parser (accept rv)) in
    let (planes, ic) = get_planes Nil[plane] (request rv) in
    let ic           = connect planes ic in
      printer ic

in
  main ()