Can someone help me to detect the error in the following PDDL domain:
(define (domain petri)
(:requirements :strips :fluents :typing)
(:types
place transition token
)
(:predicates
(at ?t - token ?p - place )
)
(:functions
(number-of-tokens ?p - place)
(incoming ?p - place ?t - transition)
(outgoing ?t - transition ?p - place)
)
(:action fire-transition
:parameters (?t - transition)
:preconditions (forall
(?p - place)
(or (not (incoming ?p ?t))
(> (number-of-tokens ?p) 0)))
:effects
(forall
(?p - place)
(when
(incoming ?p ?t)
(decrease (number-of-tokens ?p))))
(forall
(?p - place)
(when
(outgoing ?t ?p)
(increase (number-of-tokens ?p))))
)
I am trying to implement the PDDL domain for petri net with fire-transition function which.
Related
I have this type synonym :
type ParseResult a = Either [CompilerError] (a, [CompilerWarning])
Where CompilerError and CompilerWarning are data types.
Now I know that Either has instance for Functor and Applicative but the instance for Functor applies fmap on the tuple (a,[CompilerWarning]), I want to redefine the instance for this type synonym so that fmap applies on a not the whole tuple, the same goes for Applicative.
If I use newtype I will have to put ParseResult all over the place and I've written a lot of code already.
I'm aware I need TypeSynonymInstances but I faced the same problem in this question, from the question I think I need to define my type synonym like this :
type ParseResult = ...
I need to fill ..., I don't know how to make the right side of kind * -> * with Either and the tuple, I tried Either [CompilerError] ((,) [CompilerWarning]) but this has two problems : first CompilerWarning is the first element and I need it to be the second(so that I don't have to change a lot of code), second I get this message :
• Expecting one more argument to ‘(,) [CompilerWarning]’
Expected a type, but ‘(,) [CompilerWarning]’ has kind ‘* -> *’
• In the second argument of ‘Either’, namely
‘(,) [CompilerWarning]’
In the type ‘Either [CompilerError] ((,) [CompilerWarning])’
In the type declaration for ‘ParseResult’
What's the best, least expensive solution to this problem ?
You can't redefine existing instances (and it would be terrible if you could).
Your options are:
Make ParseResult a real type, either using newtype or something like
data ParseResult a = Failure [CompilerError] | Success a [CompilerWarning]
and define instances for it.
Don't bother with type classes at all, and just define functions like
mapParseResult :: (a -> b) -> ParseResult a -> ParseResult b
You can take advantage of both Either and (,) being bifunctors, not just functors. This means using second . first instead of fmap to apply a function to the value of type a.
> import Data.Bifunctor
> (second . first) (+1) (Right (1, []))
Right (2, [])
> (second . first) (+1) (Left ["oops"])
Left ["oops"]
first f (x, y) is equivalent to (f x, y).
second f (Right x) is equivalent to Right f x, while second f (Left y) is equivalent to Left y.
Putting them together, you can see that
(second . first) (+1) (Right (1, [])) == second (first (+1)) (Right (1, []))
== Right $ first (+1) (1, [])
== Right ((+1) 1, [])
== Right (2, [])
If you have a Left instead, nothing happens.
(second . first) (+1) (Left ["oops"]) == second (first (+1)) (Left ["oops"])
== Left ["oops"]
Since fmap is the same as second for Either, this means you can still use fmap. You just need to wrap the function with first before using it.
(second . first) f == second (first f)
== fmap (first f)
Thus
> import Data.Bifunctor
> fmap (first (+1)) (Right (1, []))
Right (2, [])
> fmap (first (+1)) (Left ["oops"])
Left ["oops"]
Consider the way core.typed annotates a function:
(t/ann typed-function [t/Str :-> t/Str])
Now consider the way Prismatic Schema annotates a function:
(s/defn schema-function :- s/Str
[arg :- s/Str]
arg)
Personally, I find the way that core.typed annotates a function be much clearer and closer in spirit to strongly typed languages like Haskell.
Question: Is there a way to make some sort of macro or function in clojure with Prismatic Schema that has the effect of (2) but the visual appearance of (1)? That is, something like the following:'
(custom-annotation-macro schema-function [s/Str :-> s/Str])
(defn schema-function [arg] arg)
such that the effect is merely just
(s/defn schema-function :- s/Str
[arg :- s/Str]
arg)
To illustrate how you could solve this with two macros:
(def type-annots (atom (hash-map)))
(defn add-type-annot [fn-name ty]
(swap! type-annots #(conj % [fn-name ty])))
(defmacro custom-annotation-macro [fn-name ty]
(add-type-annot fn-name ty)
nil)
(defn split-fun-type [ty]
;; You will need to write this one;
;; it should split [a :-> b :-> c] to [[a b] c]
['[s/Str s/Int] 's/Str])
(defmacro defn-typed [fn-name args & body]
(let [ty (get #type-annots fn-name)]
(if ty
(let [[arg-types ret-ty] (split-fun-type ty)
args-typed (apply vector (apply concat (map vector args arg-types)))]
`(s/defn ~fn-name :- ~ret-ty ~args-typed ~#body))
`(defn ~fn-name ~args ~#body))))
I haven't bothered to implement split-fun-type because I don't really know Clojure; the above is based on my understanding that it's a Lisp.
I'm compiling lambda calculus terms to interaction nets in order to evaluate them using Lamping's abstract algorithm. In order to test my implementation, I used this church-number division function:
div = (λ a b c d . (b (λ e . (e d)) (a (b (λ e f g . (e (λ h . (f h g)))) (λ e . e) (λ e f . (f (c e)))) (b (λ e f . e) (λ e . e) (λ e . e)))))
Dividing 4 by 4 (that is, (λ k . (div k k)) (λ f x . (f (f (f (f x)))))), I get this net:
(Sorry for the awful rendering. λ is a lambda, R is root, D is a fan, e is eraser.)
Reading this term back, I get the church number 1, as expected. But this net is very inflated: it has a lot of fans and erasers that serve no obvious purpose. Dividing bigger numbers is even worse. Here is div 32 32:
This again reads back as one, but here we can see an even longer tail of redundant fan nodes. My question is: is this an expected behavior of interaction needs when reducing that particular term or is this a possible bug on my implementation? If this isn't a bug, is there any way around that?
Yes, it is usual (but there are techniques to lessen its presence)
Abstracting from some details of your implementation with Interaction Nets,
and also from your hypothesis of soundness of the abstract algorithm for your div,
everything seems just fine to me.
No further interaction can be applied to the output you show, in spite of chi's claim, because none of the pairs D-e can interact through their principal port.
This latter kind of reduction rule (that is not allowed by IN framework) may improve efficiency and it is also sound in some particular cases.
Basically, the involved fan must not have any "twin", i.e. there must exists no D' in the net such that eventually the annihilation D-D' can happen.
For more details, look at The optimal implementation of functional programming language, chapter Safe nodes (which is available online!), or at the original paper from which that came:
Asperti, Andrea, and Juliusz Chroboczek. "Safe Operators: Brackets Closed Forever Optimizing Optimal λ-Calculus Implementations." Applicable Algebra in Engineering, Communication and Computing 8.6 (1997): 437-468.
Finally, the read-back procedure must be intended not as some sort of external cost for your reduction precedure, but rather as a deferred cost of computing duplication and erasure.
As you notice, such a cost is rarely negligible, so if you want to test efficiency in a real-world scenario, always sum up both sharing reduction and read-back reduction.
Consider the following example:
#lang racket
(match '(cat . doge)
[`(,a . ,b)
(match b
[a #t]
[_ #f])]
[_ "Not a pair"])
This is what I might write if I wanted to match pairs where the head and tail are the same. This doesn't work though because the second a is bound as a new variable (and matches anything). Are there any pattern forms which allow me to use the previously bound a from the outer scope?
I know this can be achieved in the following way
(match* ('cat 'doge)
[(a a) #t]
[(_ _) #f])
but I still would like to know if there is a way to get that variable from the outer scope (or if there is a reason for not doing so, like some potential name collision problem or something).
Use ==:
(match '(cat . doge)
[`(,a . ,b)
(match b
[(== a) #t]
[_ #f])]
[_ "Not a pair"])
Due to the placement in the docs, == is easy to overlook.
I have defined is-string?:
(define (is-string? expr)
(string? expr))
and also interpret-string:
(define (interpret-string expr env)
expr)
These are the functions I have written:
(define (string-equals? expr)
(and
(list? expr)
(= 3 (length expr))
(equal? '= (first expr))))
(define (interpret-string expr env)
(=
(internet (second expr) env)
(interpret (third expr) env)))
This is the interpreter for string:
((is-string? expr) (interpret-string expr env))
When I enter this line to check whether it works:
(check-expect (is-string-equals? ’(string-equals "abc" "abc"))
#t)
I get an error saying:
check-expect encountered the following error instead of the expected value, #t.
:: reference to undefined identifier: is-string-equals?
What would I have to do for is-string-equals? I have no clue.
here is the actual question asked as requested.
Extend the interpreter with a “string” datatype. You will need to write functions:
is-string?
interpret-string
and modify the interpret function to support the string datatype.
Also add two functions to work with strings within the interpreted language:
string-equals? to check if two given strings are the same.
string-join to return a new string formed by joining two strings togeter.
Make sure your functions pass the following tests:
(check-expect (is-string-equals? ’(string-equals "abc" "abc"))
#t)
(check-expect (interpret-string-equals ’(string-equals "abc" "abc") (make-env))
#t)
I see where this is going--this is for a PL class, and you're extending an existing interpreter so that it can handle strings and string comparisons--but it looks to me like it would help you a lot to add purpose statements to your functions, and test cases for all of them. Start at the top. What is is-string? supposed to do? Write test cases to make sure it does what it's supposed to. Work your way down.
Also, shame on your instructor for giving you test cases :).