Metaprogramming - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Metaprogramming

Description:

One possible coding obj meta. notation: A = coding of A. define a ... IE: infers new knowledge from the KB. Uncertainty ... ask me this (fact F) ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 25
Provided by: timokn
Category:

less

Transcript and Presenter's Notes

Title: Metaprogramming


1
Meta-programming
  • Vocabulary
  • Meta-language manipulating language
  • Object language manipulated language
  • can be the same!
  • In LP
  • use LP to reason about LP
  • meta-programs programs manipulating programs
  • compilers, debuggers, editors,
  • here interpreters

2
Interpreters
  • Functional description
  • Input program written in some language
  • Output result of executing the program
  • Representation of object program
  • important decision
  • program structure, data structures
  • ground representation
  • non-ground representation
  • meta- object languages integrated (dangerous?)

3
Ground representation
  • Model the universe of formulas
  • formulas ? terms of the meta-language
  • One possible coding obj ? meta
  • notation ltAgt coding of A
  • define a mapping F(A) ? T(ltAgt)
  • constants, variables ? constants
  • functors, predicate symbols ? functors
  • logical connectives ? functors

4
A possible coding
  • Alphabet
  • predicates, functors, constants ? themselves
  • variable ? constant v_n (n unique for each var)
  • Formulas
  • lt?Agt not(ltAgt)
  • ltL1 ? ? Lmgt ltL1gt,,ltLmgt
  • ltH ? Bgt if(ltHgt,ltBgt)
  • lt ? G gt if(, ltGgt)
  • Programs
  • ltC1,,Cngt ltC1gt, , ltCngt
  • or stored in facts clause/1

5
Some sidenotes
  • Meta- object-level should not get mixed
  • Prolog no automatic tools to prevent this
  • typed languages types for coded programs
  • Static dynamic representations
  • dynamic coded program given in some argument
  • static coded program stored in clauses

6
Interpreting a program
  • We have a search problem!
  • initial state query
  • final state empty clause
  • operations resolution
  • selection of goal
  • selection of unifying clauses
  • Notes
  • example implementations return the chain of
    clauses used as solution path (not answer
    substitution)
  • solution using a static program dfs is simpler

7
Whats the use?
  • Direct execution surely more efficient
  • Own execution surely more flexible!
  • alternative search strategies
  • debugging (tracing, statistics)
  • dynamic object programs (database updates)
  • collect proof tree (or even SLD-tree)
  • other logics (fuzzy, nonmonotonic, modal)
  • other languages (CF DCG grammars)
  • applications transformation, verification,
    synthesis

8
Ground non-ground
  • Ground interpreters are slow
  • main reason variables represented as constants
  • renaming, unification, application, answer
    extraction
  • Non-ground
  • use variable manipulation capabilities of the
    meta-language
  • represent object-level vars with meta-level vars
  • semantics changes!
  • same variables for obj. formulas individuals
  • typing helps
  • more pragmatic, less logical and surprisingly
    simple

9
Meta-interpreting full Prolog
  • Negation
  • use negation as failure
  • System predicates
  • handle separately as special cases
  • Cuts
  • obvious way does not work
  • needs ancestor cut or programmed backtracking

10
Advanced debugging
  • Tracing just add output/input commands
  • Error types
  • ? G does not terminate (stack overflow)
  • program returns wrong answers
  • program does not return some correct answer
  • Reasons
  • M(P) is not correct / complete in relation to the
    intended model IM of the programmer

11
Debugging nontermination
  • Add a depth bound D to computations
  • When D is exceeded
  • return call stack to the current goal
  • Reasons for nontermination
  • loops (,Gi,,Gj,), Gi is an instance of Gj
  • non-inductive definitions
  • How to find good D?

12
Wrong answers
  • P is incorrect (with respect to IM)
  • ? P must contain an incorrect clause C
  • When is C H ? B1,,Bn incorrect?
  • There exists an instance C? such that
  • (B1 Bm)? is in IM but H? is not
  • C? counter-example of C
  • Idea
  • find counter-examples from incorrect computations
  • How?

13
Wrong answers method 1
  • Algorithm
  • Construct the proof tree of the wrong answer
  • Traverse the tree bottom-up (postorder)
  • Ask oracle for each node (atom) whether it is
    true or not (yes/no)
  • Interpretation of oracles answer
  • No current node is the head of the
    counter-example
  • Yes continue right or upwards
  • Answers can also be provided by another (correct)
    program!

14
Better method for wrong answers
  • Method 1 asks (in worst case) about every node
  • Query complexity of oracle queries
  • very important if oracle is a human
  • Method 1 size of the proof tree
  • Method 2 depth of the proof tree
  • Idea
  • advance top-down, search for wrong descendants
  • if none then the current node is a
    counter-example
  • otherwise advance to the wrong node

15
Missing solutions
  • P should compute A but ?A fails
  • A is not covered by P
  • When does P cover some A?
  • C H ? B1, , Bn
  • B1,,Bm? ? M(P)
  • H? is an instance of A
  • if P can compute H? then P can also compute A
  • Idea for debugging
  • Find lowest-level uncovered goal MG
  • Clauses defining MG are suspects (for the missing
    solution)

16
Debugging the missing solution
  • Idea try to solve A anyway
  • If (when) A fails
  • for each C H ? B1,,Bn s.t. H? A? (mgu)
  • ask if theres a ground instance of (B1,,Bn)? in
    IM
  • no A is uncovered (and C is a suspect)
  • yes we continue from a failing (uncovered) Bi?
  • If A succeeds, it is clearly covered

17
Requirements for the instance-oracle
  • oracle(C, Answer)
  • Answer is either
  • a ground instance of C that is true in IM
  • no, if none
  • human oracle
  • some user-friendliness?
  • ask only for the variables of C
  • computer
  • compute all goals H, B1, , Bm
  • bind remaining variables (to what? any constants?)

18
Expert systems
  • Definition of an ES
  • guide for humans in problems that require some
    kind of expertise
  • Typical tasks
  • diagnosis find reasons to symptoms
  • control prevent a system from entering abnormal
    states
  • planning find a sequence of actions (e.g.
    assembly)

19
Characteristics of ESs
  • Inference engine knowledge base
  • KB knowledge about the domain (rules)
  • IE infers new knowledge from the KB
  • Uncertainty incompleteness possible in KB
  • Commercial ES GUI dialogue
  • Explanation capabilities
  • How the result was achieved
  • Why something was shown true
  • Why something was asked about
  • Support for knowledge acquisition

20
ES LP
  • KB IE clauses, SLD-resolution
  • some systems (OPL5) use forward chaining
  • ES must assume that KB is incomplete
  • KB in general terms
  • user gives the missing details
  • symptom F, program P find cause A such that
  • P ? A - F (abductive reasoning)

21
Acquiring knowledge
  • Enhance meta-interpreter
  • ask for missing knowledge
  • e.g. symptoms in a diagnosis case
  • Things to note
  • decide how to store acquired knowledge
  • do not ask same thing twice
  • do not ask blindly anything

22
Justifying and explaining
  • Answer with questions
  • Why?
  • Why does the ES ask me this (fact F)?
  • Answer rule A ? B1,,F,Bm is to be used in the
    reasoning and F is not known
  • How?
  • How did the ES come up with conclusion G?
  • Answer interpret the proof tree of G to the user

23
Telling how
  • Construct the proof tree PG of G
  • Explain the structure of PG to the user
  • tell what was asked for and what was deduced
  • filter out trivia
  • Enhancements
  • show tree level by level
  • ask user which parts of the tree should be
    explained

24
Reasoning with uncertainty
  • Here a simplistic Mycin-like implementation
  • Add certainty factors to rules
  • clause(if(H,B), CF)
  • CF our amount of trust in rule H ? B
  • Modify solver to compute with these
  • p(B1,,Bm) minp(Bi) i1..n
  • p(A) p(B?)CF, where
  • clause(if(H,B), CF) is in P and
  • ? mgu(A,H)
  • Threshold cutoff p lt 0.0001 fail
Write a Comment
User Comments (0)
About PowerShow.com