Class Extents

java.lang.Object
net.hydromatic.morel.compile.Extents

public class Extents extends Object
Generates an expression for the set of values that a variable can take in a program.

If i is a variable of type int then one approximation is the set of all 232 values of the int data type. (Every data type, primitive data types and those built using sum (datatype) or product (record and tuple), has a finite set of values, but the set is usually too large to iterate over.)

There is often a better approximation that can be deduced from the uses of the variable. For example,


 let
   fun isOdd i = i % 2 = 0
 in
   from e in emps,
       i
     where isOdd i
       andalso i < 100
       andalso i = e.deptno
 end
 

we can deduce a better extent for i, namely


 from e in emps
   yield e.deptno
   where deptno % 2 = 0
     andalso deptno < 100
 
  • Constructor Details

    • Extents

      private Extents()
  • Method Details

    • create

      public static Extents.Analysis create(TypeSystem typeSystem, Core.Pat pat, SortedMap<Core.NamedPat,Core.Exp> boundPats, Iterable<? extends Core.FromStep> followingSteps, PairList<Core.IdPat,Core.Exp> idPats)
      Analyzes the extent of a pattern in an expression and creates an Extents.Analysis.

      For example, given the program

      
       let
         fun f i = i elem [1, 2, 4]
       in
         from x where f x
       end
       

      we can deduce that the extent of "x" is "[1, 2, 4]".

      We can also compute the extent of tuples. For the program

      
       let
         val edges = [(1, 2), (2, 3), (1, 4), (4, 2), (4, 3)]
         fun edge (i, j) = (i, j) elem edges
       in
         from x, y, z
         where edge (x, y) andalso edge (y, z) andalso x <> z
       end
       

      we could deduce that "x" has extent "from e in edges group e.i", "y" has extent "from e in edges group e.j" ("from e in edges group e.i" is also valid), "z" has extent "from e in edges group e.j", and therefore "(x, y, z)" has extent

      
       from x in (from e in edges group e.i),
         y in (from e in edges group e.j),
         z in (from e in edges group e.j)
       

      but we can do better by computing the extent of (x, y) simultaneously:

      
       from (x, y) in (from e in edges),
         z in (from e in edges group e.j)
       
    • flatten

      private static List<Core.IdPat> flatten(Core.Pat pat)
      Converts a singleton id pattern "x" or tuple pattern "(x, y)" to a list of id patterns.
    • isInfinite

      public static boolean isInfinite(Core.Exp exp)
      Returns whether an expression is an infinite extent.
    • infinitePats

      public static Core.Decl infinitePats(TypeSystem typeSystem, Core.Decl node)
    • intersect

      public static <C extends Comparable<C>> Map<String,com.google.common.collect.ImmutableRangeSet<C>> intersect(List<Map<String,com.google.common.collect.ImmutableRangeSet<C>>> rangeSetMaps)
      Intersects a collection of range set maps (maps from prefix to RangeSet) into one.
    • union

      public static <C extends Comparable<C>> Map<String,com.google.common.collect.ImmutableRangeSet<C>> union(List<Map<String,com.google.common.collect.ImmutableRangeSet<C>>> rangeSetMaps)
      Unions a collection of range set maps (maps from prefix to RangeSet) into one.
    • intersectRangeSets

      private static <C extends Comparable<C>> com.google.common.collect.ImmutableRangeSet<C> intersectRangeSets(Collection<com.google.common.collect.ImmutableRangeSet<C>> rangeSets)
      Intersects a collection of RangeSet into one.
      See Also:
      • ImmutableRangeSet.intersection(RangeSet)
    • unionRangeSets

      private static <C extends Comparable<C>> com.google.common.collect.ImmutableRangeSet<C> unionRangeSets(Collection<com.google.common.collect.ImmutableRangeSet<C>> rangeSets)
      Unions a collection of RangeSet into one.
      See Also:
      • ImmutableRangeSet.union(RangeSet)
    • reduceAnd

      static Pair<Core.Exp,Core.Exp> reduceAnd(TypeSystem typeSystem, PairList<Core.Exp,Core.Exp> extentFilters)
      Reduces a list of extent-filter pairs [e0, f0, e1, f1, ...] to an extent-filter pair [e0 intersect e1 ..., f0 andalso f1 ...].

      If any of the ei are calls to extent, merges them into a single extent. For example, in

      
       [extent "int: (0, inf)", x > 0,
         x elem primes, isPrime x,
         extent "int: (-inf, 10)", x < 10]
       

      the extents for "(0, inf)" and "(-inf, 10)" are merged into extent "(0, 10)":

      
       (extent "int: (0, 10)" intersect primes,
         x > 0 andalso isPrime x andalso x < 10)
       
    • reduceOr

      static Pair<Core.Exp,Core.Exp> reduceOr(TypeSystem typeSystem, PairList<Core.Exp,Core.Exp> extentFilters)
      Reduces a list of extent-filter pairs [e0, f0, e1, f1, ...] to an extent-filter pair [e0 union e1 ..., f0 orelse f1 ...].