Link to Snippet

DSL for detecting patterns in 2D

A simple domain specific langauge (DSL) that can be used to specify and recognize patterrns in 2D arrays. A pattern is defined by composing primitive checks, rotating and translating patterns. See also: http://t.co/6Poty4FL

/// A type that represents a function that test/// whether an array contains some pattern at a /// specified location. It gets the location to /// test & the array as arguments and returns bool.typeShapeDetector=SDof (int->int->int[,] ->bool)
/// A primitive that tests whether the value at the/// current location contains a value 'v'letequalsv=SD (funxyarr->arr.[x,y] =v)
/// A combinator that takes 'ShapeDetector' and /// creates a new one that returns 'def' when /// accessing outside of the array boundsletborderdef (SDf) =SD (funxyarr->ifx<0||y<0||x>=arr.GetLength(0) ||y>=arr.GetLength(1)
thendefelsefxyarr)
/// A combinator that calls a given ShapeDetector/// at a location specified by offset dx, dyletaround (dx, dy) (SDf) =SD (funxyarr->f (x+dx) (y+dy) arr)
/// A combinator that takes a ShapeDetector and/// builds a new one, which is rotated by 90 degreesletrotate (SDf) =SD (funxyarr->f-yxarr)
/// Creates a shape detector that succeeds only/// when both of the arguments succeed.let (<&>) (SDf1) (SDf2) =SD (funxyarr->f1xyarr&&f2xyarr)
/// Creates a shape detector that succeeds /// when either of the arguments succeed.let (<|>) (SDf1) (SDf2) =SD (funxyarr->f1xyarr||f2xyarr)