Simplify Expressions in Prolog


I wanted to ask how I can simplify expressions like:

1+2+a*5+0/b-c*0 = 3+a*5

And especially how can I separate such expressions in lists.


It's actually kind of fun in Prolog, because you don't need to do anything too magical to make it work.

?- X-Y = 1+2+a*5+0/b-c*0. X = 1+2+a*5+0/b, Y = c*0.

So you could start by doing something like this:

simplify(C, C) :- atom(C) ; number(C). simplify(X+Y, X1+Y1) :- simplify(X, X1), simplify(Y, Y1). simplify(X*Y, X1*Y1) :- simplify(X, X1), simplify(Y, Y1). simplify(X/Y, X1/Y1) :- simplify(X, X1), simplify(Y, Y1). simplify(X-Y, X1-Y1) :- simplify(X, X1), simplify(Y, Y1).

This is an identity transform: it doesn't do anything.

?- simplify(1+2+a*5+0/b-c*0, Result). Result = 1+2+a*5+0/b-c*0.

Now you can add rules for specific cases:

simplify(X*0, 0). simplify(0*X, 0).

Now you get multiple results:

?- simplify(1+2+a*5+0/b-c*0, Result). Result = 1+2+a*5+0/b-c*0 ; Result = 1+2+a*5+0/b-0 ;

You could add a rule for constant folding:

simplify(X+Y, C) :- number(X), number(Y), C is X+Y.

You know, just have fun with it.

Lists aren't really any easier to work with, but you can make them using the "univ" operator: =..:

?- 1+2+a*5+0/b-c*0 =.. R. R = [-, 1+2+a*5+0/b, c*0].


  • Brace expansion with range in fish shell
  • Nullable type with inline if cannot work together?
  • Make a POST request
  • Specify datetime format for WCF Data Service
  • Legend control with two data frames of different x-scales and different geoms in ggplot2
  • Can NSRange determine if a snippet of text exists in a larger string?
  • How to get a list of all blobs in a repository in Git
  • Force ggplot legend to show all categories when no values are present [duplicate]
  • How to move object along the polygons
  • Java Circle-Circle Collision Detection
  • No projects found to import
  • C++ calling the default constructor with parens vs without parens [duplicate]
  • Adding/Removing Lines from a JPanel
  • Get XML response value with GDataXML
  • Creating a layer of gradient within an SVG path dynamically
  • SQL Server re-calculate or not?
  • Relative paths. baseUrl and paths not working on ionic2 - angular2
  • CORS with socket.io
  • Use sed with regex and (
  • Is it possible to define rest argument in OCaml?
  • How can I replace the server in Web Component Tester
  • Using Sax parsing to edit and write XML in VB6
  • java inputstream
  • Is there a javascript serializer for JSON.Net?
  • Fetching methods from BroadcastReceiver to update UI
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Where to put my custom functions in Wordpress?
  • R: gsub and capture
  • Numpy divide by zero. Why?
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Proper folder structure for lots of source files
  • Buffer size for converting unsigned long to string
  • python regex in pyparsing
  • Android Google Maps API OnLocationChanged only called once
  • Binding checkboxes to object values in AngularJs
  • Converting MP3 duration time
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How to load view controller without button in storyboard?